Skip to content

Commit 978f7e6

Browse files
committed
pytest execution functioning
1 parent ec0ca15 commit 978f7e6

File tree

3 files changed

+282
-179
lines changed

3 files changed

+282
-179
lines changed

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

Lines changed: 127 additions & 28 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,137 @@ 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);
70-
71-
// Create the headers
72-
const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
73-
74-
// Create the payload by concatenating the headers and the test data
75-
const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
76-
77-
// Send the payload to the socket
78-
socket.write(payload);
65+
// export const startTestIdServer = (testIds: string[]): Promise<number> => {
66+
// const a = 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);
70+
71+
// // Create the headers
72+
// const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
73+
74+
// // Create the payload by concatenating the headers and the test data
75+
// const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
76+
77+
// // Send the payload to the socket
78+
// socket.write(payload);
79+
80+
// // Handle socket events
81+
// socket.on('data', (data) => {
82+
// traceLog('Received data:', data.toString());
83+
// });
84+
85+
// socket.on('end', () => {
86+
// traceLog('Client disconnected');
87+
// });
88+
// });
89+
90+
// server.listen(0, () => {
91+
// const { port } = server.address() as net.AddressInfo;
92+
// traceLog(`Server listening on port ${port}`);
93+
// resolve(port);
94+
// });
95+
96+
// server.on('error', (error: Error) => {
97+
// reject(error);
98+
// });
99+
// });
100+
// return a;
101+
// };
102+
103+
export async function startTestIdServer(testIds: string[]): Promise<number> {
104+
const startServer = (): Promise<number> =>
105+
new Promise((resolve, reject) => {
106+
const server = net.createServer((socket: net.Socket) => {
107+
// Convert the test_ids array to JSON
108+
const testData = JSON.stringify(testIds);
109+
110+
// Create the headers
111+
const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
112+
113+
// Create the payload by concatenating the headers and the test data
114+
const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
115+
116+
// Send the payload to the socket
117+
socket.write(payload);
118+
119+
// Handle socket events
120+
socket.on('data', (data) => {
121+
traceLog('Received data:', data.toString());
122+
});
123+
124+
socket.on('end', () => {
125+
traceLog('Client disconnected');
126+
});
127+
});
79128

80-
// Handle socket events
81-
socket.on('data', (data) => {
82-
traceLog('Received data:', data.toString());
129+
server.listen(0, () => {
130+
const { port } = server.address() as net.AddressInfo;
131+
traceLog(`Server listening on port ${port}`);
132+
resolve(port);
83133
});
84134

85-
socket.on('end', () => {
86-
traceLog('Client disconnected');
135+
server.on('error', (error: Error) => {
136+
reject(error);
87137
});
88138
});
89139

90-
server.listen(0, () => {
91-
const { port } = server.address() as net.AddressInfo;
92-
traceLog(`Server listening on port ${port}`);
93-
resolve(port);
140+
// Start the server and wait until it is listening
141+
await startServer()
142+
.then((assignedPort) => {
143+
traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`);
144+
return assignedPort;
145+
// if (spawnOptions.extraVariables) spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort;
146+
})
147+
.catch((error) => {
148+
traceError('Error starting server for pytest test ids server:', error);
94149
});
150+
return 0;
151+
}
95152

96-
server.on('error', (error: Error) => {
97-
reject(error);
98-
});
99-
});
153+
// export async function startTestIdsServerFunc(testIds: string[]): Promise<number> {
154+
// const a = new Promise((resolve, reject) => {
155+
// const server = net.createServer((socket: net.Socket) => {
156+
// // Convert the test_ids array to JSON
157+
// const testData = JSON.stringify(testIds);
158+
159+
// // Create the headers
160+
// const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
161+
162+
// // Create the payload by concatenating the headers and the test data
163+
// const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
164+
165+
// // Send the payload to the socket
166+
// socket.write(payload);
167+
168+
// // Handle socket events
169+
// socket.on('data', (data) => {
170+
// traceLog('Received data:', data.toString());
171+
// });
172+
173+
// socket.on('end', () => {
174+
// traceLog('Client disconnected');
175+
// });
176+
// });
177+
178+
// server.listen(0, () => {
179+
// const { port } = server.address() as net.AddressInfo;
180+
// traceLog(`Server listening on port ${port}`);
181+
// resolve(port);
182+
// });
183+
184+
// server.on('error', (error: Error) => {
185+
// reject(error);
186+
// });
187+
// });
188+
// await a
189+
// .then((assignedPort: number) => {
190+
// traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`);
191+
// return assignedPort;
192+
// // if (spawnOptions.extraVariables) spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort;
193+
// })
194+
// .catch((error) => {
195+
// traceError('Error starting server for test ids:', error);
196+
// });
197+
// return 0;
198+
// }

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, () => {

0 commit comments

Comments
 (0)