Skip to content

Commit

Permalink
test: use Promise.all() in test-hash-seed
Browse files Browse the repository at this point in the history
We have several tests where a number of asynchronous processes need to
finish before some checks happen. These are done in a number of ways,
including (as here) using our Countdown testing module. I think
Promise.all() may be the idiomatic and ergonomic way to go for a lot of
these tests. Using this one to get feedback on the idea.

PR-URL: #32273
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
  • Loading branch information
Trott authored and MylesBorins committed Mar 24, 2020
1 parent 1476182 commit 05fd160
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions test/pummel/test-hash-seed.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
'use strict';

// Check that spawn child doesn't create duplicated entries
require('../common');
const Countdown = require('../common/countdown');
const REPETITIONS = 2;
const common = require('../common');
const kRepetitions = 2;
const assert = require('assert');
const fixtures = require('../common/fixtures');
const { spawn } = require('child_process');
const { promisify, debuglog } = require('util');
const debug = debuglog('test');

const { execFile } = require('child_process');
const execFilePromise = promisify(execFile);
const targetScript = fixtures.path('guess-hash-seed.js');
const seeds = [];

const requiredCallback = () => {
console.log(`Seeds: ${seeds}`);
const requiredCallback = common.mustCall((results) => {
const seeds = results.map((val) => val.stdout.trim());
debug(`Seeds: ${seeds}`);
assert.strictEqual(new Set(seeds).size, seeds.length);
assert.strictEqual(seeds.length, REPETITIONS);
};

const countdown = new Countdown(REPETITIONS, requiredCallback);
assert.strictEqual(seeds.length, kRepetitions);
});

for (let i = 0; i < REPETITIONS; ++i) {
let result = '';
const subprocess = spawn(process.execPath, [targetScript]);
subprocess.stdout.setEncoding('utf8');
subprocess.stdout.on('data', (data) => { result += data; });
const generateSeed = () => execFilePromise(process.execPath, [targetScript]);
const subprocesses = [...new Array(kRepetitions)].map(generateSeed);

subprocess.on('exit', () => {
seeds.push(result.trim());
countdown.dec();
});
}
Promise.all(subprocesses)
.then(requiredCallback);

0 comments on commit 05fd160

Please sign in to comment.