fix(chromium): pause workers on start to not miss any events (#832)

This commit is contained in:
Dmitry Gozman 2020-02-04 19:36:46 -08:00 committed by GitHub
parent 4b761f4485
commit 8f1df5e1e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 11 deletions

View file

@ -98,6 +98,7 @@ export class CRPage implements PageDelegate {
this._client.send('Page.setLifecycleEventsEnabled', { enabled: true }),
this._client.send('Runtime.enable', {}).then(() => this._ensureIsolatedWorld(UTILITY_WORLD_NAME)),
this._networkManager.initialize(),
this._client.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true }),
];
const options = this._page.browserContext()._options;
if (options.bypassCSP)
@ -215,10 +216,14 @@ export class CRPage implements PageDelegate {
}
_onAttachedToTarget(event: Protocol.Target.attachedToTargetPayload) {
if (event.targetInfo.type !== 'worker')
return;
const url = event.targetInfo.url;
const session = CRConnection.fromSession(this._client).session(event.sessionId)!;
if (event.targetInfo.type !== 'worker') {
// Ideally, detaching should resume any target, but there is bug in the backend.
session.send('Runtime.runIfWaitingForDebugger').catch(debugError);
this._client.send('Target.detachFromTarget', { sessionId: event.sessionId }).catch(debugError);
return;
}
const url = event.targetInfo.url;
const worker = new Worker(url);
this._page._addWorker(event.sessionId, worker);
session.once('Runtime.executionContextCreated', async event => {
@ -227,6 +232,7 @@ export class CRPage implements PageDelegate {
Promise.all([
session.send('Runtime.enable'),
session.send('Network.enable'),
session.send('Runtime.runIfWaitingForDebugger'),
]).catch(debugError); // This might fail if the target is closed before we initialize.
session.on('Runtime.consoleAPICalled', event => {
const args = event.args.map(o => worker._existingExecutionContext!._createHandle(o));

View file

@ -84,14 +84,7 @@ export class CRTarget {
const page = this._crPage.page();
(page as any)[targetSymbol] = this;
client.once(CRSessionEvents.Disconnected, () => page._didDisconnect());
client.on('Target.attachedToTarget', event => {
if (event.targetInfo.type !== 'worker') {
// If we don't detach from service workers, they will never die.
client.send('Target.detachFromTarget', { sessionId: event.sessionId }).catch(debugError);
}
});
await this._crPage.initialize();
await client.send('Target.setAutoAttach', {autoAttach: true, waitForDebuggerOnStart: false, flatten: true});
return page;
});
}

View file

@ -113,7 +113,7 @@ module.exports.describe = function({testRunner, expect, FFOX, CHROMIUM, WEBKIT})
expect(response.request()).toBe(request);
expect(response.ok()).toBe(true);
});
it.skip(CHROMIUM)('should report network activity on worker creation', async function({page, server}) {
it('should report network activity on worker creation', async function({page, server}) {
// Chromium needs waitForDebugger enabled for this one.
await page.goto(server.EMPTY_PAGE);
const url = server.PREFIX + '/one-style.css';