-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
79d4986
commit 19907c2
Showing
212 changed files
with
1,095 additions
and
2,859 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
'use strict'; | ||
var common = require('../../common'); | ||
const common = require('../../common'); | ||
var assert = require('assert'); | ||
const binding = require(`./build/${common.buildType}/binding`); | ||
var called = false; | ||
|
||
process.on('exit', function() { | ||
assert(called); | ||
}); | ||
|
||
binding(5, function(err, val) { | ||
binding(5, common.mustCall(function(err, val) { | ||
assert.equal(null, err); | ||
assert.equal(10, val); | ||
process.nextTick(function() { | ||
called = true; | ||
}); | ||
}); | ||
process.nextTick(common.mustCall(function() {})); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,14 @@ | ||
'use strict'; | ||
require('../common'); | ||
var assert = require('assert'); | ||
const common = require('../common'); | ||
var net = require('net'); | ||
|
||
var client, killed = false, ended = false; | ||
var client; | ||
var TIMEOUT = 10 * 1000; | ||
|
||
client = net.createConnection(53, '8.8.8.8', function() { | ||
client.unref(); | ||
}); | ||
|
||
client.on('close', function() { | ||
ended = true; | ||
}); | ||
|
||
setTimeout(function() { | ||
killed = true; | ||
client.end(); | ||
}, TIMEOUT).unref(); | ||
client.on('close', common.fail); | ||
|
||
process.on('exit', function() { | ||
assert.strictEqual(killed, false, 'A client should have connected'); | ||
assert.strictEqual(ended, false, 'A client should stay connected'); | ||
}); | ||
setTimeout(common.fail, TIMEOUT).unref(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,22 @@ | ||
'use strict'; | ||
require('../common'); | ||
const common = require('../common'); | ||
var assert = require('assert'); | ||
var exec = require('child_process').exec; | ||
var os = require('os'); | ||
|
||
var success_count = 0; | ||
|
||
var str = 'hello'; | ||
|
||
// default encoding | ||
exec('echo ' + str, function(err, stdout, stderr) { | ||
exec('echo ' + str, common.mustCall(function(err, stdout, stderr) { | ||
assert.ok('string', typeof stdout, 'Expected stdout to be a string'); | ||
assert.ok('string', typeof stderr, 'Expected stderr to be a string'); | ||
assert.equal(str + os.EOL, stdout); | ||
|
||
success_count++; | ||
}); | ||
})); | ||
|
||
// no encoding (Buffers expected) | ||
exec('echo ' + str, { | ||
encoding: null | ||
}, function(err, stdout, stderr) { | ||
}, common.mustCall(function(err, stdout, stderr) { | ||
assert.ok(stdout instanceof Buffer, 'Expected stdout to be a Buffer'); | ||
assert.ok(stderr instanceof Buffer, 'Expected stderr to be a Buffer'); | ||
assert.equal(str + os.EOL, stdout.toString()); | ||
|
||
success_count++; | ||
}); | ||
|
||
process.on('exit', function() { | ||
assert.equal(2, success_count); | ||
}); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.