|
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,137 @@ 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); |
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 | + }); |
79 | 128 |
|
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); |
83 | 133 | });
|
84 | 134 |
|
85 |
| - socket.on('end', () => { |
86 |
| - traceLog('Client disconnected'); |
| 135 | + server.on('error', (error: Error) => { |
| 136 | + reject(error); |
87 | 137 | });
|
88 | 138 | });
|
89 | 139 |
|
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); |
94 | 149 | });
|
| 150 | + return 0; |
| 151 | +} |
95 | 152 |
|
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 | +// } |
0 commit comments