Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use test server as a singleton #423

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions src/playwrightTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ export class PlaywrightTest {
const env = await reporterServer.env({ selfDestruct: false });
const reporter = reporterServer.reporterFile();
if (mode === 'list')
testServer.list({ locations, reporter, env });
testServer.list({ configFile: config.configFile, locations, reporter, env });
if (mode === 'run') {
testServer.test({ locations, reporter, env, options });
testServer.test({ configFile: config.configFile, locations, reporter, env, ...options });
token.onCancellationRequested(() => {
testServer.stop();
testServer.stop({ configFile: config.configFile });
});
testServer.on('stdio', params => {
if (params.type === 'stdout')
Expand Down Expand Up @@ -309,7 +309,7 @@ export class PlaywrightTest {
const testServer = await this._testServerController.testServerFor(config);
if (!testServer)
return await this._findRelatedTestFilesCLI(config, files);
return await testServer.findRelatedTestFiles({ files });
return await testServer.findRelatedTestFiles({ configFile: config.configFile, files });
}

async debugTests(vscode: vscodeTypes.VSCode, config: TestConfig, projectNames: string[], testDirs: string[], settingsEnv: NodeJS.ProcessEnv, locations: string[] | null, listener: TestListener, parametrizedTestTitle: string | undefined, token: vscodeTypes.CancellationToken) {
Expand Down
72 changes: 60 additions & 12 deletions src/testServerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@
import { BackendClient, BackendServer } from './backend';
import { ConfigFindRelatedTestFilesReport } from './listTests';
import { TestConfig } from './playwrightTest';
import type { TestError } from './reporter';
import * as vscodeTypes from './vscodeTypes';

export class TestServerController implements vscodeTypes.Disposable {
private _vscode: vscodeTypes.VSCode;
private _testServers = new Map<TestConfig, TestServer>();
private _instancePromise: Promise<TestServer | null> | undefined;
private _instance: TestServer | null = null;
private _envProvider: () => NodeJS.ProcessEnv;

constructor(vscode: vscodeTypes.VSCode, envProvider: () => NodeJS.ProcessEnv) {
this._vscode = vscode;
this._envProvider = envProvider;
}

async testServerFor(config: TestConfig): Promise<TestServer | null> {
const existing = this._testServers.get(config);
if (existing)
return existing;
async testServerFor(config: TestConfig): Promise<TestServerInterface & TestServerEvents | null> {
if (this._instancePromise)
return this._instancePromise;
this._instancePromise = this._createTestServer(config);
return this._instancePromise;
}

private async _createTestServer(config: TestConfig): Promise<TestServer | null> {
const args = [config.cli, 'test-server'];
const testServerBackend = new BackendServer<TestServer>(this._vscode, {
args,
Expand All @@ -47,9 +53,7 @@ export class TestServerController implements vscodeTypes.Disposable {
dumpIO: false,
});
const testServer = await testServerBackend.start();
if (!testServer)
return null;
this._testServers.set(config, testServer);
this._instance = testServer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be unused.

return testServer;
}

Expand All @@ -58,13 +62,52 @@ export class TestServerController implements vscodeTypes.Disposable {
}

reset() {
for (const backend of this._testServers.values())
backend.close();
this._testServers.clear();
if (this._instancePromise)
this._instancePromise.then(server => server?.closeGracefully());
this._instancePromise = undefined;
this._instance = null;
}
}

class TestServer extends BackendClient {
interface TestServerInterface {
list(params: {
configFile: string;
locations: string[];
reporter: string;
env: NodeJS.ProcessEnv;
}): Promise<void>;

test(params: {
configFile: string;
locations: string[];
reporter: string;
env: NodeJS.ProcessEnv;
headed?: boolean;
oneWorker?: boolean;
trace?: 'on' | 'off';
projects?: string[];
grep?: string;
reuseContext?: boolean;
connectWsEndpoint?: string;
}): Promise<void>;

findRelatedTestFiles(params: {
configFile: string;
files: string[];
}): Promise<{ testFiles: string[]; errors?: TestError[]; }>;

stop(params: {
configFile: string;
}): Promise<void>;

closeGracefully(): Promise<void>;
}

interface TestServerEvents {
on(event: 'stdio', listener: (params: { type: 'stdout' | 'stderr', text?: string, buffer?: string }) => void): void;
}

class TestServer extends BackendClient implements TestServerInterface, TestServerEvents {
override async initialize(): Promise<void> {
}

Expand All @@ -83,4 +126,9 @@ class TestServer extends BackendClient {
async stop() {
await this.send('stop', {});
}

async closeGracefully() {
await this.send('closeGracefully', {});
this.close();
}
}
Loading