Skip to content

Commit

Permalink
cherry-pick(#30226): chore: migrate to the testserver.initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Apr 3, 2024
1 parent 6b94231 commit 79d4776
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class StdinServer implements Transport {
}

async dispatch(method: string, params: any) {
if (method === 'ready') {
if (method === 'initialize') {
if (this._traceUrl)
this._loadTrace(this._traceUrl);
}
Expand Down
28 changes: 10 additions & 18 deletions packages/playwright/src/isomorphic/testServerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
});
const pingInterval = setInterval(() => this._sendMessage('ping').catch(() => {}), 30000);
this._connectedPromise = new Promise<void>((f, r) => {
this._ws.addEventListener('open', () => {
f();
this._ws.send(JSON.stringify({ id: -1, method: 'ready' }));
});
this._ws.addEventListener('open', () => f());
this._ws.addEventListener('error', r);
});
this._ws.addEventListener('close', () => {
Expand All @@ -76,10 +73,6 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
});
}

connect() {
return this._connectedPromise;
}

private async _sendMessage(method: string, params?: any): Promise<any> {
const logForTest = (globalThis as any).__logForTest;
logForTest?.({ method, params });
Expand Down Expand Up @@ -110,8 +103,8 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this._onLoadTraceRequestedEmitter.fire(params);
}

async setSerializer(params: { serializer: string; }): Promise<void> {
await this._sendMessage('setSerializer', params);
async initialize(params: Parameters<TestServerInterface['initialize']>[0]): ReturnType<TestServerInterface['initialize']> {
await this._sendMessage('initialize', params);
}

async ping(params: Parameters<TestServerInterface['ping']>[0]): ReturnType<TestServerInterface['ping']> {
Expand All @@ -130,10 +123,6 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this._sendMessageNoReply('watch', params);
}

async watchTestDir(params: Parameters<TestServerInterface['watchTestDir']>[0]): ReturnType<TestServerInterface['watchTestDir']> {
await this._sendMessage('watchTestDir', params);
}

async open(params: Parameters<TestServerInterface['open']>[0]): ReturnType<TestServerInterface['open']> {
await this._sendMessage('open', params);
}
Expand Down Expand Up @@ -190,11 +179,14 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this._sendMessageNoReply('stopTests', params);
}

async setInterceptStdio(params: Parameters<TestServerInterface['setInterceptStdio']>[0]): ReturnType<TestServerInterface['setInterceptStdio']> {
await this._sendMessage('setInterceptStdio', params);
}

async closeGracefully(params: Parameters<TestServerInterface['closeGracefully']>[0]): ReturnType<TestServerInterface['closeGracefully']> {
await this._sendMessage('closeGracefully', params);
}

close() {
try {
this._ws.close();
} catch {
}
}
}
11 changes: 6 additions & 5 deletions packages/playwright/src/isomorphic/testServerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ import type { JsonEvent } from './teleReceiver';
export type ReportEntry = JsonEvent;

export interface TestServerInterface {
setSerializer(params: { serializer: string }): Promise<void>;
initialize(params: {
serializer?: string,
closeOnDisconnect?: boolean,
interceptStdio?: boolean,
watchTestDirs?: boolean,
}): Promise<void>;

ping(params: {}): Promise<void>;

watch(params: {
fileNames: string[];
}): Promise<void>;

watchTestDir(params: {}): Promise<void>;

open(params: { location: reporterTypes.Location }): Promise<void>;

resizeTerminal(params: { cols: number, rows: number }): Promise<void>;
Expand Down Expand Up @@ -90,8 +93,6 @@ export interface TestServerInterface {

stopTests(params: {}): Promise<void>;

setInterceptStdio(params: { intercept: boolean }): Promise<void>;

closeGracefully(params: {}): Promise<void>;
}

Expand Down
35 changes: 20 additions & 15 deletions packages/playwright/src/runner/testServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TestServer {
}

async stop() {
await this._dispatcher?.setInterceptStdio({ intercept: false });
await this._dispatcher?._setInterceptStdio(false);
await this._dispatcher?.runGlobalTeardown();
}
}
Expand All @@ -72,13 +72,17 @@ class TestServerDispatcher implements TestServerInterface {
readonly _dispatchEvent: TestServerInterfaceEventEmitters['dispatchEvent'];
private _plugins: TestRunnerPluginRegistration[] | undefined;
private _serializer = require.resolve('./uiModeReporter');
private _watchTestDir = false;
private _watchTestDirs = false;
private _closeOnDisconnect = false;

constructor(configFile: string | undefined) {
this._configFile = configFile;
this.transport = {
dispatch: (method, params) => (this as any)[method](params),
onclose: () => {},
onclose: () => {
if (this._closeOnDisconnect)
gracefullyProcessExitDoNotHang(0);
},
};
this._globalWatcher = new Watcher('deep', () => this._dispatchEvent('listChanged', {}));
this._testWatcher = new Watcher('flat', events => {
Expand All @@ -89,10 +93,6 @@ class TestServerDispatcher implements TestServerInterface {
this._dispatchEvent = (method, params) => this.transport.sendEvent?.(method, params);
}

async setSerializer(params: { serializer: string; }): Promise<void> {
this._serializer = params.serializer;
}

private async _wireReporter(messageSink: (message: any) => void) {
return await createReporterForTestServer(this._serializer, messageSink);
}
Expand All @@ -104,7 +104,16 @@ class TestServerDispatcher implements TestServerInterface {
return { reporter, report };
}

async ready() {}
async initialize(params: Parameters<TestServerInterface['initialize']>[0]): ReturnType<TestServerInterface['initialize']> {
if (params.serializer)
this._serializer = params.serializer;
if (params.closeOnDisconnect)
this._closeOnDisconnect = true;
if (params.interceptStdio)
await this._setInterceptStdio(true);
if (params.watchTestDirs)
this._watchTestDirs = true;
}

async ping() {}

Expand Down Expand Up @@ -226,7 +235,7 @@ class TestServerDispatcher implements TestServerInterface {
projectOutputs.add(result.outDir);
}

if (this._watchTestDir)
if (this._watchTestDirs)
this._globalWatcher.update([...projectDirs], [...projectOutputs], false);
return { report, status };
}
Expand Down Expand Up @@ -295,10 +304,6 @@ class TestServerDispatcher implements TestServerInterface {
return { status: await run };
}

async watchTestDir() {
this._watchTestDir = true;
}

async watch(params: { fileNames: string[]; }) {
const files = new Set<string>();
for (const fileName of params.fileNames) {
Expand All @@ -321,10 +326,10 @@ class TestServerDispatcher implements TestServerInterface {
await this._testRun?.run;
}

async setInterceptStdio(params: Parameters<TestServerInterface['setInterceptStdio']>[0]): ReturnType<TestServerInterface['setInterceptStdio']> {
async _setInterceptStdio(intercept: boolean) {
if (process.env.PWTEST_DEBUG)
return;
if (params.intercept) {
if (intercept) {
process.stdout.write = (chunk: string | Buffer) => {
this._dispatchEvent('stdio', chunkToPayload('stdout', chunk));
return true;
Expand Down
6 changes: 4 additions & 2 deletions packages/trace-viewer/src/ui/uiModeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ export const UIModeView: React.FC<{}> = ({
setIsLoading(true);
setWatchedTreeIds({ value: new Set() });
(async () => {
await testServerConnection.setInterceptStdio({ intercept: true });
await testServerConnection.watchTestDir({});
await testServerConnection.initialize({
interceptStdio: true,
watchTestDirs: true
});
const { status } = await testServerConnection.runGlobalSetup({});
if (status !== 'passed')
return;
Expand Down
1 change: 1 addition & 0 deletions packages/trace-viewer/src/ui/workbenchLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const WorkbenchLoader: React.FunctionComponent<{
setDragOver(false);
setProcessingErrorMessage(null);
});
testServerConnection.initialize({}).catch(() => {});
} else if (!newTraceURLs.some(url => url.startsWith('blob:'))) {
// Don't re-use blob file URLs on page load (results in Fetch error)
setTraceURLs(newTraceURLs);
Expand Down

0 comments on commit 79d4776

Please sign in to comment.