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: https://github.com/nodejs/node/pull/7753
Reviewed-By: Wyatt Preul <wpreul@gmail.com>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
cjihrig 2016-07-15 15:43:24 -04:00
parent 59741a9bee
commit 04b4d15b39
213 changed files with 1101 additions and 2882 deletions

View file

@ -1,6 +1,5 @@
'use strict';
require('../common');
var assert = require('assert');
const common = require('../common');
var http = require('http');
var body = 'hello world\n';
@ -13,27 +12,20 @@ function test(headers) {
server.close();
});
var gotEnd = false;
server.listen(0, function() {
server.listen(0, common.mustCall(function() {
var request = http.request({
port: this.address().port,
method: 'HEAD',
path: '/'
}, function(response) {
}, common.mustCall(function(response) {
console.error('response start');
response.on('end', function() {
response.on('end', common.mustCall(function() {
console.error('response end');
gotEnd = true;
});
}));
response.resume();
});
}));
request.end();
});
process.on('exit', function() {
assert.ok(gotEnd);
});
}));
}
test({