Skip to content

Commit

Permalink
test: fix flaky test-runner-cli-concurrency.js
Browse files Browse the repository at this point in the history
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
cjihrig committed Oct 10, 2023
1 parent 78a1570 commit cb1688a
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions test/parallel/test-runner-cli-concurrency.js
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();
});

0 comments on commit cb1688a

Please sign in to comment.