|
1 | 1 | 'use strict';
|
2 | 2 | const common = require('../common');
|
3 | 3 | 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'); |
7 | 6 | 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'); |
9 | 10 |
|
10 | 11 | function createTestFile(name) {
|
11 |
| - writeFileSync(join(tmpdir.path, name), ` |
| 12 | + const path = join(tmpdir.path, name); |
| 13 | + writeFileSync(path, ` |
12 | 14 | const fs = require('node:fs');
|
| 15 | + const { join } = require('node:path'); |
13 | 16 |
|
14 |
| - fs.unlinkSync(__filename); |
| 17 | + fs.writeFileSync(join(process.cwd(), '${name}.out'), 'x'); |
15 | 18 | setTimeout(() => {}, 1_000_000_000);
|
16 | 19 | `);
|
| 20 | + return path; |
17 | 21 | }
|
18 | 22 |
|
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'); |
24 | 40 |
|
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 }); |
30 | 45 |
|
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(); |
34 | 48 | });
|
35 | 49 |
|
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 }); |
41 | 54 |
|
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(); |
45 | 57 | });
|
0 commit comments