feat(rpc): update BrowserServer (#3112)

- Make BrowserServer a scope.
- Plumb exitCode and signal to BrowserServer.on('close').
- Use toImpl in a test.
This commit is contained in:
Dmitry Gozman 2020-07-24 10:17:07 -07:00 committed by GitHub
parent 0f0e2acfaf
commit deccddba24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 10 deletions

View File

@ -274,7 +274,10 @@ export interface BrowserServerChannel extends Channel {
close(params?: BrowserServerCloseParams): Promise<BrowserServerCloseResult>;
kill(params?: BrowserServerKillParams): Promise<BrowserServerKillResult>;
}
export type BrowserServerCloseEvent = {};
export type BrowserServerCloseEvent = {
exitCode?: number,
signal?: string,
};
export type BrowserServerCloseParams = {};
export type BrowserServerCloseResult = void;
export type BrowserServerKillParams = {};

View File

@ -25,8 +25,11 @@ export class BrowserServer extends ChannelOwner<BrowserServerChannel, BrowserSer
}
constructor(parent: ChannelOwner, type: string, guid: string, initializer: BrowserServerInitializer) {
super(parent, type, guid, initializer);
this._channel.on('close', () => this.emit(Events.BrowserServer.Close));
super(parent, type, guid, initializer, true);
this._channel.on('close', ({ exitCode, signal }) => {
this.emit(Events.BrowserServer.Close, exitCode === undefined ? null : exitCode, signal === undefined ? null : signal);
this._dispose();
});
}
process(): ChildProcess {

View File

@ -242,7 +242,10 @@ scheme.BrowserServerInitializer = tObject({
wsEndpoint: tString,
pid: tNumber,
});
scheme.BrowserServerCloseEvent = tObject({});
scheme.BrowserServerCloseEvent = tObject({
exitCode: tOptional(tNumber),
signal: tOptional(tString),
});
scheme.BrowserServerCloseParams = tOptional(tObject({}));
scheme.BrowserServerCloseResult = tUndefined;
scheme.BrowserServerKillParams = tOptional(tObject({}));

View File

@ -354,7 +354,9 @@ BrowserServer:
events:
close:
parameters:
exitCode: number?
signal: string?
Browser:

View File

@ -24,8 +24,14 @@ export class BrowserServerDispatcher extends Dispatcher<BrowserServer, BrowserSe
super(scope, browserServer, 'BrowserServer', {
wsEndpoint: browserServer.wsEndpoint(),
pid: browserServer.process().pid
}, true);
browserServer.on(Events.BrowserServer.Close, (exitCode, signal) => {
this._dispatchEvent('close', {
exitCode: exitCode === null ? undefined : exitCode,
signal: signal === null ? undefined : signal,
});
this._dispose();
});
browserServer.on(Events.BrowserServer.Close, () => this._dispatchEvent('close'));
}
async close(): Promise<void> {

View File

@ -327,8 +327,8 @@ describe('launchPersistentContext()', function() {
const error = await browserType.launchPersistentContext(userDataDir, options).catch(e => e);
expect(error.message).toContain('can not specify page');
});
it('should have passed URL when launching with ignoreDefaultArgs: true', async ({browserType, defaultBrowserOptions, server, userDataDir}) => {
const args = require('..')[browserType.name()]._defaultArgs(defaultBrowserOptions, 'persistent', userDataDir, 0).filter(a => a !== 'about:blank');
it.skip(USES_HOOKS)('should have passed URL when launching with ignoreDefaultArgs: true', async ({browserType, defaultBrowserOptions, server, userDataDir, toImpl}) => {
const args = toImpl(browserType)._defaultArgs(defaultBrowserOptions, 'persistent', userDataDir, 0).filter(a => a !== 'about:blank');
const options = {
...defaultBrowserOptions,
args: [...args, server.EMPTY_PAGE],

View File

@ -274,10 +274,12 @@ describe('browserType.launchServer', function() {
});
it('should fire close event', async ({browserType, defaultBrowserOptions}) => {
const browserServer = await browserType.launchServer(defaultBrowserOptions);
await Promise.all([
new Promise(f => browserServer.on('close', f)),
const [result] = await Promise.all([
new Promise(f => browserServer.on('close', (exitCode, signal) => f({ exitCode, signal }))),
browserServer.close(),
]);
expect(result.exitCode).toBe(0);
expect(result.signal).toBe(null);
});
});