Skip to content

Commit 05fd160

Browse files
TrottMylesBorins
authored andcommitted
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>
1 parent 1476182 commit 05fd160

1 file changed

Lines changed: 16 additions & 21 deletions

File tree

‎test/pummel/test-hash-seed.js‎

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
'use strict';
22

33
// Check that spawn child doesn't create duplicated entries
4-
require('../common');
5-
constCountdown=require('../common/countdown');
6-
constREPETITIONS=2;
4+
constcommon=require('../common');
5+
constkRepetitions=2;
76
constassert=require('assert');
87
constfixtures=require('../common/fixtures');
9-
const{ spawn }=require('child_process');
8+
const{ promisify, debuglog }=require('util');
9+
constdebug=debuglog('test');
10+
11+
const{ execFile }=require('child_process');
12+
constexecFilePromise=promisify(execFile);
1013
consttargetScript=fixtures.path('guess-hash-seed.js');
11-
constseeds=[];
1214

13-
constrequiredCallback=()=>{
14-
console.log(`Seeds: ${seeds}`);
15+
constrequiredCallback=common.mustCall((results)=>{
16+
constseeds=results.map((val)=>val.stdout.trim());
17+
debug(`Seeds: ${seeds}`);
1518
assert.strictEqual(newSet(seeds).size,seeds.length);
16-
assert.strictEqual(seeds.length,REPETITIONS);
17-
};
18-
19-
constcountdown=newCountdown(REPETITIONS,requiredCallback);
19+
assert.strictEqual(seeds.length,kRepetitions);
20+
});
2021

21-
for(leti=0;i<REPETITIONS;++i){
22-
letresult='';
23-
constsubprocess=spawn(process.execPath,[targetScript]);
24-
subprocess.stdout.setEncoding('utf8');
25-
subprocess.stdout.on('data',(data)=>{result+=data;});
22+
constgenerateSeed=()=>execFilePromise(process.execPath,[targetScript]);
23+
constsubprocesses=[...newArray(kRepetitions)].map(generateSeed);
2624

27-
subprocess.on('exit',()=>{
28-
seeds.push(result.trim());
29-
countdown.dec();
30-
});
31-
}
25+
Promise.all(subprocesses)
26+
.then(requiredCallback);

0 commit comments

Comments
 (0)