diff options
author | Rich Trott <rtrott@gmail.com> | 2020-03-14 16:16:50 -0700 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2020-04-22 18:03:30 +0200 |
commit | 8dbd7cf0e49e4836ee1c78d7510fe2fc57b3c058 (patch) | |
tree | 5d86644287f130fd860ff5b9ac551c0795b41c0e /test/pummel | |
parent | 46c751e7f1d2e0a16c0832a7e26a3e51475a215e (diff) | |
download | node-new-8dbd7cf0e49e4836ee1c78d7510fe2fc57b3c058.tar.gz |
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: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'test/pummel')
-rw-r--r-- | test/pummel/test-hash-seed.js | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/test/pummel/test-hash-seed.js b/test/pummel/test-hash-seed.js index 2b7f13593a..30edca32f6 100644 --- a/test/pummel/test-hash-seed.js +++ b/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); |