Skip to content

Commit 19907c2

Browse files
cjihrigMylesBorins
authored andcommitted
test: use mustCall() for simple flow tracking
Many of the tests use variables to track when callback functions are invoked or events are emitted. These variables are then asserted on process exit. This commit replaces this pattern in straightforward cases with common.mustCall(). This makes the tests easier to reason about, leads to a net reduction in lines of code, and uncovered a few bugs in tests. This commit also replaces some callbacks that should never be called with common.fail(). PR-URL: #7753 Reviewed-By: Wyatt Preul <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 79d4986 commit 19907c2

File tree

212 files changed

+1095
-2859
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

212 files changed

+1095
-2859
lines changed
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
'use strict';
2-
varcommon=require('../../common');
2+
constcommon=require('../../common');
33
varassert=require('assert');
44
constbinding=require(`./build/${common.buildType}/binding`);
5-
varcalled=false;
65

7-
process.on('exit',function(){
8-
assert(called);
9-
});
10-
11-
binding(5,function(err,val){
6+
binding(5,common.mustCall(function(err,val){
127
assert.equal(null,err);
138
assert.equal(10,val);
14-
process.nextTick(function(){
15-
called=true;
16-
});
17-
});
9+
process.nextTick(common.mustCall(function(){}));
10+
}));

‎test/internet/test-http-dns-fail.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
* should trigger the error event after each attempt.
55
*/
66

7-
require('../common');
7+
constcommon=require('../common');
88
varassert=require('assert');
99
varhttp=require('http');
1010

11-
varresDespiteError=false;
1211
varhadError=0;
1312

1413
functionhttpreq(count){
@@ -19,9 +18,7 @@ function httpreq(count){
1918
port: 80,
2019
path: '/',
2120
method: 'GET'
22-
},function(res){
23-
resDespiteError=true;
24-
});
21+
},common.fail);
2522

2623
req.on('error',function(e){
2724
console.log(e.message);
@@ -37,6 +34,5 @@ httpreq(0);
3734

3835

3936
process.on('exit',function(){
40-
assert.equal(false,resDespiteError);
4137
assert.equal(2,hadError);
4238
});
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
varcommon=require('../common');
3-
varassert=require('assert');
43

54
if(!common.hasCrypto){
65
common.skip('missing crypto');
@@ -9,21 +8,11 @@ if (!common.hasCrypto){
98
varhttps=require('https');
109

1110
varhttp=require('http');
12-
vargotHttpsResp=false;
13-
vargotHttpResp=false;
1411

15-
process.on('exit',function(){
16-
assert(gotHttpsResp);
17-
assert(gotHttpResp);
18-
console.log('ok');
19-
});
20-
21-
https.get('https://www.google.com/',function(res){
22-
gotHttpsResp=true;
12+
https.get('https://www.google.com/',common.mustCall(function(res){
2313
res.resume();
24-
});
14+
}));
2515

26-
http.get('http://www.google.com/',function(res){
27-
gotHttpResp=true;
16+
http.get('http://www.google.com/',common.mustCall(function(res){
2817
res.resume();
29-
});
18+
}));

‎test/internet/test-net-connect-timeout.js‎

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
// https://groups.google.com/forum/#!topic/nodejs/UE0ZbfLt6t8
44
// https://groups.google.com/forum/#!topic/nodejs-dev/jR7-5UDqXkw
55

6-
require('../common');
6+
constcommon=require('../common');
77
varnet=require('net');
88
varassert=require('assert');
99

1010
varstart=newDate();
1111

12-
vargotTimeout=false;
13-
14-
vargotConnect=false;
15-
1612
varT=100;
1713

1814
// 192.0.2.1 is part of subnet assigned as "TEST-NET" in RFC 5737.
@@ -23,22 +19,11 @@ var socket = net.createConnection(9999, '192.0.2.1');
2319

2420
socket.setTimeout(T);
2521

26-
socket.on('timeout',function(){
22+
socket.on('timeout',common.mustCall(function(){
2723
console.error('timeout');
28-
gotTimeout=true;
2924
varnow=newDate();
3025
assert.ok(now-start<T+500);
3126
socket.destroy();
32-
});
33-
34-
socket.on('connect',function(){
35-
console.error('connect');
36-
gotConnect=true;
37-
socket.destroy();
38-
});
39-
27+
}));
4028

41-
process.on('exit',function(){
42-
assert.ok(gotTimeout);
43-
assert.ok(!gotConnect);
44-
});
29+
socket.on('connect',common.fail);
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
'use strict';
2-
require('../common');
3-
varassert=require('assert');
2+
constcommon=require('../common');
43
varnet=require('net');
54

6-
varclient,killed=false,ended=false;
5+
varclient;
76
varTIMEOUT=10*1000;
87

98
client=net.createConnection(53,'8.8.8.8',function(){
109
client.unref();
1110
});
1211

13-
client.on('close',function(){
14-
ended=true;
15-
});
16-
17-
setTimeout(function(){
18-
killed=true;
19-
client.end();
20-
},TIMEOUT).unref();
12+
client.on('close',common.fail);
2113

22-
process.on('exit',function(){
23-
assert.strictEqual(killed,false,'A client should have connected');
24-
assert.strictEqual(ended,false,'A client should stay connected');
25-
});
14+
setTimeout(common.fail,TIMEOUT).unref();

‎test/parallel/test-child-process-buffering.js‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
varcommon=require('../common');
33
varassert=require('assert');
44

5-
varpwd_called=false;
6-
varchildClosed=false;
7-
varchildExited=false;
8-
95
functionpwd(callback){
106
varoutput='';
117
varchild=common.spawnPwd();
@@ -16,17 +12,14 @@ function pwd(callback){
1612
output+=s;
1713
});
1814

19-
child.on('exit',function(c){
15+
child.on('exit',common.mustCall(function(c){
2016
console.log('exit: '+c);
2117
assert.equal(0,c);
22-
childExited=true;
23-
});
18+
}));
2419

25-
child.on('close',function(){
20+
child.on('close',common.mustCall(function(){
2621
callback(output);
27-
pwd_called=true;
28-
childClosed=true;
29-
});
22+
}));
3023
}
3124

3225

@@ -35,9 +28,3 @@ pwd(function(result){
3528
assert.equal(true,result.length>1);
3629
assert.equal('\n',result[result.length-1]);
3730
});
38-
39-
process.on('exit',function(){
40-
assert.equal(true,pwd_called);
41-
assert.equal(true,childExited);
42-
assert.equal(true,childClosed);
43-
});

‎test/parallel/test-child-process-disconnect.js‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,16 @@ if (process.argv[2] === 'child'){
4848
varchild=fork(process.argv[1],['child']);
4949

5050
varchildFlag=false;
51-
varchildSelfTerminate=false;
52-
varparentEmit=false;
5351
varparentFlag=false;
5452

5553
// when calling .disconnect the event should emit
5654
// and the disconnected flag should be true.
57-
child.on('disconnect',function(){
58-
parentEmit=true;
55+
child.on('disconnect',common.mustCall(function(){
5956
parentFlag=child.connected;
60-
});
57+
}));
6158

6259
// the process should also self terminate without using signals
63-
child.on('exit',function(){
64-
childSelfTerminate=true;
65-
});
60+
child.on('exit',common.mustCall(function(){}));
6661

6762
// when child is listening
6863
child.on('message',function(obj){
@@ -91,8 +86,5 @@ if (process.argv[2] === 'child'){
9186
process.on('exit',function(){
9287
assert.equal(childFlag,false);
9388
assert.equal(parentFlag,false);
94-
95-
assert.ok(childSelfTerminate);
96-
assert.ok(parentEmit);
9789
});
9890
}
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
'use strict';
2-
require('../common');
2+
constcommon=require('../common');
33
varassert=require('assert');
44
varexec=require('child_process').exec;
55
varos=require('os');
6-
7-
varsuccess_count=0;
8-
96
varstr='hello';
107

118
// default encoding
12-
exec('echo '+str,function(err,stdout,stderr){
9+
exec('echo '+str,common.mustCall(function(err,stdout,stderr){
1310
assert.ok('string',typeofstdout,'Expected stdout to be a string');
1411
assert.ok('string',typeofstderr,'Expected stderr to be a string');
1512
assert.equal(str+os.EOL,stdout);
16-
17-
success_count++;
18-
});
13+
}));
1914

2015
// no encoding (Buffers expected)
2116
exec('echo '+str,{
2217
encoding: null
23-
},function(err,stdout,stderr){
18+
},common.mustCall(function(err,stdout,stderr){
2419
assert.ok(stdoutinstanceofBuffer,'Expected stdout to be a Buffer');
2520
assert.ok(stderrinstanceofBuffer,'Expected stderr to be a Buffer');
2621
assert.equal(str+os.EOL,stdout.toString());
27-
28-
success_count++;
29-
});
30-
31-
process.on('exit',function(){
32-
assert.equal(2,success_count);
33-
});
22+
}));

‎test/parallel/test-child-process-exec-cwd.js‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ const common = require('../common');
33
varassert=require('assert');
44
varexec=require('child_process').exec;
55

6-
varsuccess_count=0;
7-
varerror_count=0;
8-
96
varpwdcommand,dir;
107

118
if(common.isWindows){
@@ -16,21 +13,7 @@ if (common.isWindows){
1613
dir='/dev';
1714
}
1815

19-
exec(pwdcommand,{cwd: dir},function(err,stdout,stderr){
20-
if(err){
21-
error_count++;
22-
console.log('error!: '+err.code);
23-
console.log('stdout: '+JSON.stringify(stdout));
24-
console.log('stderr: '+JSON.stringify(stderr));
25-
assert.equal(false,err.killed);
26-
}else{
27-
success_count++;
28-
console.log(stdout);
29-
assert.ok(stdout.indexOf(dir)==0);
30-
}
31-
});
32-
33-
process.on('exit',function(){
34-
assert.equal(1,success_count);
35-
assert.equal(0,error_count);
36-
});
16+
exec(pwdcommand,{cwd: dir},common.mustCall(function(err,stdout,stderr){
17+
assert.ifError(err);
18+
assert.ok(stdout.indexOf(dir)==0);
19+
}));

‎test/parallel/test-child-process-exec-error.js‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@ var assert = require('assert');
44
varchild_process=require('child_process');
55

66
functiontest(fun,code){
7-
varerrors=0;
8-
9-
fun('does-not-exist',function(err){
7+
fun('does-not-exist',common.mustCall(function(err){
108
assert.equal(err.code,code);
119
assert(/does\-not\-exist/.test(err.cmd));
12-
errors++;
13-
});
14-
15-
process.on('exit',function(){
16-
assert.equal(errors,1);
17-
});
10+
}));
1811
}
1912

2013
if(common.isWindows){

0 commit comments

Comments
(0)