Skip to content

Commit

Permalink
Move testid server (#21400)
Browse files Browse the repository at this point in the history
Created a testIdServer to handle the starting up of the testIdServer for
both unittest and pytest. This extracts the method to be in another file
and easy to test.
  • Loading branch information
eleanorjboyd authored Jun 13, 2023
1 parent 8cec0fc commit dc5fa4e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 65 deletions.
1 change: 1 addition & 0 deletions pythonFiles/vscode_pytest/run_pytest_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# Add the root directory to the path so that we can import the plugin.
directory_path = pathlib.Path(__file__).parent.parent
sys.path.append(os.fspath(directory_path))
sys.path.insert(0, os.getcwd())
# Get the rest of the args to run with pytest.
args = sys.argv[1:]
run_test_ids_port = os.environ.get("RUN_TEST_IDS_PORT")
Expand Down
2 changes: 1 addition & 1 deletion src/client/testing/testController/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContai
return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment);
}

export const startServer = (testIds: string): Promise<number> =>
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
Expand Down
36 changes: 3 additions & 33 deletions src/client/testing/testController/pytest/pytestExecutionAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import { TestRun, Uri } from 'vscode';
import * as path from 'path';
import * as net from 'net';
import { IConfigurationService, ITestOutputChannel } from '../../../common/types';
import { createDeferred, Deferred } from '../../../common/utils/async';
import { traceError, traceInfo, traceLog, traceVerbose } from '../../../logging';
Expand All @@ -23,6 +22,7 @@ import { removePositionalFoldersAndFiles } from './arguments';
import { ITestDebugLauncher, LaunchOptions } from '../../common/types';
import { PYTEST_PROVIDER } from '../../common/constants';
import { EXTENSION_ROOT_DIR } from '../../../common/constants';
import { startTestIdServer } from '../common/utils';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
// (global as any).EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR;
Expand Down Expand Up @@ -116,40 +116,10 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
if (debugBool && !testArgs.some((a) => a.startsWith('--capture') || a === '-s')) {
testArgs.push('--capture', 'no');
}

// create payload with testIds to send to run pytest script
const testData = JSON.stringify(testIds);
const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
traceLog(`Running pytest execution for the following test ids: ${testIds}`);
traceLog(`Running PYTEST execution for the following test ids: ${testIds}`);

let pytestRunTestIdsPort: string | undefined;
const startServer = (): Promise<number> =>
new Promise((resolve, reject) => {
const server = net.createServer((socket: net.Socket) => {
socket.on('end', () => {
traceVerbose('Client disconnected for pytest test ids server');
});
});

server.listen(0, () => {
const { port } = server.address() as net.AddressInfo;
traceVerbose(`Server listening on port ${port} for pytest test ids server`);
resolve(port);
});

server.on('error', (error: Error) => {
traceError('Error starting server for pytest test ids server:', error);
reject(error);
});
server.on('connection', (socket: net.Socket) => {
socket.write(payload);
traceVerbose('payload sent for pytest execution', payload);
});
});

// Start the server and wait until it is listening
await startServer()
await startTestIdServer(testIds)
.then((assignedPort) => {
traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`);
pytestRunTestIdsPort = assignedPort.toString();
Expand Down
35 changes: 4 additions & 31 deletions src/client/testing/testController/unittest/testExecutionAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

import * as path from 'path';
import { TestRun, Uri } from 'vscode';
import * as net from 'net';
import { IConfigurationService, ITestOutputChannel } from '../../../common/types';
import { createDeferred, Deferred } from '../../../common/utils/async';
import { Deferred, createDeferred } from '../../../common/utils/async';
import { EXTENSION_ROOT_DIR } from '../../../constants';
import {
DataReceivedEvent,
Expand All @@ -17,6 +16,7 @@ import {
TestExecutionCommand,
} from '../common/types';
import { traceLog, traceError } from '../../../logging';
import { startTestIdServer } from '../common/utils';

/**
* Wrapper Class for unittest test execution. This is where we call `runTestCommand`?
Expand Down Expand Up @@ -75,37 +75,10 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {

const deferred = createDeferred<ExecutionTestPayload>();
this.promiseMap.set(uuid, deferred);
// create payload with testIds to send to run pytest script
const testData = JSON.stringify(testIds);
const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
traceLog(`Running UNITTEST execution for the following test ids: ${testIds}`);

let runTestIdsPort: string | undefined;
const startServer = (): Promise<number> =>
new Promise((resolve, reject) => {
const server = net.createServer((socket: net.Socket) => {
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.on('error', (error: Error) => {
reject(error);
});
server.on('connection', (socket: net.Socket) => {
socket.write(payload);
traceLog('payload sent', payload);
});
});

// Start the server and wait until it is listening
await startServer()
await startTestIdServer(testIds)
.then((assignedPort) => {
traceLog(`Server started and listening on port ${assignedPort}`);
runTestIdsPort = assignedPort.toString();
Expand Down

0 comments on commit dc5fa4e

Please sign in to comment.