mirror of https://github.com/lukechilds/node.git
Browse Source
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>v7.x
213 changed files with 1101 additions and 2882 deletions
@ -1,17 +1,10 @@ |
|||
'use strict'; |
|||
require('../../common'); |
|||
const common = require('../../common'); |
|||
var assert = require('assert'); |
|||
var binding = require('./build/Release/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() {})); |
|||
})); |
|||
|
@ -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(); |
|||
|
@ -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); |
|||
}); |
|||
})); |
|||
|
@ -1,20 +1,14 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
var assert = require('assert'); |
|||
var ch = require('child_process'); |
|||
|
|||
var SIZE = 100000; |
|||
var childGone = false; |
|||
|
|||
var cp = ch.spawn('python', ['-c', 'print ' + SIZE + ' * "C"'], { |
|||
stdio: 'inherit' |
|||
}); |
|||
|
|||
cp.on('exit', function(code) { |
|||
childGone = true; |
|||
cp.on('exit', common.mustCall(function(code) { |
|||
assert.equal(0, code); |
|||
}); |
|||
|
|||
process.on('exit', function() { |
|||
assert.ok(childGone); |
|||
}); |
|||
})); |
|||
|
@ -1,39 +1,26 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
var assert = require('assert'); |
|||
var cluster = require('cluster'); |
|||
|
|||
assert(cluster.isMaster); |
|||
|
|||
var assertsRun = 0; |
|||
|
|||
function emitAndCatch(next) { |
|||
cluster.once('setup', function(settings) { |
|||
cluster.once('setup', common.mustCall(function(settings) { |
|||
assert.strictEqual(settings.exec, 'new-exec'); |
|||
console.log('ok "setup" emitted with options set'); |
|||
assertsRun += 1; |
|||
setImmediate(next); |
|||
}); |
|||
})); |
|||
cluster.setupMaster({ exec: 'new-exec' }); |
|||
} |
|||
|
|||
function emitAndCatch2(next) { |
|||
cluster.once('setup', function(settings) { |
|||
cluster.once('setup', common.mustCall(function(settings) { |
|||
assert('exec' in settings); |
|||
console.log('ok "setup" emitted without options set'); |
|||
assertsRun += 1; |
|||
setImmediate(next); |
|||
}); |
|||
})); |
|||
cluster.setupMaster(); |
|||
} |
|||
|
|||
process.on('exit', function() { |
|||
assert.strictEqual(assertsRun, 2); |
|||
console.log('ok correct number of assertions'); |
|||
}); |
|||
|
|||
emitAndCatch(function() { |
|||
emitAndCatch2(function() { |
|||
console.log('ok emitted and caught'); |
|||
}); |
|||
}); |
|||
emitAndCatch(common.mustCall(function() { |
|||
emitAndCatch2(common.mustCall(function() {})); |
|||
})); |
|||
|
@ -1,25 +1,17 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
var assert = require('assert'); |
|||
var cluster = require('cluster'); |
|||
|
|||
if (!cluster.isMaster) { |
|||
process.exit(42); |
|||
} else { |
|||
var seenExit = 0; |
|||
var seenDeath = 0; |
|||
var worker = cluster.fork(); |
|||
worker.on('exit', function(exitCode, signalCode) { |
|||
worker.on('exit', common.mustCall(function(exitCode, signalCode) { |
|||
assert.equal(exitCode, 42); |
|||
assert.equal(signalCode, null); |
|||
seenExit++; |
|||
}); |
|||
cluster.on('exit', function(worker_) { |
|||
})); |
|||
cluster.on('exit', common.mustCall(function(worker_) { |
|||
assert.equal(worker_, worker); |
|||
seenDeath++; |
|||
}); |
|||
process.on('exit', function() { |
|||
assert.equal(seenExit, 1); |
|||
assert.equal(seenDeath, 1); |
|||
}); |
|||
})); |
|||
} |
|||
|
@ -1,21 +1,14 @@ |
|||
'use strict'; |
|||
var assert = require('assert'); |
|||
var common = require('../common'); |
|||
var dgram = require('dgram'); |
|||
|
|||
var buf = Buffer.alloc(1024, 42); |
|||
|
|||
var socket = dgram.createSocket('udp4'); |
|||
var closeEvents = 0; |
|||
|
|||
socket.send(buf, 0, buf.length, common.PORT, 'localhost'); |
|||
|
|||
// if close callback is not function, ignore the argument.
|
|||
socket.close('bad argument'); |
|||
|
|||
socket.on('close', function() { |
|||
++closeEvents; |
|||
}); |
|||
|
|||
process.on('exit', function() { |
|||
assert.equal(closeEvents, 1); |
|||
}); |
|||
socket.on('close', common.mustCall(function() {})); |
|||
|
@ -1,19 +1,9 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
var assert = require('assert'); |
|||
|
|||
const common = require('../common'); |
|||
var dgram = require('dgram'); |
|||
var closed = false; |
|||
|
|||
var s = dgram.createSocket('udp4'); |
|||
s.bind(); |
|||
s.unref(); |
|||
|
|||
setTimeout(function() { |
|||
closed = true; |
|||
s.close(); |
|||
}, 1000).unref(); |
|||
|
|||
process.on('exit', function() { |
|||
assert.strictEqual(closed, false, 'Unrefd socket should not hold loop open'); |
|||
}); |
|||
setTimeout(common.fail, 1000).unref(); |
|||
|
@ -1,27 +1,13 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
var util = require('util'); |
|||
var assert = require('assert'); |
|||
var exec = require('child_process').exec; |
|||
|
|||
var success_count = 0; |
|||
var error_count = 0; |
|||
|
|||
var cmd = ['"' + process.execPath + '"', '-e', '"console.error(process.argv)"', |
|||
'foo', 'bar'].join(' '); |
|||
var expected = util.format([process.execPath, 'foo', 'bar']) + '\n'; |
|||
exec(cmd, function(err, stdout, stderr) { |
|||
if (err) { |
|||
console.log(err.toString()); |
|||
++error_count; |
|||
return; |
|||
} |
|||
exec(cmd, common.mustCall(function(err, stdout, stderr) { |
|||
assert.ifError(err); |
|||
assert.equal(stderr, expected); |
|||
++success_count; |
|||
}); |
|||
|
|||
process.on('exit', function() { |
|||
assert.equal(1, success_count); |
|||
assert.equal(0, error_count); |
|||
}); |
|||
|
|||
})); |
|||
|
@ -1,26 +1,16 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
var assert = require('assert'); |
|||
const common = require('../common'); |
|||
var http = require('http'); |
|||
|
|||
var request = 0; |
|||
var response = 0; |
|||
process.on('exit', function() { |
|||
assert.equal(request, 1, 'http server "request" callback was not called'); |
|||
assert.equal(response, 1, 'http request "response" callback was not called'); |
|||
}); |
|||
|
|||
var server = http.createServer(function(req, res) { |
|||
request++; |
|||
var server = http.createServer(common.mustCall(function(req, res) { |
|||
res.end(); |
|||
}).listen(0, function() { |
|||
})).listen(0, common.mustCall(function() { |
|||
var options = { |
|||
agent: null, |
|||
port: this.address().port |
|||
}; |
|||
http.get(options, function(res) { |
|||
response++; |
|||
http.get(options, common.mustCall(function(res) { |
|||
res.resume(); |
|||
server.close(); |
|||
}); |
|||
}); |
|||
})); |
|||
})); |
|||
|
@ -1,26 +1,15 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
var assert = require('assert'); |
|||
var http = require('http'); |
|||
|
|||
var gotError = false; |
|||
var server1 = http.createServer(common.fail); |
|||
server1.listen(0, '127.0.0.1', common.mustCall(function() { |
|||
var server2 = http.createServer(common.fail); |
|||
server2.listen(this.address().port, '127.0.0.1', common.fail); |
|||
|
|||
process.on('exit', function() { |
|||
assert(gotError); |
|||
}); |
|||
|
|||
function dontCall() { |
|||
assert(false); |
|||
} |
|||
|
|||
var server1 = http.createServer(dontCall); |
|||
server1.listen(0, '127.0.0.1', function() { |
|||
var server2 = http.createServer(dontCall); |
|||
server2.listen(this.address().port, '127.0.0.1', dontCall); |
|||
|
|||
server2.on('error', function(e) { |
|||
server2.on('error', common.mustCall(function(e) { |
|||
assert.equal(e.code, 'EADDRINUSE'); |
|||
server1.close(); |
|||
gotError = true; |
|||
}); |
|||
}); |
|||
})); |
|||
})); |
|||
|
@ -1,29 +1,20 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
var assert = require('assert'); |
|||
const common = require('../common'); |
|||
var http = require('http'); |
|||
var server = http.createServer(function(req, res) { |
|||
res.end(); |
|||
}); |
|||
var count = 0; |
|||
server.listen(0, function() { |
|||
|
|||
server.listen(0, common.mustCall(function() { |
|||
var req = http.request({ |
|||
port: this.address().port |
|||
}, function() { |
|||
assert(false, 'should not receive data'); |
|||
}); |
|||
}, common.fail); |
|||
|
|||
req.on('abort', function() { |
|||
// should only be emitted once
|
|||
count++; |
|||
req.on('abort', common.mustCall(function() { |
|||
server.close(); |
|||
}); |
|||
})); |
|||
|
|||
req.end(); |
|||
req.abort(); |
|||
req.abort(); |
|||
}); |
|||
|
|||
process.on('exit', function() { |
|||
assert.equal(count, 1); |
|||
}); |
|||
})); |
|||
|
@ -1,52 +1,44 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
var assert = require('assert'); |
|||
var http = require('http'); |
|||
|
|||
var N = 1024; |
|||
var bytesReceived = 0; |
|||
var server_req_complete = false; |
|||
var client_res_complete = false; |
|||
|
|||
var server = http.createServer(function(req, res) { |
|||
var server = http.createServer(common.mustCall(function(req, res) { |
|||
assert.equal('POST', req.method); |
|||
|
|||
var bytesReceived = 0; |
|||
|
|||
req.on('data', function(chunk) { |
|||
bytesReceived += chunk.length; |
|||
}); |
|||
|
|||
req.on('end', function() { |
|||
server_req_complete = true; |
|||
req.on('end', common.mustCall(function() { |
|||
assert.strictEqual(N, bytesReceived); |
|||
console.log('request complete from server'); |
|||
res.writeHead(200, {'Content-Type': 'text/plain'}); |
|||
res.write('hello\n'); |
|||
res.end(); |
|||
}); |
|||
}); |
|||
})); |
|||
})); |
|||
server.listen(0); |
|||
|
|||
server.on('listening', function() { |
|||
server.on('listening', common.mustCall(function() { |
|||
var req = http.request({ |
|||
port: this.address().port, |
|||
method: 'POST', |
|||
path: '/' |
|||
}, function(res) { |
|||
}, common.mustCall(function(res) { |
|||
res.setEncoding('utf8'); |
|||
res.on('data', function(chunk) { |
|||
console.log(chunk); |
|||
}); |
|||
res.on('end', function() { |
|||
client_res_complete = true; |
|||
res.on('end', common.mustCall(function() { |
|||
server.close(); |
|||
}); |
|||
}); |
|||
})); |
|||
})); |
|||
|
|||
req.write(Buffer.allocUnsafe(N)); |
|||
req.end(); |
|||
}); |
|||
|
|||
process.on('exit', function() { |
|||
assert.equal(N, bytesReceived); |
|||
assert.equal(true, server_req_complete); |
|||
assert.equal(true, client_res_complete); |
|||
}); |
|||
})); |
|||
|
@ -1,55 +1,46 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
var assert = require('assert'); |
|||
var http = require('http'); |
|||
|
|||
var sent_body = ''; |
|||
var server_req_complete = false; |
|||
var client_res_complete = false; |
|||
|
|||
var server = http.createServer(function(req, res) { |
|||
var server = http.createServer(common.mustCall(function(req, res) { |
|||
assert.equal('POST', req.method); |
|||
req.setEncoding('utf8'); |
|||
|
|||
var sent_body = ''; |
|||
|
|||
req.on('data', function(chunk) { |
|||
console.log('server got: ' + JSON.stringify(chunk)); |
|||
sent_body += chunk; |
|||
}); |
|||
|
|||
req.on('end', function() { |
|||
server_req_complete = true; |
|||
req.on('end', common.mustCall(function() { |
|||
assert.strictEqual('1\n2\n3\n', sent_body); |
|||
console.log('request complete from server'); |
|||
res.writeHead(200, {'Content-Type': 'text/plain'}); |
|||
res.write('hello\n'); |
|||
res.end(); |
|||
}); |
|||
}); |
|||
})); |
|||
})); |
|||
server.listen(0); |
|||
|
|||
server.on('listening', function() { |
|||
server.on('listening', common.mustCall(function() { |
|||
var req = http.request({ |
|||
port: this.address().port, |
|||
method: 'POST', |
|||
path: '/' |
|||
}, function(res) { |
|||
}, common.mustCall(function(res) { |
|||
res.setEncoding('utf8'); |
|||
res.on('data', function(chunk) { |
|||
console.log(chunk); |
|||
}); |
|||
res.on('end', function() { |
|||
client_res_complete = true; |
|||
res.on('end', common.mustCall(function() { |
|||
server.close(); |
|||
}); |
|||
}); |
|||
})); |
|||
})); |
|||
|
|||
req.write('1\n'); |
|||
req.write('2\n'); |
|||
req.write('3\n'); |
|||
req.end(); |
|||
}); |
|||
|
|||
process.on('exit', function() { |
|||
assert.equal('1\n2\n3\n', sent_body); |
|||
assert.equal(true, server_req_complete); |
|||
assert.equal(true, client_res_complete); |
|||
}); |
|||
})); |
|||
|
@ -1,26 +1,23 @@ |
|||
'use strict'; |
|||
require('../common'); |
|||
const common = require('../common'); |
|||
var assert = require('assert'); |
|||
var net = require('net'); |
|||
var http = require('http'); |
|||
|
|||
var body = ''; |
|||
|
|||
var server = net.createServer(function(socket) { |
|||
// Neither Content-Length nor Connection
|
|||
socket.end('HTTP/1.1 200 ok\r\n\r\nHello'); |
|||
}).listen(0, function() { |
|||
http.get({port: this.address().port}, function(res) { |
|||
}).listen(0, common.mustCall(function() { |
|||
http.get({port: this.address().port}, common.mustCall(function(res) { |
|||
var body = ''; |
|||
|
|||
res.setEncoding('utf8'); |
|||
res.on('data', function(chunk) { |
|||
body += chunk; |
|||
}); |
|||
res.on('end', function() { |
|||
res.on('end', common.mustCall(function() { |
|||
assert.strictEqual(body, 'Hello'); |
|||
server.close(); |
|||
}); |
|||
}); |
|||
}); |
|||
|
|||
process.on('exit', function() { |
|||
assert.equal(body, 'Hello'); |
|||
}); |
|||
})); |
|||
})); |
|||
})); |
|||
|
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue