★ wanayoo — archive 1999 https://github.com/nodejs/node/commit/05fd16048cNouvelle recherche | Portail wanayoo
Skip to content
Permalink
Browse files
test: use Promise.all() in test-hash-seed
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 <targos@protonmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
  • Loading branch information
Trott authored and MylesBorins committed Mar 24, 2020
1 parent 1476182 commit 05fd16048c622bad0e760151f5d0043970df179b
Showing with 16 additions and 21 deletions.
  1. +16 −21 test/pummel/test-hash-seed.js
@@ -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.