forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Reliably end test tasks in Azure Pipelines #5410
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Reliably end test tasks in Azure Pipelines. |
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,103 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| import { ChildProcess, spawn, SpawnOptions } from 'child_process'; | ||
| import * as fs from 'fs-extra'; | ||
| import { createServer, Server } from 'net'; | ||
| import * as path from 'path'; | ||
| import { EXTENSION_ROOT_DIR } from '../client/constants'; | ||
| import { noop, sleep } from './core'; | ||
|
|
||
| // tslint:disable:no-console | ||
|
|
||
| /* | ||
| This is a simple work around for tests tasks not completing on Azure Pipelines. | ||
| What's been happening is, the tests run however for some readon the Node propcess (VS Code) does not exit. | ||
| Here's what we've tried thus far: | ||
| * Dispose all timers | ||
| * Close all open streams/sockets. | ||
| * Use `process.exit` and use the VSC commands to close itself. | ||
|
|
||
| Final solution: | ||
| * Start a node.js procecss | ||
| * This process will start a socket server | ||
| * This procecss will start the tests in a separate procecss (spawn) | ||
| * When the tests have completed, | ||
| * Send a message to the socket server with a flag (true/false whether tests passed/failed) | ||
| * Socket server (main procecss) will receive the test status flag. | ||
| * This will kill the spawned process | ||
| * This main process will kill itself with exit code 0 if tests pass succesfully, else 1. | ||
| */ | ||
|
|
||
| const testFile = process.argv[2]; | ||
| const portFile = path.join(EXTENSION_ROOT_DIR, 'port.txt'); | ||
|
|
||
| let proc: ChildProcess | undefined; | ||
| let server: Server | undefined; | ||
|
|
||
| async function deletePortFile() { | ||
| try { | ||
| if (await fs.pathExists(portFile)) { | ||
| await fs.unlink(portFile); | ||
| } | ||
| } catch { | ||
| noop(); | ||
| } | ||
| } | ||
| async function end(exitCode: number) { | ||
| if (exitCode === 0) { | ||
| console.log('Exiting without errors'); | ||
| } else { | ||
| console.error('Exiting with test failures'); | ||
| } | ||
| if (proc) { | ||
| try { | ||
| const procToKill = proc; | ||
| proc = undefined; | ||
| console.log('Killing VSC'); | ||
| await deletePortFile(); | ||
| // Wait for the std buffers to get flushed before killing. | ||
| await sleep(5_000); | ||
| procToKill.kill(); | ||
| } catch { | ||
| noop(); | ||
| } | ||
| } | ||
| if (server) { | ||
| server.close(); | ||
DonJayamanne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| // Exit with required code. | ||
| process.exit(exitCode); | ||
| } | ||
|
|
||
| async function startSocketServer() { | ||
| return new Promise(resolve => { | ||
| server = createServer(socket => { | ||
| socket.on('data', buffer => { | ||
| const data = buffer.toString('utf8'); | ||
| console.log(`Exit code from Tests is ${data}`); | ||
| const code = parseInt(data.substring(0, 1), 10); | ||
| end(code).catch(noop); | ||
| }); | ||
| }); | ||
|
|
||
| server.listen({ host: '127.0.0.1', port: 0 }, async () => { | ||
| const port = server!.address().port; | ||
| console.log(`Test server listening on port ${port}`); | ||
| await deletePortFile(); | ||
| await fs.writeFile(portFile, port.toString()); | ||
| resolve(); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function start() { | ||
| await startSocketServer(); | ||
| const options: SpawnOptions = { cwd: process.cwd(), env: process.env, detached: true, stdio: 'inherit' }; | ||
| proc = spawn(process.execPath, [testFile], options); | ||
| proc.once('close', end); | ||
| } | ||
|
|
||
| start().catch(ex => console.error(ex)); | ||
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.