Skip to content

Commit 79d4776

Browse files
committed
cherry-pick(#30226): chore: migrate to the testserver.initialize
1 parent 6b94231 commit 79d4776

File tree

6 files changed

+42
-41
lines changed

6 files changed

+42
-41
lines changed

packages/playwright-core/src/server/trace/viewer/traceViewer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class StdinServer implements Transport {
222222
}
223223

224224
async dispatch(method: string, params: any) {
225-
if (method === 'ready') {
225+
if (method === 'initialize') {
226226
if (this._traceUrl)
227227
this._loadTrace(this._traceUrl);
228228
}

packages/playwright/src/isomorphic/testServerConnection.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
6464
});
6565
const pingInterval = setInterval(() => this._sendMessage('ping').catch(() => {}), 30000);
6666
this._connectedPromise = new Promise<void>((f, r) => {
67-
this._ws.addEventListener('open', () => {
68-
f();
69-
this._ws.send(JSON.stringify({ id: -1, method: 'ready' }));
70-
});
67+
this._ws.addEventListener('open', () => f());
7168
this._ws.addEventListener('error', r);
7269
});
7370
this._ws.addEventListener('close', () => {
@@ -76,10 +73,6 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
7673
});
7774
}
7875

79-
connect() {
80-
return this._connectedPromise;
81-
}
82-
8376
private async _sendMessage(method: string, params?: any): Promise<any> {
8477
const logForTest = (globalThis as any).__logForTest;
8578
logForTest?.({ method, params });
@@ -110,8 +103,8 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
110103
this._onLoadTraceRequestedEmitter.fire(params);
111104
}
112105

113-
async setSerializer(params: { serializer: string; }): Promise<void> {
114-
await this._sendMessage('setSerializer', params);
106+
async initialize(params: Parameters<TestServerInterface['initialize']>[0]): ReturnType<TestServerInterface['initialize']> {
107+
await this._sendMessage('initialize', params);
115108
}
116109

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

133-
async watchTestDir(params: Parameters<TestServerInterface['watchTestDir']>[0]): ReturnType<TestServerInterface['watchTestDir']> {
134-
await this._sendMessage('watchTestDir', params);
135-
}
136-
137126
async open(params: Parameters<TestServerInterface['open']>[0]): ReturnType<TestServerInterface['open']> {
138127
await this._sendMessage('open', params);
139128
}
@@ -190,11 +179,14 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
190179
this._sendMessageNoReply('stopTests', params);
191180
}
192181

193-
async setInterceptStdio(params: Parameters<TestServerInterface['setInterceptStdio']>[0]): ReturnType<TestServerInterface['setInterceptStdio']> {
194-
await this._sendMessage('setInterceptStdio', params);
195-
}
196-
197182
async closeGracefully(params: Parameters<TestServerInterface['closeGracefully']>[0]): ReturnType<TestServerInterface['closeGracefully']> {
198183
await this._sendMessage('closeGracefully', params);
199184
}
185+
186+
close() {
187+
try {
188+
this._ws.close();
189+
} catch {
190+
}
191+
}
200192
}

packages/playwright/src/isomorphic/testServerInterface.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ import type { JsonEvent } from './teleReceiver';
2121
export type ReportEntry = JsonEvent;
2222

2323
export interface TestServerInterface {
24-
setSerializer(params: { serializer: string }): Promise<void>;
24+
initialize(params: {
25+
serializer?: string,
26+
closeOnDisconnect?: boolean,
27+
interceptStdio?: boolean,
28+
watchTestDirs?: boolean,
29+
}): Promise<void>;
2530

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

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

32-
watchTestDir(params: {}): Promise<void>;
33-
3437
open(params: { location: reporterTypes.Location }): Promise<void>;
3538

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

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

93-
setInterceptStdio(params: { intercept: boolean }): Promise<void>;
94-
9596
closeGracefully(params: {}): Promise<void>;
9697
}
9798

packages/playwright/src/runner/testServer.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class TestServer {
5656
}
5757

5858
async stop() {
59-
await this._dispatcher?.setInterceptStdio({ intercept: false });
59+
await this._dispatcher?._setInterceptStdio(false);
6060
await this._dispatcher?.runGlobalTeardown();
6161
}
6262
}
@@ -72,13 +72,17 @@ class TestServerDispatcher implements TestServerInterface {
7272
readonly _dispatchEvent: TestServerInterfaceEventEmitters['dispatchEvent'];
7373
private _plugins: TestRunnerPluginRegistration[] | undefined;
7474
private _serializer = require.resolve('./uiModeReporter');
75-
private _watchTestDir = false;
75+
private _watchTestDirs = false;
76+
private _closeOnDisconnect = false;
7677

7778
constructor(configFile: string | undefined) {
7879
this._configFile = configFile;
7980
this.transport = {
8081
dispatch: (method, params) => (this as any)[method](params),
81-
onclose: () => {},
82+
onclose: () => {
83+
if (this._closeOnDisconnect)
84+
gracefullyProcessExitDoNotHang(0);
85+
},
8286
};
8387
this._globalWatcher = new Watcher('deep', () => this._dispatchEvent('listChanged', {}));
8488
this._testWatcher = new Watcher('flat', events => {
@@ -89,10 +93,6 @@ class TestServerDispatcher implements TestServerInterface {
8993
this._dispatchEvent = (method, params) => this.transport.sendEvent?.(method, params);
9094
}
9195

92-
async setSerializer(params: { serializer: string; }): Promise<void> {
93-
this._serializer = params.serializer;
94-
}
95-
9696
private async _wireReporter(messageSink: (message: any) => void) {
9797
return await createReporterForTestServer(this._serializer, messageSink);
9898
}
@@ -104,7 +104,16 @@ class TestServerDispatcher implements TestServerInterface {
104104
return { reporter, report };
105105
}
106106

107-
async ready() {}
107+
async initialize(params: Parameters<TestServerInterface['initialize']>[0]): ReturnType<TestServerInterface['initialize']> {
108+
if (params.serializer)
109+
this._serializer = params.serializer;
110+
if (params.closeOnDisconnect)
111+
this._closeOnDisconnect = true;
112+
if (params.interceptStdio)
113+
await this._setInterceptStdio(true);
114+
if (params.watchTestDirs)
115+
this._watchTestDirs = true;
116+
}
108117

109118
async ping() {}
110119

@@ -226,7 +235,7 @@ class TestServerDispatcher implements TestServerInterface {
226235
projectOutputs.add(result.outDir);
227236
}
228237

229-
if (this._watchTestDir)
238+
if (this._watchTestDirs)
230239
this._globalWatcher.update([...projectDirs], [...projectOutputs], false);
231240
return { report, status };
232241
}
@@ -295,10 +304,6 @@ class TestServerDispatcher implements TestServerInterface {
295304
return { status: await run };
296305
}
297306

298-
async watchTestDir() {
299-
this._watchTestDir = true;
300-
}
301-
302307
async watch(params: { fileNames: string[]; }) {
303308
const files = new Set<string>();
304309
for (const fileName of params.fileNames) {
@@ -321,10 +326,10 @@ class TestServerDispatcher implements TestServerInterface {
321326
await this._testRun?.run;
322327
}
323328

324-
async setInterceptStdio(params: Parameters<TestServerInterface['setInterceptStdio']>[0]): ReturnType<TestServerInterface['setInterceptStdio']> {
329+
async _setInterceptStdio(intercept: boolean) {
325330
if (process.env.PWTEST_DEBUG)
326331
return;
327-
if (params.intercept) {
332+
if (intercept) {
328333
process.stdout.write = (chunk: string | Buffer) => {
329334
this._dispatchEvent('stdio', chunkToPayload('stdout', chunk));
330335
return true;

packages/trace-viewer/src/ui/uiModeView.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ export const UIModeView: React.FC<{}> = ({
172172
setIsLoading(true);
173173
setWatchedTreeIds({ value: new Set() });
174174
(async () => {
175-
await testServerConnection.setInterceptStdio({ intercept: true });
176-
await testServerConnection.watchTestDir({});
175+
await testServerConnection.initialize({
176+
interceptStdio: true,
177+
watchTestDirs: true
178+
});
177179
const { status } = await testServerConnection.runGlobalSetup({});
178180
if (status !== 'passed')
179181
return;

packages/trace-viewer/src/ui/workbenchLoader.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const WorkbenchLoader: React.FunctionComponent<{
9393
setDragOver(false);
9494
setProcessingErrorMessage(null);
9595
});
96+
testServerConnection.initialize({}).catch(() => {});
9697
} else if (!newTraceURLs.some(url => url.startsWith('blob:'))) {
9798
// Don't re-use blob file URLs on page load (results in Fetch error)
9899
setTraceURLs(newTraceURLs);

0 commit comments

Comments
 (0)