Skip to content

Commit 4d1c196

Browse files
authored
Execution adapter tests (#21421)
adds test coverage for `testExecutionAdapter.ts` and `pytestExecutionAdapter.ts`.
1 parent dc5fa4e commit 4d1c196

File tree

5 files changed

+260
-307
lines changed

5 files changed

+260
-307
lines changed

src/client/testing/testController/common/utils.ts

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33
import * as net from 'net';
4-
import { traceLog } from '../../../logging';
4+
import { traceError, traceLog, traceVerbose } from '../../../logging';
55

66
import { EnableTestAdapterRewrite } from '../../../common/experiments/groups';
77
import { IExperimentService } from '../../../common/types';
@@ -62,38 +62,52 @@ export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContai
6262
return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment);
6363
}
6464

65-
export const startTestIdServer = (testIds: string[]): Promise<number> =>
66-
new Promise((resolve, reject) => {
67-
const server = net.createServer((socket: net.Socket) => {
68-
// Convert the test_ids array to JSON
69-
const testData = JSON.stringify(testIds);
65+
export async function startTestIdServer(testIds: string[]): Promise<number> {
66+
const startServer = (): Promise<number> =>
67+
new Promise((resolve, reject) => {
68+
const server = net.createServer((socket: net.Socket) => {
69+
// Convert the test_ids array to JSON
70+
const testData = JSON.stringify(testIds);
7071

71-
// Create the headers
72-
const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
72+
// Create the headers
73+
const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
7374

74-
// Create the payload by concatenating the headers and the test data
75-
const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
75+
// Create the payload by concatenating the headers and the test data
76+
const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
7677

77-
// Send the payload to the socket
78-
socket.write(payload);
78+
// Send the payload to the socket
79+
socket.write(payload);
7980

80-
// Handle socket events
81-
socket.on('data', (data) => {
82-
traceLog('Received data:', data.toString());
81+
// Handle socket events
82+
socket.on('data', (data) => {
83+
traceLog('Received data:', data.toString());
84+
});
85+
86+
socket.on('end', () => {
87+
traceLog('Client disconnected');
88+
});
8389
});
8490

85-
socket.on('end', () => {
86-
traceLog('Client disconnected');
91+
server.listen(0, () => {
92+
const { port } = server.address() as net.AddressInfo;
93+
traceLog(`Server listening on port ${port}`);
94+
resolve(port);
8795
});
88-
});
8996

90-
server.listen(0, () => {
91-
const { port } = server.address() as net.AddressInfo;
92-
traceLog(`Server listening on port ${port}`);
93-
resolve(port);
97+
server.on('error', (error: Error) => {
98+
reject(error);
99+
});
94100
});
95101

96-
server.on('error', (error: Error) => {
97-
reject(error);
102+
// Start the server and wait until it is listening
103+
await startServer()
104+
.then((assignedPort) => {
105+
traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`);
106+
return assignedPort;
107+
// if (spawnOptions.extraVariables) spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort;
108+
})
109+
.catch((error) => {
110+
traceError('Error starting server for pytest test ids server:', error);
98111
});
99-
});
112+
return 0;
113+
}

src/client/testing/testController/pytest/pytestExecutionAdapter.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,9 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
118118
}
119119
traceLog(`Running PYTEST execution for the following test ids: ${testIds}`);
120120

121-
let pytestRunTestIdsPort: string | undefined;
122-
await startTestIdServer(testIds)
123-
.then((assignedPort) => {
124-
traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`);
125-
pytestRunTestIdsPort = assignedPort.toString();
126-
if (spawnOptions.extraVariables)
127-
spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort;
128-
})
129-
.catch((error) => {
130-
traceError('Error starting server for pytest test ids server:', error);
131-
});
121+
const pytestRunTestIdsPort = await startTestIdServer(testIds);
122+
if (spawnOptions.extraVariables)
123+
spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort.toString();
132124

133125
if (debugBool) {
134126
const pytestPort = this.testServer.getPort().toString();
@@ -140,7 +132,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
140132
testProvider: PYTEST_PROVIDER,
141133
pytestPort,
142134
pytestUUID,
143-
runTestIdsPort: pytestRunTestIdsPort,
135+
runTestIdsPort: pytestRunTestIdsPort.toString(),
144136
};
145137
traceInfo(`Running DEBUG pytest with arguments: ${testArgs.join(' ')}\r\n`);
146138
await debugLauncher!.launchDebugger(launchOptions, () => {

src/client/testing/testController/unittest/testExecutionAdapter.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
TestCommandOptions,
1616
TestExecutionCommand,
1717
} from '../common/types';
18-
import { traceLog, traceError } from '../../../logging';
18+
import { traceLog } from '../../../logging';
1919
import { startTestIdServer } from '../common/utils';
2020

2121
/**
@@ -77,19 +77,9 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
7777
this.promiseMap.set(uuid, deferred);
7878
traceLog(`Running UNITTEST execution for the following test ids: ${testIds}`);
7979

80-
let runTestIdsPort: string | undefined;
81-
await startTestIdServer(testIds)
82-
.then((assignedPort) => {
83-
traceLog(`Server started and listening on port ${assignedPort}`);
84-
runTestIdsPort = assignedPort.toString();
85-
// Send test command to server.
86-
// Server fire onDataReceived event once it gets response.
87-
})
88-
.catch((error) => {
89-
traceError('Error starting server:', error);
90-
});
80+
const runTestIdsPort = await startTestIdServer(testIds);
9181

92-
await this.testServer.sendCommand(options, runTestIdsPort, () => {
82+
await this.testServer.sendCommand(options, runTestIdsPort.toString(), () => {
9383
// disposable.dispose();
9484
deferred.resolve();
9585
});

0 commit comments

Comments
 (0)