Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cb1688a

Browse files
committedOct 10, 2023
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: #50101
1 parent 78a1570 commit cb1688a

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed
 
+39-27
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
11
'use strict';
22
const common = require('../common');
33
const tmpdir = require('../common/tmpdir');
4-
const { deepStrictEqual, strictEqual } = require('node:assert');
5-
const { spawnSync } = require('node:child_process');
6-
const { readdirSync, writeFileSync } = require('node:fs');
4+
const { spawn } = require('node:child_process');
5+
const { mkdirSync, readdirSync, writeFileSync } = require('node:fs');
76
const { join } = require('node:path');
8-
const { beforeEach, test } = require('node:test');
7+
const { test } = require('node:test');
8+
const { setTimeout: sleep } = require('node:timers/promises');
9+
const { isDeepStrictEqual } = require('node:util');
910

1011
function createTestFile(name) {
11-
writeFileSync(join(tmpdir.path, name), `
12+
const path = join(tmpdir.path, name);
13+
writeFileSync(path, `
1214
const fs = require('node:fs');
15+
const { join } = require('node:path');
1316
14-
fs.unlinkSync(__filename);
17+
fs.writeFileSync(join(process.cwd(), '${name}.out'), 'x');
1518
setTimeout(() => {}, 1_000_000_000);
1619
`);
20+
return path;
1721
}
1822

19-
beforeEach(() => {
20-
tmpdir.refresh();
21-
createTestFile('test-1.js');
22-
createTestFile('test-2.js');
23-
});
23+
let cnt = 0;
24+
function nextDir() {
25+
const path = join(tmpdir.path, String(cnt));
26+
cnt++;
27+
mkdirSync(path);
28+
return path;
29+
}
30+
31+
async function waitForFiles(dir, files) {
32+
while (!isDeepStrictEqual(readdirSync(dir), files)) {
33+
await sleep(common.platformTimeout(1000));
34+
}
35+
}
36+
37+
tmpdir.refresh();
38+
const test1 = createTestFile('test-1.js');
39+
const test2 = createTestFile('test-2.js');
2440

25-
test('concurrency of one', () => {
26-
const cp = spawnSync(process.execPath, ['--test', '--test-concurrency=1'], {
27-
cwd: tmpdir.path,
28-
timeout: common.platformTimeout(1000),
29-
});
41+
test('concurrency of one', async () => {
42+
const cwd = nextDir();
43+
const args = ['--test', '--test-concurrency=1', test1, test2];
44+
const cp = spawn(process.execPath, args, { cwd });
3045

31-
strictEqual(cp.stderr.toString(), '');
32-
strictEqual(cp.error.code, 'ETIMEDOUT');
33-
deepStrictEqual(readdirSync(tmpdir.path), ['test-2.js']);
46+
await waitForFiles(cwd, ['test-1.js.out']);
47+
cp.kill();
3448
});
3549

36-
test('concurrency of two', () => {
37-
const cp = spawnSync(process.execPath, ['--test', '--test-concurrency=2'], {
38-
cwd: tmpdir.path,
39-
timeout: common.platformTimeout(1000),
40-
});
50+
test('concurrency of two', async () => {
51+
const cwd = nextDir();
52+
const args = ['--test', '--test-concurrency=2', test1, test2];
53+
const cp = spawn(process.execPath, args, { cwd });
4154

42-
strictEqual(cp.stderr.toString(), '');
43-
strictEqual(cp.error.code, 'ETIMEDOUT');
44-
deepStrictEqual(readdirSync(tmpdir.path), []);
55+
await waitForFiles(cwd, ['test-1.js.out', 'test-2.js.out']);
56+
cp.kill();
4557
});

0 commit comments

Comments
 (0)
Please sign in to comment.