|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 | import * as net from 'net'; |
4 | | -import { traceLog } from '../../../logging'; |
| 4 | +import { traceError, traceLog, traceVerbose } from '../../../logging'; |
5 | 5 |
|
6 | 6 | import { EnableTestAdapterRewrite } from '../../../common/experiments/groups'; |
7 | 7 | import { IExperimentService } from '../../../common/types'; |
@@ -62,38 +62,52 @@ export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContai |
62 | 62 | return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment); |
63 | 63 | } |
64 | 64 |
|
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); |
70 | 71 |
|
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']; |
73 | 74 |
|
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}`; |
76 | 77 |
|
77 | | - // Send the payload to the socket |
78 | | - socket.write(payload); |
| 78 | + // Send the payload to the socket |
| 79 | + socket.write(payload); |
79 | 80 |
|
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 | + }); |
83 | 89 | }); |
84 | 90 |
|
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); |
87 | 95 | }); |
88 | | - }); |
89 | 96 |
|
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 | + }); |
94 | 100 | }); |
95 | 101 |
|
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); |
98 | 111 | }); |
99 | | - }); |
| 112 | + return 0; |
| 113 | +} |
0 commit comments