|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | // Check that spawn child doesn't create duplicated entries
|
4 |
| -require('../common'); |
5 |
| -const Countdown = require('../common/countdown'); |
6 |
| -const REPETITIONS = 2; |
| 4 | +const common = require('../common'); |
| 5 | +const kRepetitions = 2; |
7 | 6 | const assert = require('assert');
|
8 | 7 | const fixtures = require('../common/fixtures');
|
9 |
| -const { spawn } = require('child_process'); |
| 8 | +const { promisify, debuglog } = require('util'); |
| 9 | +const debug = debuglog('test'); |
| 10 | + |
| 11 | +const { execFile } = require('child_process'); |
| 12 | +const execFilePromise = promisify(execFile); |
10 | 13 | const targetScript = fixtures.path('guess-hash-seed.js');
|
11 |
| -const seeds = []; |
12 | 14 |
|
13 |
| -const requiredCallback = () => { |
14 |
| - console.log(`Seeds: ${seeds}`); |
| 15 | +const requiredCallback = common.mustCall((results) => { |
| 16 | + const seeds = results.map((val) => val.stdout.trim()); |
| 17 | + debug(`Seeds: ${seeds}`); |
15 | 18 | assert.strictEqual(new Set(seeds).size, seeds.length);
|
16 |
| - assert.strictEqual(seeds.length, REPETITIONS); |
17 |
| -}; |
18 |
| - |
19 |
| -const countdown = new Countdown(REPETITIONS, requiredCallback); |
| 19 | + assert.strictEqual(seeds.length, kRepetitions); |
| 20 | +}); |
20 | 21 |
|
21 |
| -for (let i = 0; i < REPETITIONS; ++i) { |
22 |
| - let result = ''; |
23 |
| - const subprocess = spawn(process.execPath, [targetScript]); |
24 |
| - subprocess.stdout.setEncoding('utf8'); |
25 |
| - subprocess.stdout.on('data', (data) => { result += data; }); |
| 22 | +const generateSeed = () => execFilePromise(process.execPath, [targetScript]); |
| 23 | +const subprocesses = [...new Array(kRepetitions)].map(generateSeed); |
26 | 24 |
|
27 |
| - subprocess.on('exit', () => { |
28 |
| - seeds.push(result.trim()); |
29 |
| - countdown.dec(); |
30 |
| - }); |
31 |
| -} |
| 25 | +Promise.all(subprocesses) |
| 26 | + .then(requiredCallback); |
0 commit comments