forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: fix flaky test-runner-cli-concurrency.js
This test was flaky on Windows when trying to clean up the tmp directory between tests. This commit updates the test to not delete anything between tests. Fixes: nodejs#50101
- Loading branch information
Showing
1 changed file
with
39 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,45 +1,57 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const { deepStrictEqual, strictEqual } = require('node:assert'); | ||
const { spawnSync } = require('node:child_process'); | ||
const { readdirSync, writeFileSync } = require('node:fs'); | ||
const { spawn } = require('node:child_process'); | ||
const { mkdirSync, readdirSync, writeFileSync } = require('node:fs'); | ||
const { join } = require('node:path'); | ||
const { beforeEach, test } = require('node:test'); | ||
const { test } = require('node:test'); | ||
const { setTimeout: sleep } = require('node:timers/promises'); | ||
const { isDeepStrictEqual } = require('node:util'); | ||
|
||
function createTestFile(name) { | ||
writeFileSync(join(tmpdir.path, name), ` | ||
const path = join(tmpdir.path, name); | ||
writeFileSync(path, ` | ||
const fs = require('node:fs'); | ||
const { join } = require('node:path'); | ||
fs.unlinkSync(__filename); | ||
fs.writeFileSync(join(process.cwd(), '${name}.out'), 'x'); | ||
setTimeout(() => {}, 1_000_000_000); | ||
`); | ||
return path; | ||
} | ||
|
||
beforeEach(() => { | ||
tmpdir.refresh(); | ||
createTestFile('test-1.js'); | ||
createTestFile('test-2.js'); | ||
}); | ||
let cnt = 0; | ||
function nextDir() { | ||
const path = join(tmpdir.path, String(cnt)); | ||
cnt++; | ||
mkdirSync(path); | ||
return path; | ||
} | ||
|
||
async function waitForFiles(dir, files) { | ||
while (!isDeepStrictEqual(readdirSync(dir), files)) { | ||
await sleep(common.platformTimeout(1000)); | ||
} | ||
} | ||
|
||
tmpdir.refresh(); | ||
const test1 = createTestFile('test-1.js'); | ||
const test2 = createTestFile('test-2.js'); | ||
|
||
test('concurrency of one', () => { | ||
const cp = spawnSync(process.execPath, ['--test', '--test-concurrency=1'], { | ||
cwd: tmpdir.path, | ||
timeout: common.platformTimeout(1000), | ||
}); | ||
test('concurrency of one', async () => { | ||
const cwd = nextDir(); | ||
const args = ['--test', '--test-concurrency=1', test1, test2]; | ||
const cp = spawn(process.execPath, args, { cwd }); | ||
|
||
strictEqual(cp.stderr.toString(), ''); | ||
strictEqual(cp.error.code, 'ETIMEDOUT'); | ||
deepStrictEqual(readdirSync(tmpdir.path), ['test-2.js']); | ||
await waitForFiles(cwd, ['test-1.js.out']); | ||
cp.kill(); | ||
}); | ||
|
||
test('concurrency of two', () => { | ||
const cp = spawnSync(process.execPath, ['--test', '--test-concurrency=2'], { | ||
cwd: tmpdir.path, | ||
timeout: common.platformTimeout(1000), | ||
}); | ||
test('concurrency of two', async () => { | ||
const cwd = nextDir(); | ||
const args = ['--test', '--test-concurrency=2', test1, test2]; | ||
const cp = spawn(process.execPath, args, { cwd }); | ||
|
||
strictEqual(cp.stderr.toString(), ''); | ||
strictEqual(cp.error.code, 'ETIMEDOUT'); | ||
deepStrictEqual(readdirSync(tmpdir.path), []); | ||
await waitForFiles(cwd, ['test-1.js.out', 'test-2.js.out']); | ||
cp.kill(); | ||
}); |