Skip to content

Execution adapter tests #21421

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

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
64 changes: 39 additions & 25 deletions src/client/testing/testController/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as net from 'net';
import { traceLog } from '../../../logging';
import { traceError, traceLog, traceVerbose } from '../../../logging';

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

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

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

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

// Send the payload to the socket
socket.write(payload);
// Send the payload to the socket
socket.write(payload);

// Handle socket events
socket.on('data', (data) => {
traceLog('Received data:', data.toString());
// Handle socket events
socket.on('data', (data) => {
traceLog('Received data:', data.toString());
});

socket.on('end', () => {
traceLog('Client disconnected');
});
});

socket.on('end', () => {
traceLog('Client disconnected');
server.listen(0, () => {
const { port } = server.address() as net.AddressInfo;
traceLog(`Server listening on port ${port}`);
resolve(port);
});
});

server.listen(0, () => {
const { port } = server.address() as net.AddressInfo;
traceLog(`Server listening on port ${port}`);
resolve(port);
server.on('error', (error: Error) => {
reject(error);
});
});

server.on('error', (error: Error) => {
reject(error);
// Start the server and wait until it is listening
await startServer()
.then((assignedPort) => {
traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`);
return assignedPort;
// if (spawnOptions.extraVariables) spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort;
})
.catch((error) => {
traceError('Error starting server for pytest test ids server:', error);
});
});
return 0;
}
16 changes: 4 additions & 12 deletions src/client/testing/testController/pytest/pytestExecutionAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,9 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
}
traceLog(`Running PYTEST execution for the following test ids: ${testIds}`);

let pytestRunTestIdsPort: string | undefined;
await startTestIdServer(testIds)
.then((assignedPort) => {
traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`);
pytestRunTestIdsPort = assignedPort.toString();
if (spawnOptions.extraVariables)
spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort;
})
.catch((error) => {
traceError('Error starting server for pytest test ids server:', error);
});
const pytestRunTestIdsPort = await startTestIdServer(testIds);
if (spawnOptions.extraVariables)
spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort.toString();

if (debugBool) {
const pytestPort = this.testServer.getPort().toString();
Expand All @@ -140,7 +132,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
testProvider: PYTEST_PROVIDER,
pytestPort,
pytestUUID,
runTestIdsPort: pytestRunTestIdsPort,
runTestIdsPort: pytestRunTestIdsPort.toString(),
};
traceInfo(`Running DEBUG pytest with arguments: ${testArgs.join(' ')}\r\n`);
await debugLauncher!.launchDebugger(launchOptions, () => {
Expand Down
16 changes: 3 additions & 13 deletions src/client/testing/testController/unittest/testExecutionAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
TestCommandOptions,
TestExecutionCommand,
} from '../common/types';
import { traceLog, traceError } from '../../../logging';
import { traceLog } from '../../../logging';
import { startTestIdServer } from '../common/utils';

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

let runTestIdsPort: string | undefined;
await startTestIdServer(testIds)
.then((assignedPort) => {
traceLog(`Server started and listening on port ${assignedPort}`);
runTestIdsPort = assignedPort.toString();
// Send test command to server.
// Server fire onDataReceived event once it gets response.
})
.catch((error) => {
traceError('Error starting server:', error);
});
const runTestIdsPort = await startTestIdServer(testIds);

await this.testServer.sendCommand(options, runTestIdsPort, () => {
await this.testServer.sendCommand(options, runTestIdsPort.toString(), () => {
// disposable.dispose();
deferred.resolve();
});
Expand Down
Loading