Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixes for useIsolation
  • Loading branch information
patrickhulce committed Apr 26, 2019
commit 6f6a598ea6b40594270dd316eae9ba7ea489bc3a
19 changes: 18 additions & 1 deletion lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ class Driver {
this._handleReceivedMessageFromTarget(event, []).catch(this._handleEventError);
});

this.on('Runtime.executionContextDestroyed', event => {
if (event.executionContextId === this._isolatedExecutionContextId) {
this._clearIsolatedContextId();
}
});

this.on('Page.frameNavigated', this._clearIsolatedContextId.bind(this));

connection.on('protocolevent', this._handleProtocolEvent.bind(this));

/**
Expand Down Expand Up @@ -464,7 +472,16 @@ class Driver {
*/
async evaluateAsync(expression, options = {}) {
const contextId = options.useIsolation ? await this._getOrCreateIsolatedContextId() : undefined;
return this._evaluateInContext(expression, contextId);
return this._evaluateInContext(expression, contextId).catch(async err => {
// If we were using isolation and the context disappeared on us, retry one more time.
if (contextId && err.message.includes('Cannot find context')) {
this._clearIsolatedContextId();
const freshContextId = await this._getOrCreateIsolatedContextId();
return this._evaluateInContext(expression, freshContextId);
}

throw err;
});
}

/**
Expand Down
13 changes: 13 additions & 0 deletions lighthouse-core/test/gather/driver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,19 @@ describe('.evaluateAsync', () => {
expect.anything()
);
});

it('recovers from isolation failures', async () => {
connectionStub.sendCommand = createMockSendCommandFn()
.mockResponse('Page.getResourceTree', {frameTree: {frame: {id: 1337}}})
.mockResponse('Page.createIsolatedWorld', {executionContextId: 9001})
.mockResponse('Runtime.evaluate', Promise.reject(new Error('Cannot find context')))
.mockResponse('Page.getResourceTree', {frameTree: {frame: {id: 1337}}})
.mockResponse('Page.createIsolatedWorld', {executionContextId: 9002})
.mockResponse('Runtime.evaluate', {result: {value: 2}});

const value = await driver.evaluateAsync('1 + 1', {useIsolation: true});
expect(value).toEqual(2);
});
});

describe('.sendCommand', () => {
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/gather/gather-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ describe('GatherRunner', function() {
afterPass(context) {
context.driver.scrollTo({x: 1000, y: 1000});
}
};
}

const url = 'https://example.com';
const driver = Object.assign({}, fakeDriver);
Expand Down