Skip to content

Commit

Permalink
rename runtime controller
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Nov 18, 2020
1 parent ca9dca2 commit 1b51776
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const Fetcher = require('./fetcher.js');
const RuntimeController = require('../fraggle-rock/gather/runtime-controller.js');
const ExecutionContext = require('./driver/execution-context.js');
const NetworkRecorder = require('../lib/network-recorder.js');
const emulation = require('../lib/emulation.js');
const LHElement = require('../lib/lh-element.js');
Expand Down Expand Up @@ -117,7 +117,7 @@ class Driver {
/** @type {Fetcher} */
this.fetcher = new Fetcher(this);

this._runtimeController = new RuntimeController(this);
this._executionContext = new ExecutionContext(this);
}

static get traceCategories() {
Expand Down Expand Up @@ -452,7 +452,7 @@ class Driver {
* @return {Promise<*>}
*/
evaluateAsync(expression, options) {
return this._runtimeController.evaluateAsync(expression, options);
return this._executionContext.evaluateAsync(expression, options);
}

/**
Expand Down Expand Up @@ -1020,7 +1020,7 @@ class Driver {
}

await this._beginNetworkStatusMonitoring(url);
await this._runtimeController.clearContextId();
await this._executionContext.clearContextId();

// Enable auto-attaching to subtargets so we receive iframe information
await this.sendCommand('Target.setAutoAttach', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const pageFunctions = require('../../lib/page-functions.js');

class RuntimeController {
class ExecutionContext {
/** @param {LH.Gatherer.FRProtocolSession} session */
constructor(session) {
this._session = session;
Expand Down Expand Up @@ -150,4 +150,4 @@ class RuntimeController {
}
}

module.exports = RuntimeController;
module.exports = ExecutionContext;
2 changes: 1 addition & 1 deletion lighthouse-core/test/gather/driver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('.getRequestContent', () => {
});

describe('.evaluateAsync', () => {
// Most of the logic here is tested by lighthouse-core/test/fraggle-rock/gather/runtime-controller-test.js
// Most of the logic here is tested by lighthouse-core/test/gather/driver/execution-context-test.js
// Just exercise a bit of the plumbing here to ensure we delegate correctly.
it('evaluates an expression', async () => {
connectionStub.sendCommand = createMockSendCommandFn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
'use strict';

const RuntimeController = require('../../../fraggle-rock/gather/runtime-controller.js');
const ExecutionContext = require('../../../gather/driver/execution-context.js');
const {
createMockSendCommandFn: createMockSendCommandFn_,
makePromiseInspectable,
Expand All @@ -27,67 +27,67 @@ function createMockSession() {
return session;
}

describe('RuntimeController', () => {
describe('ExecutionContext', () => {
/** @type {LH.Gatherer.FRProtocolSession} */
let sessionMock;
/** @type {(controller: RuntimeController, id: number) => Promise<void>} */
/** @type {(executionContext: ExecutionContext, id: number) => Promise<void>} */
let forceNewContextId;

beforeEach(() => {
sessionMock = createMockSession();

forceNewContextId = async (controller, executionContextId) => {
controller._session.sendCommand = createMockSendCommandFn()
forceNewContextId = async (executionContext, executionContextId) => {
executionContext._session.sendCommand = createMockSendCommandFn()
.mockResponse('Page.getResourceTree', {frameTree: {frame: {id: '1337'}}})
.mockResponse('Page.createIsolatedWorld', {executionContextId})
.mockResponse('Runtime.evaluate', {result: {value: 2}});

await controller.evaluateAsync('1 + 1', {useIsolation: true});
await executionContext.evaluateAsync('1 + 1', {useIsolation: true});
};
});

it('should clear context on frame navigations', async () => {
const onMock = sessionMock.on = jest.fn();

const controller = new RuntimeController(sessionMock);
const executionContext = new ExecutionContext(sessionMock);

const frameListener = onMock.mock.calls.find(call => call[0] === 'Page.frameNavigated');
expect(frameListener).toBeDefined();

await forceNewContextId(controller, 42);
expect(controller.getContextId()).toEqual(42);
await forceNewContextId(executionContext, 42);
expect(executionContext.getContextId()).toEqual(42);
frameListener[1]();
expect(controller.getContextId()).toEqual(undefined);
expect(executionContext.getContextId()).toEqual(undefined);
});

it('should clear context on execution context destroyed', async () => {
const onMock = sessionMock.on = jest.fn();

const controller = new RuntimeController(sessionMock);
const executionContext = new ExecutionContext(sessionMock);

const executionDestroyed = onMock.mock.calls
.find(call => call[0] === 'Runtime.executionContextDestroyed');
expect(executionDestroyed).toBeDefined();

await forceNewContextId(controller, 42);
expect(controller.getContextId()).toEqual(42);
await forceNewContextId(executionContext, 42);
expect(executionContext.getContextId()).toEqual(42);
executionDestroyed[1]({executionContextId: 51});
expect(controller.getContextId()).toEqual(42);
expect(executionContext.getContextId()).toEqual(42);
executionDestroyed[1]({executionContextId: 42});
expect(controller.getContextId()).toEqual(undefined);
expect(executionContext.getContextId()).toEqual(undefined);
});
});

describe('.evaluateAsync', () => {
/** @type {LH.Gatherer.FRProtocolSession} */
let sessionMock;
/** @type {RuntimeController} */
let runtimeController;
/** @type {ExecutionContext} */
let executionContext;

beforeEach(() => {
sessionMock = createMockSession();
sessionMock.on = jest.fn();
runtimeController = new RuntimeController(sessionMock);
executionContext = new ExecutionContext(sessionMock);
});

it('evaluates an expression', async () => {
Expand All @@ -96,7 +96,7 @@ describe('.evaluateAsync', () => {
{result: {value: 2}}
));

const value = await runtimeController.evaluateAsync('1 + 1');
const value = await executionContext.evaluateAsync('1 + 1');
expect(value).toEqual(2);
sendCommand.findInvocation('Runtime.evaluate');
});
Expand All @@ -106,7 +106,7 @@ describe('.evaluateAsync', () => {
sessionMock.hasNextProtocolTimeout = jest.fn().mockReturnValue(false);
sessionMock.sendCommand = createMockSendCommandFn().mockRejectedValue(new Error('Timeout'));

const evaluatePromise = makePromiseInspectable(runtimeController.evaluateAsync('1 + 1'));
const evaluatePromise = makePromiseInspectable(executionContext.evaluateAsync('1 + 1'));

await flushAllTimersAndMicrotasks();
expect(setNextProtocolTimeout).toHaveBeenCalledWith(60000);
Expand All @@ -121,7 +121,7 @@ describe('.evaluateAsync', () => {
sessionMock.getNextProtocolTimeout = jest.fn().mockReturnValue(expectedTimeout);
sessionMock.sendCommand = createMockSendCommandFn().mockRejectedValue(new Error('Timeout'));

const evaluatePromise = makePromiseInspectable(runtimeController.evaluateAsync('1 + 1'));
const evaluatePromise = makePromiseInspectable(executionContext.evaluateAsync('1 + 1'));

await flushAllTimersAndMicrotasks();
expect(setNextProtocolTimeout).toHaveBeenCalledWith(expectedTimeout);
Expand All @@ -135,7 +135,7 @@ describe('.evaluateAsync', () => {
.mockResponse('Page.createIsolatedWorld', {executionContextId: 1})
.mockResponse('Runtime.evaluate', {result: {value: 2}}));

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

// Check that we used the correct frame when creating the isolated context
Expand All @@ -151,7 +151,7 @@ describe('.evaluateAsync', () => {
'Runtime.evaluate',
{result: {value: 2}}
);
await runtimeController.evaluateAsync('1 + 1', {useIsolation: true});
await executionContext.evaluateAsync('1 + 1', {useIsolation: true});
expect(sessionMock.sendCommand).not.toHaveBeenCalledWith(
'Page.createIsolatedWorld',
expect.anything()
Expand All @@ -167,7 +167,7 @@ describe('.evaluateAsync', () => {
.mockResponse('Page.createIsolatedWorld', {executionContextId: 9002})
.mockResponse('Runtime.evaluate', {result: {value: 'mocked value'}});

const value = await runtimeController.evaluateAsync('"magic"', {useIsolation: true});
const value = await executionContext.evaluateAsync('"magic"', {useIsolation: true});
expect(value).toEqual('mocked value');
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"files": [
// Opt-in to typechecking for some core tests.
"lighthouse-core/test/scripts/lantern/constants-test.js",
"lighthouse-core/test/fraggle-rock/gather/runtime-controller-test.js",
"lighthouse-core/test/gather/driver/execution-context-test.js",
"lighthouse-core/test/gather/driver-test.js",
"lighthouse-core/test/gather/gather-runner-test.js",
"lighthouse-core/test/audits/script-treemap-data-test.js"
Expand Down

0 comments on commit 1b51776

Please sign in to comment.