forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
allow large scale testing #21269
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
Merged
allow large scale testing #21269
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cc4b112
allow large scale testing
eleanorjboyd 972d708
remove comment
eleanorjboyd e90deca
pass test ids through stdin
eleanorjboyd 807fa21
working but need to clean up
eleanorjboyd cf0cde1
working with process json
eleanorjboyd 4eba881
clean up
eleanorjboyd 9730dcb
type edits
eleanorjboyd 9db9cd2
fix logging
eleanorjboyd 83256eb
fix pyright for pr check
eleanorjboyd 2090f23
fix typing
eleanorjboyd 81c5971
switch to stdinStr
eleanorjboyd 88b0389
remove extra version pyright note
eleanorjboyd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
import io | ||
import json | ||
import os | ||
import pathlib | ||
import socket | ||
import sys | ||
from typing import List | ||
|
||
import pytest | ||
|
||
CONTENT_LENGTH: str = "Content-Length:" | ||
|
||
|
||
def process_rpc_json(data: str) -> List[str]: | ||
"""Process the JSON data which comes from the server which runs the pytest discovery.""" | ||
str_stream: io.StringIO = io.StringIO(data) | ||
|
||
length: int = 0 | ||
|
||
while True: | ||
line: str = str_stream.readline() | ||
if CONTENT_LENGTH.lower() in line.lower(): | ||
length = int(line[len(CONTENT_LENGTH) :]) | ||
break | ||
|
||
if not line or line.isspace(): | ||
raise ValueError("Header does not contain Content-Length") | ||
|
||
while True: | ||
line: str = str_stream.readline() | ||
if not line or line.isspace(): | ||
break | ||
|
||
raw_json: str = str_stream.read(length) | ||
return json.loads(raw_json) | ||
|
||
|
||
# This script handles running pytest via pytest.main(). It is called via run in the | ||
# pytest execution adapter and gets the test_ids to run via stdin and the rest of the | ||
# args through sys.argv. It then runs pytest.main() with the args and test_ids. | ||
|
||
if __name__ == "__main__": | ||
# 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)) | ||
# 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") | ||
run_test_ids_port_int = ( | ||
int(run_test_ids_port) if run_test_ids_port is not None else 0 | ||
) | ||
test_ids_from_buffer = [] | ||
try: | ||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
client_socket.connect(("localhost", run_test_ids_port_int)) | ||
print(f"CLIENT: Server listening on port {run_test_ids_port_int}...") | ||
buffer = b"" | ||
|
||
while True: | ||
# Receive the data from the client | ||
data = client_socket.recv(1024 * 1024) | ||
if not data: | ||
break | ||
|
||
# Append the received data to the buffer | ||
buffer += data | ||
|
||
try: | ||
# Try to parse the buffer as JSON | ||
test_ids_from_buffer = process_rpc_json(buffer.decode("utf-8")) | ||
# Clear the buffer as complete JSON object is received | ||
buffer = b"" | ||
|
||
# Process the JSON data | ||
print(f"Received JSON data: {test_ids_from_buffer}") | ||
break | ||
except json.JSONDecodeError: | ||
# JSON decoding error, the complete JSON object is not yet received | ||
continue | ||
except socket.error as e: | ||
print(f"Error: Could not connect to runTestIdsPort: {e}") | ||
print("Error: Could not connect to runTestIdsPort") | ||
try: | ||
if test_ids_from_buffer: | ||
arg_array = ["-p", "vscode_pytest"] + args + test_ids_from_buffer | ||
pytest.main(arg_array) | ||
except json.JSONDecodeError: | ||
print("Error: Could not parse test ids from stdin") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,10 @@ | |
|
||
import { 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 { traceVerbose } from '../../../logging'; | ||
import { traceLog, traceVerbose } from '../../../logging'; | ||
import { DataReceivedEvent, ExecutionTestPayload, ITestExecutionAdapter, ITestServer } from '../common/types'; | ||
import { | ||
ExecutionFactoryCreateWithEnvironmentOptions, | ||
|
@@ -90,6 +91,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { | |
TEST_PORT: this.testServer.getPort().toString(), | ||
}, | ||
outputChannel: this.outputChannel, | ||
stdinStr: testIds.toString(), | ||
}; | ||
|
||
// Create the Python environment in which to execute the command. | ||
|
@@ -114,7 +116,48 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { | |
if (debugBool && !testArgs.some((a) => a.startsWith('--capture') || a === '-s')) { | ||
testArgs.push('--capture', 'no'); | ||
} | ||
const pluginArgs = ['-p', 'vscode_pytest', '-v'].concat(testArgs).concat(testIds); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -v was not supposed to be included in the first place- this should be removed |
||
const pluginArgs = ['-p', 'vscode_pytest'].concat(testArgs).concat(testIds); | ||
const scriptPath = path.join(fullPluginPath, 'vscode_pytest', 'run_pytest_script.py'); | ||
const runArgs = [scriptPath, ...testArgs]; | ||
|
||
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}`; | ||
|
||
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() | ||
.then((assignedPort) => { | ||
traceLog(`Server started and listening on port ${assignedPort}`); | ||
if (spawnOptions.extraVariables) | ||
spawnOptions.extraVariables.RUN_TEST_IDS_PORT = assignedPort.toString(); | ||
}) | ||
.catch((error) => { | ||
console.error('Error starting server:', error); | ||
}); | ||
|
||
if (debugBool) { | ||
const pytestPort = this.testServer.getPort().toString(); | ||
const pytestUUID = uuid.toString(); | ||
|
@@ -129,9 +172,10 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { | |
console.debug(`Running debug test with arguments: ${pluginArgs.join(' ')}\r\n`); | ||
await debugLauncher!.launchDebugger(launchOptions); | ||
} else { | ||
const runArgs = ['-m', 'pytest'].concat(pluginArgs); | ||
console.debug(`Running test with arguments: ${runArgs.join(' ')}\r\n`); | ||
execService?.exec(runArgs, spawnOptions); | ||
await execService?.exec(runArgs, spawnOptions).catch((ex) => { | ||
console.debug(`Error while running tests: ${testIds}\r\n${ex}\r\n\r\n`); | ||
return Promise.reject(ex); | ||
}); | ||
} | ||
} catch (ex) { | ||
console.debug(`Error while running tests: ${testIds}\r\n${ex}\r\n\r\n`); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.