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,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() {}));
}));

View File

@ -4,11 +4,10 @@
* should trigger the error event after each attempt.
*/
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var resDespiteError = false;
var hadError = 0;
function httpreq(count) {
@ -19,9 +18,7 @@ function httpreq(count) {
port: 80,
path: '/',
method: 'GET'
}, function(res) {
resDespiteError = true;
});
}, common.fail);
req.on('error', function(e) {
console.log(e.message);
@ -37,6 +34,5 @@ httpreq(0);
process.on('exit', function() {
assert.equal(false, resDespiteError);
assert.equal(2, hadError);
});

View File

@ -1,6 +1,5 @@
'use strict';
var common = require('../common');
var assert = require('assert');
if (!common.hasCrypto) {
common.skip('missing crypto');
@ -9,21 +8,11 @@ if (!common.hasCrypto) {
var https = require('https');
var http = require('http');
var gotHttpsResp = false;
var gotHttpResp = false;
process.on('exit', function() {
assert(gotHttpsResp);
assert(gotHttpResp);
console.log('ok');
});
https.get('https://www.google.com/', function(res) {
gotHttpsResp = true;
https.get('https://www.google.com/', common.mustCall(function(res) {
res.resume();
});
}));
http.get('http://www.google.com/', function(res) {
gotHttpResp = true;
http.get('http://www.google.com/', common.mustCall(function(res) {
res.resume();
});
}));

View File

@ -3,16 +3,12 @@
// https://groups.google.com/forum/#!topic/nodejs/UE0ZbfLt6t8
// https://groups.google.com/forum/#!topic/nodejs-dev/jR7-5UDqXkw
require('../common');
const common = require('../common');
var net = require('net');
var assert = require('assert');
var start = new Date();
var gotTimeout = false;
var gotConnect = false;
var T = 100;
// 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');
socket.setTimeout(T);
socket.on('timeout', function() {
socket.on('timeout', common.mustCall(function() {
console.error('timeout');
gotTimeout = true;
var now = new Date();
assert.ok(now - start < T + 500);
socket.destroy();
});
}));
socket.on('connect', function() {
console.error('connect');
gotConnect = true;
socket.destroy();
});
process.on('exit', function() {
assert.ok(gotTimeout);
assert.ok(!gotConnect);
});
socket.on('connect', common.fail);

View File

@ -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;
});
client.on('close', common.fail);
setTimeout(function() {
killed = true;
client.end();
}, TIMEOUT).unref();
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();

View File

@ -2,10 +2,6 @@
var common = require('../common');
var assert = require('assert');
var pwd_called = false;
var childClosed = false;
var childExited = false;
function pwd(callback) {
var output = '';
var child = common.spawnPwd();
@ -16,17 +12,14 @@ function pwd(callback) {
output += s;
});
child.on('exit', function(c) {
child.on('exit', common.mustCall(function(c) {
console.log('exit: ' + c);
assert.equal(0, c);
childExited = true;
});
}));
child.on('close', function() {
child.on('close', common.mustCall(function() {
callback(output);
pwd_called = true;
childClosed = true;
});
}));
}
@ -35,9 +28,3 @@ pwd(function(result) {
assert.equal(true, result.length > 1);
assert.equal('\n', result[result.length - 1]);
});
process.on('exit', function() {
assert.equal(true, pwd_called);
assert.equal(true, childExited);
assert.equal(true, childClosed);
});

View File

@ -48,21 +48,16 @@ if (process.argv[2] === 'child') {
var child = fork(process.argv[1], ['child']);
var childFlag = false;
var childSelfTerminate = false;
var parentEmit = false;
var parentFlag = false;
// when calling .disconnect the event should emit
// and the disconnected flag should be true.
child.on('disconnect', function() {
parentEmit = true;
child.on('disconnect', common.mustCall(function() {
parentFlag = child.connected;
});
}));
// the process should also self terminate without using signals
child.on('exit', function() {
childSelfTerminate = true;
});
child.on('exit', common.mustCall(function() {}));
// when child is listening
child.on('message', function(obj) {
@ -91,8 +86,5 @@ if (process.argv[2] === 'child') {
process.on('exit', function() {
assert.equal(childFlag, false);
assert.equal(parentFlag, false);
assert.ok(childSelfTerminate);
assert.ok(parentEmit);
});
}

View File

@ -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);
});
}));

View File

@ -3,9 +3,6 @@ const common = require('../common');
var assert = require('assert');
var exec = require('child_process').exec;
var success_count = 0;
var error_count = 0;
var pwdcommand, dir;
if (common.isWindows) {
@ -16,21 +13,7 @@ if (common.isWindows) {
dir = '/dev';
}
exec(pwdcommand, {cwd: dir}, function(err, stdout, stderr) {
if (err) {
error_count++;
console.log('error!: ' + err.code);
console.log('stdout: ' + JSON.stringify(stdout));
console.log('stderr: ' + JSON.stringify(stderr));
assert.equal(false, err.killed);
} else {
success_count++;
console.log(stdout);
exec(pwdcommand, {cwd: dir}, common.mustCall(function(err, stdout, stderr) {
assert.ifError(err);
assert.ok(stdout.indexOf(dir) == 0);
}
});
process.on('exit', function() {
assert.equal(1, success_count);
assert.equal(0, error_count);
});
}));

View File

@ -4,17 +4,10 @@ var assert = require('assert');
var child_process = require('child_process');
function test(fun, code) {
var errors = 0;
fun('does-not-exist', function(err) {
fun('does-not-exist', common.mustCall(function(err) {
assert.equal(err.code, code);
assert(/does\-not\-exist/.test(err.cmd));
errors++;
});
process.on('exit', function() {
assert.equal(errors, 1);
});
}));
}
if (common.isWindows) {

View File

@ -4,29 +4,18 @@ var assert = require('assert');
var spawn = require('child_process').spawn;
var path = require('path');
var exits = 0;
var exitScript = path.join(common.fixturesDir, 'exit.js');
var exitChild = spawn(process.argv[0], [exitScript, 23]);
exitChild.on('exit', function(code, signal) {
exitChild.on('exit', common.mustCall(function(code, signal) {
assert.strictEqual(code, 23);
assert.strictEqual(signal, null);
exits++;
});
}));
var errorScript = path.join(common.fixturesDir,
'child_process_should_emit_error.js');
var errorChild = spawn(process.argv[0], [errorScript]);
errorChild.on('exit', function(code, signal) {
errorChild.on('exit', common.mustCall(function(code, signal) {
assert.ok(code !== 0);
assert.strictEqual(signal, null);
exits++;
});
process.on('exit', function() {
assert.equal(2, exits);
});
}));

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var spawn = require('child_process').spawn;
var fork = require('child_process').fork;
@ -7,12 +7,11 @@ var fork = require('child_process').fork;
// Fork, then spawn. The spawned process should not hang.
switch (process.argv[2] || '') {
case '':
fork(__filename, ['fork']).on('exit', checkExit);
process.on('exit', haveExit);
fork(__filename, ['fork']).on('exit', common.mustCall(checkExit));
break;
case 'fork':
spawn(process.execPath, [__filename, 'spawn']).on('exit', checkExit);
process.on('exit', haveExit);
spawn(process.execPath, [__filename, 'spawn'])
.on('exit', common.mustCall(checkExit));
break;
case 'spawn':
break;
@ -20,14 +19,7 @@ switch (process.argv[2] || '') {
assert(0);
}
var seenExit = false;
function checkExit(statusCode) {
seenExit = true;
assert.equal(statusCode, 0);
process.nextTick(process.exit);
}
function haveExit() {
assert.equal(seenExit, true);
}

View File

@ -9,28 +9,22 @@ let gotMessage = false;
let gotExit = false;
let gotClose = false;
cp.on('message', function(message) {
cp.on('message', common.mustCall(function(message) {
assert(!gotMessage);
assert(!gotClose);
assert.strictEqual(message, 'hello');
gotMessage = true;
});
}));
cp.on('exit', function() {
cp.on('exit', common.mustCall(function() {
assert(!gotExit);
assert(!gotClose);
gotExit = true;
});
}));
cp.on('close', function() {
cp.on('close', common.mustCall(function() {
assert(gotMessage);
assert(gotExit);
assert(!gotClose);
gotClose = true;
});
process.on('exit', function() {
assert(gotMessage);
assert(gotExit);
assert(gotClose);
});
}));

View File

@ -1,6 +1,6 @@
'use strict';
const common = require('../common');
var assert = require('assert');
var common = require('../common');
var fork = require('child_process').fork;
var args = ['foo', 'bar'];
@ -19,11 +19,6 @@ assert.throws(function() { n.send(); }, TypeError);
n.send({ hello: 'world' });
var childExitCode = -1;
n.on('exit', function(c) {
childExitCode = c;
});
process.on('exit', function() {
assert.ok(childExitCode == 0);
});
n.on('exit', common.mustCall(function(c) {
assert.strictEqual(c, 0);
}));

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
//messages
@ -21,18 +21,11 @@ if (process.argv[2] === 'child') {
var fork = require('child_process').fork;
var child = fork(process.argv[1], ['child']);
var gotNormal;
child.once('message', function(data) {
gotNormal = data;
});
child.once('message', common.mustCall(function(data) {
assert.deepStrictEqual(data, normal);
}));
var gotInternal;
child.once('internalMessage', function(data) {
gotInternal = data;
});
process.on('exit', function() {
assert.deepStrictEqual(gotNormal, normal);
assert.deepStrictEqual(gotInternal, internal);
});
child.once('internalMessage', common.mustCall(function(data) {
assert.deepStrictEqual(data, internal);
}));
}

View File

@ -1,41 +1,18 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var spawn = require('child_process').spawn;
var exitCode;
var termSignal;
var gotStdoutEOF = false;
var gotStderrEOF = false;
var cat = spawn(common.isWindows ? 'cmd' : 'cat');
cat.stdout.on('end', common.mustCall(function() {}));
cat.stderr.on('data', common.fail);
cat.stderr.on('end', common.mustCall(function() {}));
cat.stdout.on('end', function() {
gotStdoutEOF = true;
});
cat.stderr.on('data', function(chunk) {
assert.ok(false);
});
cat.stderr.on('end', function() {
gotStderrEOF = true;
});
cat.on('exit', function(code, signal) {
exitCode = code;
termSignal = signal;
});
cat.on('exit', common.mustCall(function(code, signal) {
assert.strictEqual(code, null);
assert.strictEqual(signal, 'SIGTERM');
}));
assert.equal(cat.killed, false);
cat.kill();
assert.equal(cat.killed, true);
process.on('exit', function() {
assert.strictEqual(exitCode, null);
assert.strictEqual(termSignal, 'SIGTERM');
assert.ok(gotStdoutEOF);
assert.ok(gotStderrEOF);
});

View File

@ -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);
});
}));

View File

@ -3,22 +3,15 @@ var common = require('../common');
var spawn = require('child_process').spawn;
var assert = require('assert');
var errors = 0;
var enoentPath = 'foo123';
var spawnargs = ['bar'];
assert.equal(common.fileExists(enoentPath), false);
var enoentChild = spawn(enoentPath, spawnargs);
enoentChild.on('error', function(err) {
enoentChild.on('error', common.mustCall(function(err) {
assert.equal(err.code, 'ENOENT');
assert.equal(err.errno, 'ENOENT');
assert.equal(err.syscall, 'spawn ' + enoentPath);
assert.equal(err.path, enoentPath);
assert.deepStrictEqual(err.spawnargs, spawnargs);
errors++;
});
process.on('exit', function() {
assert.equal(1, errors);
});
}));

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var spawn = require('child_process').spawn;
@ -14,11 +14,6 @@ var proc = spawn(process.execPath, [__filename, 'child'], {
stdio: ['ipc', 'inherit', 'inherit']
});
var childCode = -1;
proc.on('exit', function(code) {
childCode = code;
});
process.on('exit', function() {
assert.equal(childCode, 0);
});
proc.on('exit', common.mustCall(function(code) {
assert.strictEqual(code, 0);
}));

View File

@ -15,8 +15,6 @@ assert.ok(!cat.stdin.readable);
cat.stdin.end();
var response = '';
var exitStatus = -1;
var closed = false;
cat.stdout.setEncoding('utf8');
cat.stdout.on('data', function(chunk) {
@ -26,33 +24,18 @@ cat.stdout.on('data', function(chunk) {
cat.stdout.on('end', common.mustCall(function() {}));
cat.stderr.on('data', function(chunk) {
// shouldn't get any stderr output
assert.ok(false);
});
cat.stderr.on('data', common.fail);
cat.stderr.on('end', common.mustCall(function() {}));
cat.on('exit', function(status) {
console.log('exit event');
exitStatus = status;
});
cat.on('exit', common.mustCall(function(status) {
assert.strictEqual(0, status);
}));
cat.on('close', function() {
closed = true;
cat.on('close', common.mustCall(function() {
if (common.isWindows) {
assert.equal('hello world\r\n', response);
} else {
assert.equal('hello world', response);
}
});
process.on('exit', function() {
assert.equal(0, exitStatus);
assert(closed);
if (common.isWindows) {
assert.equal('hello world\r\n', response);
} else {
assert.equal('hello world', response);
}
});
}));

View File

@ -13,18 +13,13 @@ if (common.isWindows) {
if (cluster.isMaster) {
common.refreshTmpDir();
var ok = false;
var worker = cluster.fork();
worker.on('message', function(msg) {
worker.on('message', common.mustCall(function(msg) {
assert.equal(msg, 'DONE');
ok = true;
});
}));
worker.on('exit', function() {
process.exit();
});
process.on('exit', function() {
assert(ok);
});
return;
}

View File

@ -5,20 +5,15 @@ var cluster = require('cluster');
var net = require('net');
if (cluster.isMaster) {
var port = null;
cluster.fork();
cluster.on('listening', function(worker, address) {
port = address.port;
cluster.on('listening', common.mustCall(function(worker, address) {
const port = address.port;
// ensure that the port is not 0 or null
assert(port);
// ensure that the port is numerical
assert.strictEqual(typeof port, 'number');
worker.kill();
});
process.on('exit', function() {
// ensure that the 'listening' handler has been called
assert(port);
});
}));
} else {
net.createServer(common.fail).listen(0);
}

View File

@ -6,14 +6,9 @@ var net = require('net');
if (cluster.isMaster) {
// ensure that the worker exits peacefully
var worker = cluster.fork();
worker.on('exit', function(statusCode) {
cluster.fork().on('exit', common.mustCall(function(statusCode) {
assert.equal(statusCode, 0);
worker = null;
});
process.on('exit', function() {
assert.equal(worker, null);
});
}));
} else {
// listen() without port should not trigger a libuv assert
net.createServer(common.fail).listen(process.exit);

View File

@ -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() {}));
}));

View File

@ -3,7 +3,7 @@
// one that the cluster module installs.
// https://github.com/joyent/node/issues/2556
require('../common');
const common = require('../common');
var assert = require('assert');
var cluster = require('cluster');
var fork = require('child_process').fork;
@ -13,16 +13,10 @@ var MAGIC_EXIT_CODE = 42;
var isTestRunner = process.argv[2] != 'child';
if (isTestRunner) {
var exitCode = -1;
process.on('exit', function() {
assert.equal(exitCode, MAGIC_EXIT_CODE);
});
var master = fork(__filename, ['child']);
master.on('exit', function(code) {
exitCode = code;
});
master.on('exit', common.mustCall(function(code) {
assert.strictEqual(code, MAGIC_EXIT_CODE);
}));
} else if (cluster.isMaster) {
process.on('uncaughtException', function() {
process.nextTick(function() {

View File

@ -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);
});
}));
}

View File

@ -7,26 +7,18 @@
* both code paths.
*/
require('../common');
const common = require('../common');
var cluster = require('cluster');
var assert = require('assert');
var worker1, worker2, workerExited, workerDisconnected;
var worker1, worker2;
if (cluster.isMaster) {
worker1 = cluster.fork();
worker2 = cluster.fork();
workerExited = 0;
workerDisconnected = 0;
[worker1, worker2].forEach(function(worker) {
worker.on('disconnect', ondisconnect);
worker.on('exit', onexit);
worker.on('disconnect', common.mustCall(function() {}));
worker.on('exit', common.mustCall(function() {}));
});
process.on('exit', onProcessExit);
} else {
if (cluster.worker.id === 1) {
// Call destroy when worker is disconnected
@ -40,20 +32,3 @@ if (cluster.isMaster) {
cluster.worker.destroy();
}
}
function onProcessExit() {
assert.equal(workerExited,
2,
'When master exits, all workers should have exited too');
assert.equal(workerDisconnected,
2,
'When master exits, all workers should have disconnected');
}
function ondisconnect() {
++workerDisconnected;
}
function onexit() {
++workerExited;
}

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var cluster = require('cluster');
@ -24,14 +24,6 @@ if (cluster.isWorker) {
return;
}
var unforcedOk;
var forcedOk;
process.on('exit', function() {
assert(forcedOk);
assert(unforcedOk);
});
checkUnforced();
checkForced();
@ -40,10 +32,9 @@ function checkUnforced() {
.on('online', function() {
this.disconnect();
})
.on('exit', function(status) {
.on('exit', common.mustCall(function(status) {
assert.equal(status, SENTINEL);
unforcedOk = true;
});
}));
}
function checkForced() {
@ -51,8 +42,7 @@ function checkForced() {
.on('online', function() {
this.process.disconnect();
})
.on('exit', function(status) {
.on('exit', common.mustCall(function(status) {
assert.equal(status, 0);
forcedOk = true;
});
}));
}

View File

@ -4,7 +4,6 @@ var domain = require('domain');
var assert = require('assert');
var d = domain.create();
var expect = ['pbkdf2', 'randomBytes', 'pseudoRandomBytes'];
var errors = 0;
if (!common.hasCrypto) {
common.skip('missing crypto');
@ -12,14 +11,9 @@ if (!common.hasCrypto) {
}
var crypto = require('crypto');
process.on('exit', function() {
assert.equal(errors, 3);
});
d.on('error', function(e) {
d.on('error', common.mustCall(function(e) {
assert.equal(e.message, expect.shift());
errors += 1;
});
}, 3));
d.run(function() {
one();

View File

@ -12,16 +12,9 @@ var stream = require('stream');
var s = new stream.PassThrough();
var h = crypto.createHash('sha1');
var expect = '15987e60950cf22655b9323bc1e281f9c4aff47e';
var gotData = false;
process.on('exit', function() {
assert(gotData);
console.log('ok');
});
s.pipe(h).on('data', function(c) {
s.pipe(h).on('data', common.mustCall(function(c) {
assert.equal(c, expect);
gotData = true;
}).setEncoding('hex');
})).setEncoding('hex');
s.end('aoeu');

View File

@ -3,13 +3,9 @@ var common = require('../common');
var path = require('path');
var assert = require('assert');
var a;
setTimeout(function() {
a = require(path.join(common.fixturesDir, 'a'));
}, 50);
process.on('exit', function() {
assert.equal(true, 'A' in a);
assert.equal('A', a.A());
assert.equal('D', a.D());
});
setTimeout(common.mustCall(function() {
const a = require(path.join(common.fixturesDir, 'a'));
assert.strictEqual(true, 'A' in a);
assert.strictEqual('A', a.A());
assert.strictEqual('D', a.D());
}), 50);

View File

@ -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() {}));

View File

@ -2,24 +2,18 @@
// Ensure that if a dgram socket is closed before the DNS lookup completes, it
// won't crash.
const assert = require('assert');
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
var buf = Buffer.alloc(1024, 42);
var socket = dgram.createSocket('udp4');
var handle = socket._handle;
var closeEvents = 0;
var closeCallbacks = 0;
socket.send(buf, 0, buf.length, common.PORT, 'localhost');
assert.strictEqual(socket.close(function() {
++closeCallbacks;
}), socket);
socket.on('close', function() {
assert.equal(closeCallbacks, 1);
++closeEvents;
});
assert.strictEqual(socket.close(common.mustCall(function() {})), socket);
socket.on('close', common.mustCall(function() {}));
socket = null;
// Verify that accessing handle after closure doesn't throw
@ -28,8 +22,3 @@ setImmediate(function() {
console.log('Handle fd is: ', handle.fd);
});
});
process.on('exit', function() {
assert.equal(closeEvents, 1);
assert.equal(closeCallbacks, 1);
});

View File

@ -1,24 +1,19 @@
'use strict';
require('../common');
var assert = require('assert');
const common = require('../common');
var dgram = require('dgram');
var source = dgram.createSocket('udp4');
var target = dgram.createSocket('udp4');
var messages = 0;
process.on('exit', function() {
assert.equal(messages, 2);
});
target.on('message', function(buf) {
target.on('message', common.mustCall(function(buf) {
if (buf.toString() === 'abc') ++messages;
if (buf.toString() === 'def') ++messages;
if (messages === 2) {
source.close();
target.close();
}
});
}, 2));
target.on('listening', function() {
// Second .send() call should not throw a bind error.

View File

@ -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();

View File

@ -1,15 +1,13 @@
'use strict';
// Simple tests of most basic domain functionality.
require('../common');
const common = require('../common');
var assert = require('assert');
var domain = require('domain');
var caught = 0;
var expectCaught = 1;
var d = new domain.Domain();
d.on('error', function(er) {
d.on('error', common.mustCall(function(er) {
console.error('caught', er);
assert.strictEqual(er.domain, d);
@ -18,15 +16,7 @@ d.on('error', function(er) {
assert.strictEqual(er.code, 'ENOENT');
assert.ok(/\bthis file does not exist\b/i.test(er.path));
assert.strictEqual(typeof er.errno, 'number');
caught++;
});
process.on('exit', function() {
console.error('exit');
assert.equal(caught, expectCaught);
console.log('ok');
});
}));
// implicit handling of thrown errors while in a domain, via the

View File

@ -1,16 +1,16 @@
'use strict';
require('../common');
const common = require('../common');
var domain = require('domain');
var assert = require('assert');
var timeout_err, timeout, immediate_err;
var timeout;
var timeoutd = domain.create();
timeoutd.on('error', function(e) {
timeout_err = e;
timeoutd.on('error', common.mustCall(function(e) {
assert.equal(e.message, 'Timeout UNREFd', 'Domain should catch timer error');
clearTimeout(timeout);
});
}));
timeoutd.run(function() {
setTimeout(function() {
@ -20,9 +20,10 @@ timeoutd.run(function() {
var immediated = domain.create();
immediated.on('error', function(e) {
immediate_err = e;
});
immediated.on('error', common.mustCall(function(e) {
assert.equal(e.message, 'Immediate Error',
'Domain should catch immediate error');
}));
immediated.run(function() {
setImmediate(function() {
@ -31,10 +32,3 @@ immediated.run(function() {
});
timeout = setTimeout(function() {}, 10 * 1000);
process.on('exit', function() {
assert.equal(timeout_err.message, 'Timeout UNREFd',
'Domain should catch timer error');
assert.equal(immediate_err.message, 'Immediate Error',
'Domain should catch immediate error');
});

View File

@ -4,8 +4,6 @@ var assert = require('assert');
var exec = require('child_process').exec;
var path = require('path');
var exits = 0;
function errExec(script, callback) {
var cmd = '"' + process.argv[0] + '" "' +
path.join(common.fixturesDir, script) + '"';
@ -21,52 +19,45 @@ function errExec(script, callback) {
// Proxy the args for more tests.
callback(err, stdout, stderr);
// Count the tests
exits++;
});
}
// Simple throw error
errExec('throws_error.js', function(err, stdout, stderr) {
errExec('throws_error.js', common.mustCall(function(err, stdout, stderr) {
assert.ok(/blah/.test(stderr));
});
}));
// Trying to JSON.parse(undefined)
errExec('throws_error2.js', function(err, stdout, stderr) {
errExec('throws_error2.js', common.mustCall(function(err, stdout, stderr) {
assert.ok(/SyntaxError/.test(stderr));
});
}));
// Trying to JSON.parse(undefined) in nextTick
errExec('throws_error3.js', function(err, stdout, stderr) {
errExec('throws_error3.js', common.mustCall(function(err, stdout, stderr) {
assert.ok(/SyntaxError/.test(stderr));
});
}));
// throw ILLEGAL error
errExec('throws_error4.js', function(err, stdout, stderr) {
errExec('throws_error4.js', common.mustCall(function(err, stdout, stderr) {
assert.ok(/\/\*\*/.test(stderr));
assert.ok(/SyntaxError/.test(stderr));
});
}));
// Specific long exception line doesn't result in stack overflow
errExec('throws_error5.js', function(err, stdout, stderr) {
errExec('throws_error5.js', common.mustCall(function(err, stdout, stderr) {
assert.ok(/SyntaxError/.test(stderr));
});
}));
// Long exception line with length > errorBuffer doesn't result in assertion
errExec('throws_error6.js', function(err, stdout, stderr) {
errExec('throws_error6.js', common.mustCall(function(err, stdout, stderr) {
assert.ok(/SyntaxError/.test(stderr));
});
}));
// Object that throws in toString() doesn't print garbage
errExec('throws_error7.js', function(err, stdout, stderr) {
errExec('throws_error7.js', common.mustCall(function(err, stdout, stderr) {
assert.ok(/<toString\(\) threw exception/.test(stderr));
});
process.on('exit', function() {
assert.equal(7, exits);
});
}));

View File

@ -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);
});
}));

View File

@ -1,19 +1,10 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var events = require('events');
var gotEvent = false;
process.on('exit', function() {
assert(gotEvent);
});
var e = new events.EventEmitter();
e.on('maxListeners', function() {
gotEvent = true;
});
e.on('maxListeners', common.mustCall(function() {}));
// Should not corrupt the 'maxListeners' queue.
e.setMaxListeners(42);

View File

@ -1,22 +1,14 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var events = require('events');
var domain = require('domain');
var errorCatched = false;
var e = new events.EventEmitter();
var d = domain.create();
d.add(e);
d.on('error', function(er) {
d.on('error', common.mustCall(function(er) {
assert(er instanceof Error, 'error created');
errorCatched = true;
});
}));
e.emit('error');
process.on('exit', function() {
assert(errorCatched, 'error got caught');
});

View File

@ -1,14 +1,10 @@
'use strict';
const common = require('../common');
var assert = require('assert');
var events = require('events');
var e = new events.EventEmitter();
var times_hello_emited = 0;
e.once('hello', function(a, b) {
times_hello_emited++;
});
e.once('hello', common.mustCall(function(a, b) {}));
e.emit('hello', 'a', 'b');
e.emit('hello', 'a', 'b');
@ -23,23 +19,10 @@ e.once('foo', remove);
e.removeListener('foo', remove);
e.emit('foo');
process.on('exit', function() {
assert.equal(1, times_hello_emited);
});
var times_recurse_emitted = 0;
e.once('e', function() {
e.once('e', common.mustCall(function() {
e.emit('e');
times_recurse_emitted++;
});
}));
e.once('e', function() {
times_recurse_emitted++;
});
e.once('e', common.mustCall(function() {}));
e.emit('e');
process.on('exit', function() {
assert.equal(2, times_recurse_emitted);
});

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
@ -13,10 +13,7 @@ function MyEE(cb) {
EventEmitter.call(this);
}
var called = false;
var myee = new MyEE(function() {
called = true;
});
var myee = new MyEE(common.mustCall(function() {}));
util.inherits(ErrorEE, EventEmitter);
@ -29,7 +26,6 @@ assert.throws(function() {
}, /blerg/);
process.on('exit', function() {
assert(called);
assert(!(myee._events instanceof Object));
assert.deepStrictEqual(Object.keys(myee._events), []);
console.log('ok');

View File

@ -1,27 +1,19 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var MESSAGE = 'catch me if you can';
var caughtException = false;
process.on('uncaughtException', function(e) {
process.on('uncaughtException', common.mustCall(function(e) {
console.log('uncaught exception! 1');
assert.equal(MESSAGE, e.message);
caughtException = true;
});
}));
process.on('uncaughtException', function(e) {
process.on('uncaughtException', common.mustCall(function(e) {
console.log('uncaught exception! 2');
assert.equal(MESSAGE, e.message);
caughtException = true;
});
}));
setTimeout(function() {
throw new Error(MESSAGE);
}, 10);
process.on('exit', function() {
console.log('exit');
assert.equal(true, caughtException);
});

View File

@ -3,20 +3,8 @@ var common = require('../common');
var assert = require('assert');
var path = require('path');
var fs = require('fs');
var got_error = false;
var filename = path.join(common.fixturesDir, 'does_not_exist.txt');
fs.readFile(filename, 'latin1', function(err, content) {
if (err) {
got_error = true;
} else {
console.error('cat returned some content: ' + content);
console.error('this shouldn\'t happen as the file doesn\'t exist...');
assert.equal(true, false);
}
});
process.on('exit', function() {
console.log('done');
assert.equal(true, got_error);
});
fs.readFile(filename, 'latin1', common.mustCall(function(err, content) {
assert.ok(err);
}));

View File

@ -1,23 +1,16 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var fs = require('fs');
var f = __filename;
var exists;
var doesNotExist;
fs.exists(f, function(y) {
exists = y;
});
fs.exists(f, common.mustCall(function(y) {
assert.strictEqual(y, true);
}));
fs.exists(f + '-NO', function(y) {
doesNotExist = y;
});
fs.exists(f + '-NO', common.mustCall(function(y) {
assert.strictEqual(y, false);
}));
assert(fs.existsSync(f));
assert(!fs.existsSync(f + '-NO'));
process.on('exit', function() {
assert.strictEqual(exists, true);
assert.strictEqual(doesNotExist, false);
});

View File

@ -2,15 +2,12 @@
var common = require('../common');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
if (!common.isWindows) {
common.skip('this test is Windows-specific.');
return;
}
var successes = 0;
// make a path that will be at least 260 chars long.
var fileNameLen = Math.max(260 - common.tmpDir.length - 1, 1);
var fileName = path.join(common.tmpDir, new Array(fileNameLen + 1).join('x'));
@ -23,17 +20,14 @@ console.log({
fullPathLength: fullPath.length
});
fs.writeFile(fullPath, 'ok', function(err) {
fs.writeFile(fullPath, 'ok', common.mustCall(function(err) {
if (err) throw err;
successes++;
fs.stat(fullPath, function(err, stats) {
fs.stat(fullPath, common.mustCall(function(err, stats) {
if (err) throw err;
successes++;
});
});
}));
}));
process.on('exit', function() {
fs.unlinkSync(fullPath);
assert.equal(2, successes);
});

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var fs = require('fs');
@ -14,24 +14,14 @@ try {
}
assert.ok(caughtException);
var openFd;
fs.open(__filename, 'r', function(err, fd) {
fs.open(__filename, 'r', common.mustCall(function(err, fd) {
if (err) {
throw err;
}
openFd = fd;
});
}));
var openFd2;
fs.open(__filename, 'rs', function(err, fd) {
fs.open(__filename, 'rs', common.mustCall(function(err, fd) {
if (err) {
throw err;
}
openFd2 = fd;
});
process.on('exit', function() {
assert.ok(openFd);
assert.ok(openFd2);
});
}));

View File

@ -9,19 +9,17 @@ const fd = fs.openSync(filepath, 'r');
const expected = 'xyz\n';
const bufferAsync = Buffer.allocUnsafe(expected.length);
const bufferSync = Buffer.allocUnsafe(expected.length);
let readCalled = 0;
fs.read(fd, bufferAsync, 0, expected.length, 0, function(err, bytesRead) {
readCalled++;
fs.read(fd,
bufferAsync,
0,
expected.length,
0,
common.mustCall(function(err, bytesRead) {
assert.equal(bytesRead, expected.length);
assert.deepStrictEqual(bufferAsync, Buffer.from(expected));
});
}));
var r = fs.readSync(fd, bufferSync, 0, expected.length, 0);
assert.deepStrictEqual(bufferSync, Buffer.from(expected));
assert.equal(r, expected.length);
process.on('exit', function() {
assert.equal(readCalled, 1);
});

View File

@ -6,20 +6,17 @@ const fs = require('fs');
const filepath = path.join(common.fixturesDir, 'x.txt');
const fd = fs.openSync(filepath, 'r');
const expected = 'xyz\n';
let readCalled = 0;
fs.read(fd, expected.length, 0, 'utf-8', function(err, str, bytesRead) {
readCalled++;
fs.read(fd,
expected.length,
0,
'utf-8',
common.mustCall(function(err, str, bytesRead) {
assert.ok(!err);
assert.equal(str, expected);
assert.equal(bytesRead, expected.length);
});
}));
var r = fs.readSync(fd, expected.length, 0, 'utf-8');
assert.equal(r[0], expected);
assert.equal(r[1], expected.length);
process.on('exit', function() {
assert.equal(readCalled, 1);
});

View File

@ -11,8 +11,6 @@ if (process.platform === 'freebsd') {
return;
}
var callbacks = 0;
function test(env, cb) {
var filename = path.join(common.fixturesDir, 'test-fs-readfile-error.js');
var execPath = '"' + process.execPath + '" "' + filename + '"';
@ -25,18 +23,12 @@ function test(env, cb) {
});
}
test({ NODE_DEBUG: '' }, function(data) {
test({ NODE_DEBUG: '' }, common.mustCall(function(data) {
assert(/EISDIR/.test(data));
assert(!/test-fs-readfile-error/.test(data));
callbacks++;
});
}));
test({ NODE_DEBUG: 'fs' }, function(data) {
test({ NODE_DEBUG: 'fs' }, common.mustCall(function(data) {
assert(/EISDIR/.test(data));
assert(/test-fs-readfile-error/.test(data));
callbacks++;
});
process.on('exit', function() {
assert.equal(callbacks, 2);
});
}));

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var fs = require('fs');
@ -26,13 +26,6 @@ fs.fstatSync = function(fd) {
var d = fs.readFileSync(__filename, 'utf8');
assert.equal(d, dataExpected);
var called = false;
fs.readFile(__filename, 'utf8', function(er, d) {
fs.readFile(__filename, 'utf8', common.mustCall(function(er, d) {
assert.equal(d, dataExpected);
called = true;
});
process.on('exit', function() {
assert(called);
console.log('ok');
});
}));

View File

@ -68,18 +68,11 @@ function asynctest(testBlock, args, callback, assertBlock) {
// sub-tests:
function test_simple_error_callback(cb) {
var ncalls = 0;
fs.realpath('/this/path/does/not/exist', function(err, s) {
fs.realpath('/this/path/does/not/exist', common.mustCall(function(err, s) {
assert(err);
assert(!s);
ncalls++;
cb();
});
process.on('exit', function() {
assert.equal(ncalls, 1);
});
}));
}
function test_simple_relative_symlink(callback) {

View File

@ -1,80 +1,60 @@
/* eslint-disable strict */
require('../common');
const common = require('../common');
var assert = require('assert');
var fs = require('fs');
var got_error = false;
var success_count = 0;
fs.stat('.', function(err, stats) {
if (err) {
got_error = true;
} else {
console.dir(stats);
fs.stat('.', common.mustCall(function(err, stats) {
assert.ifError(err);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
assert(this === global);
});
}));
fs.stat('.', function(err, stats) {
fs.stat('.', common.mustCall(function(err, stats) {
assert.ok(stats.hasOwnProperty('blksize'));
assert.ok(stats.hasOwnProperty('blocks'));
});
}));
fs.lstat('.', function(err, stats) {
if (err) {
got_error = true;
} else {
console.dir(stats);
fs.lstat('.', common.mustCall(function(err, stats) {
assert.ifError(err);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
assert(this === global);
});
}));
// fstat
fs.open('.', 'r', undefined, function(err, fd) {
fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
assert.ok(!err);
assert.ok(fd);
fs.fstat(fd, function(err, stats) {
if (err) {
got_error = true;
} else {
console.dir(stats);
fs.fstat(fd, common.mustCall(function(err, stats) {
assert.ifError(err);
assert.ok(stats.mtime instanceof Date);
success_count++;
fs.close(fd);
}
assert(this === global);
});
}));
assert(this === global);
});
}));
// fstatSync
fs.open('.', 'r', undefined, function(err, fd) {
fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
var stats;
try {
stats = fs.fstatSync(fd);
} catch (err) {
got_error = true;
common.fail(err);
}
if (stats) {
console.dir(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
fs.close(fd);
});
}));
console.log(`stating: ${__filename}`);
fs.stat(__filename, function(err, s) {
if (err) {
got_error = true;
} else {
fs.stat(__filename, common.mustCall(function(err, s) {
assert.ifError(err);
console.dir(s);
success_count++;
console.log('isDirectory: ' + JSON.stringify(s.isDirectory()));
assert.equal(false, s.isDirectory());
@ -98,11 +78,4 @@ fs.stat(__filename, function(err, s) {
assert.equal(false, s.isSymbolicLink());
assert.ok(s.mtime instanceof Date);
}
});
process.on('exit', function() {
assert.equal(5, success_count);
assert.equal(false, got_error);
});
}));

View File

@ -1,6 +1,5 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var fs = require('fs');
common.refreshTmpDir();
@ -20,24 +19,14 @@ function test1(stream) {
function test2(stream) {
stream.destroy();
stream.on('open', function(fd) {
stream.on('open', common.mustCall(function(fd) {
stream.destroy();
open_cb_called++;
});
process.on('exit', function() {
assert.equal(open_cb_called, 1);
});
var open_cb_called = 0;
}));
}
function test3(stream) {
stream.on('open', function(fd) {
stream.on('open', common.mustCall(function(fd) {
stream.destroy();
stream.destroy();
open_cb_called++;
});
process.on('exit', function() {
assert.equal(open_cb_called, 1);
});
var open_cb_called = 0;
}));
}

View File

@ -5,8 +5,6 @@ var common = require('../common');
var assert = require('assert');
var path = require('path');
var fs = require('fs');
var completed = 0;
var expected_tests = 2;
var linkPath1 = path.join(common.tmpDir, 'junction1');
var linkPath2 = path.join(common.tmpDir, 'junction2');
@ -16,10 +14,10 @@ var linkData = path.join(common.fixturesDir);
common.refreshTmpDir();
// Test fs.symlink()
fs.symlink(linkData, linkPath1, 'junction', function(err) {
fs.symlink(linkData, linkPath1, 'junction', common.mustCall(function(err) {
if (err) throw err;
verifyLink(linkPath1);
});
}));
// Test fs.symlinkSync()
fs.symlinkSync(linkData, linkPath2, 'junction');
@ -35,10 +33,4 @@ function verifyLink(linkPath) {
// Clean up.
fs.unlinkSync(linkPath);
completed++;
}
process.on('exit', function() {
assert.equal(completed, expected_tests);
});

View File

@ -3,8 +3,6 @@ var common = require('../common');
var assert = require('assert');
var path = require('path');
var fs = require('fs');
var completed = 0;
var expected_tests = 4;
// test creating and reading symbolic link
var linkData = path.join(common.fixturesDir, 'cycles/');
@ -15,31 +13,22 @@ common.refreshTmpDir();
console.log('linkData: ' + linkData);
console.log('linkPath: ' + linkPath);
fs.symlink(linkData, linkPath, 'junction', function(err) {
fs.symlink(linkData, linkPath, 'junction', common.mustCall(function(err) {
if (err) throw err;
completed++;
fs.lstat(linkPath, function(err, stats) {
fs.lstat(linkPath, common.mustCall(function(err, stats) {
if (err) throw err;
assert.ok(stats.isSymbolicLink());
completed++;
fs.readlink(linkPath, function(err, destination) {
fs.readlink(linkPath, common.mustCall(function(err, destination) {
if (err) throw err;
assert.equal(destination, linkData);
completed++;
fs.unlink(linkPath, function(err) {
fs.unlink(linkPath, common.mustCall(function(err) {
if (err) throw err;
assert(!common.fileExists(linkPath));
assert(common.fileExists(linkData));
completed++;
});
});
});
});
process.on('exit', function() {
assert.equal(completed, expected_tests);
});
}));
}));
}));
}));

View File

@ -7,19 +7,15 @@ var tmp = common.tmpDir;
common.refreshTmpDir();
var filename = path.resolve(tmp, 'truncate-file.txt');
var success = 0;
fs.writeFileSync(filename, 'hello world', 'utf8');
var fd = fs.openSync(filename, 'r+');
fs.truncate(fd, 5, function(err) {
fs.truncate(fd, 5, common.mustCall(function(err) {
assert.ok(!err);
assert.equal(fs.readFileSync(filename, 'utf8'), 'hello');
success++;
});
}));
process.on('exit', function() {
fs.closeSync(fd);
fs.unlinkSync(filename);
assert.equal(success, 1);
console.log('ok');
});

View File

@ -42,20 +42,12 @@ assert.equal(stat.size, 0);
fs.closeSync(fd);
// async tests
var success = 0;
testTruncate(function(er) {
testTruncate(common.mustCall(function(er) {
if (er) throw er;
success++;
testFtruncate(function(er) {
testFtruncate(common.mustCall(function(er) {
if (er) throw er;
success++;
});
});
process.on('exit', function() {
assert.equal(success, 2);
console.log('ok');
});
}));
}));
function testTruncate(cb) {
fs.writeFile(filename, data, function(er) {

View File

@ -6,18 +6,18 @@ const Buffer = require('buffer').Buffer;
const fs = require('fs');
const filename = path.join(common.tmpDir, 'write.txt');
const expected = Buffer.from('hello');
let openCalled = 0;
let writeCalled = 0;
common.refreshTmpDir();
fs.open(filename, 'w', 0o644, function(err, fd) {
openCalled++;
fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
if (err) throw err;
fs.write(fd, expected, 0, expected.length, null, function(err, written) {
writeCalled++;
fs.write(fd,
expected,
0,
expected.length,
null,
common.mustCall(function(err, written) {
if (err) throw err;
assert.equal(expected.length, written);
@ -26,10 +26,5 @@ fs.open(filename, 'w', 0o644, function(err, fd) {
var found = fs.readFileSync(filename, 'utf8');
assert.deepStrictEqual(expected.toString(), found);
fs.unlinkSync(filename);
});
});
process.on('exit', function() {
assert.equal(1, openCalled);
assert.equal(1, writeCalled);
});
}));
}));

View File

@ -17,41 +17,34 @@ var s = '南越国是前203年至前111年存在于岭南地区的一个国家
'历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' +
'它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n';
var ncallbacks = 0;
fs.writeFile(filename, s, function(e) {
fs.writeFile(filename, s, common.mustCall(function(e) {
if (e) throw e;
ncallbacks++;
fs.readFile(filename, function(e, buffer) {
fs.readFile(filename, common.mustCall(function(e, buffer) {
if (e) throw e;
ncallbacks++;
assert.equal(Buffer.byteLength(s), buffer.length);
});
});
}));
}));
// test that writeFile accepts buffers
var filename2 = join(common.tmpDir, 'test2.txt');
var buf = Buffer.from(s, 'utf8');
fs.writeFile(filename2, buf, function(e) {
fs.writeFile(filename2, buf, common.mustCall(function(e) {
if (e) throw e;
ncallbacks++;
fs.readFile(filename2, function(e, buffer) {
fs.readFile(filename2, common.mustCall(function(e, buffer) {
if (e) throw e;
ncallbacks++;
assert.equal(buf.length, buffer.length);
});
});
}));
}));
// test that writeFile accepts numbers.
var filename3 = join(common.tmpDir, 'test3.txt');
var m = 0o600;
fs.writeFile(filename3, n, { mode: m }, function(e) {
fs.writeFile(filename3, n, { mode: m }, common.mustCall(function(e) {
if (e) throw e;
// windows permissions aren't unix
@ -60,46 +53,35 @@ fs.writeFile(filename3, n, { mode: m }, function(e) {
assert.equal(st.mode & 0o700, m);
}
ncallbacks++;
fs.readFile(filename3, function(e, buffer) {
fs.readFile(filename3, common.mustCall(function(e, buffer) {
if (e) throw e;
ncallbacks++;
assert.equal(Buffer.byteLength('' + n), buffer.length);
});
});
}));
}));
// test that writeFile accepts file descriptors
var filename4 = join(common.tmpDir, 'test4.txt');
fs.open(filename4, 'w+', function(e, fd) {
fs.open(filename4, 'w+', common.mustCall(function(e, fd) {
if (e) throw e;
ncallbacks++;
fs.writeFile(fd, s, function(e) {
fs.writeFile(fd, s, common.mustCall(function(e) {
if (e) throw e;
ncallbacks++;
fs.close(fd, function(e) {
fs.close(fd, common.mustCall(function(e) {
if (e) throw e;
ncallbacks++;
fs.readFile(filename4, function(e, buffer) {
fs.readFile(filename4, common.mustCall(function(e, buffer) {
if (e) throw e;
ncallbacks++;
assert.equal(Buffer.byteLength(s), buffer.length);
});
});
});
});
}));
}));
}));
}));
process.on('exit', function() {
assert.equal(10, ncallbacks);
fs.unlinkSync(filename);
fs.unlinkSync(filename2);
fs.unlinkSync(filename3);

View File

@ -10,24 +10,19 @@ common.refreshTmpDir();
var fn = path.join(common.tmpDir, 'write-string-coerce.txt');
var data = true;
var expected = data + '';
var found;
fs.open(fn, 'w', 0o644, function(err, fd) {
fs.open(fn, 'w', 0o644, common.mustCall(function(err, fd) {
if (err) throw err;
console.log('open done');
fs.write(fd, data, 0, 'utf8', function(err, written) {
fs.write(fd, data, 0, 'utf8', common.mustCall(function(err, written) {
console.log('write done');
if (err) throw err;
assert.equal(Buffer.byteLength(expected), written);
fs.closeSync(fd);
found = fs.readFileSync(fn, 'utf8');
const found = fs.readFileSync(fn, 'utf8');
console.log('expected: "%s"', expected);
console.log('found: "%s"', found);
fs.unlinkSync(fn);
});
});
process.on('exit', function() {
assert.equal(expected, found);
});
assert.strictEqual(expected, found);
}));
}));

View File

@ -8,50 +8,45 @@ var fn = path.join(common.tmpDir, 'write.txt');
var fn2 = path.join(common.tmpDir, 'write2.txt');
var expected = 'ümlaut.';
var constants = fs.constants;
var found, found2;
common.refreshTmpDir();
fs.open(fn, 'w', 0o644, function(err, fd) {
fs.open(fn, 'w', 0o644, common.mustCall(function(err, fd) {
if (err) throw err;
console.log('open done');
fs.write(fd, '', 0, 'utf8', function(err, written) {
assert.equal(0, written);
});
fs.write(fd, expected, 0, 'utf8', function(err, written) {
fs.write(fd, expected, 0, 'utf8', common.mustCall(function(err, written) {
console.log('write done');
if (err) throw err;
assert.equal(Buffer.byteLength(expected), written);
fs.closeSync(fd);
found = fs.readFileSync(fn, 'utf8');
const found = fs.readFileSync(fn, 'utf8');
console.log('expected: "%s"', expected);
console.log('found: "%s"', found);
fs.unlinkSync(fn);
});
});
assert.strictEqual(expected, found);
}));
}));
fs.open(fn2, constants.O_CREAT | constants.O_WRONLY | constants.O_TRUNC, 0o644,
function(err, fd) {
common.mustCall(function(err, fd) {
if (err) throw err;
console.log('open done');
fs.write(fd, '', 0, 'utf8', function(err, written) {
assert.equal(0, written);
});
fs.write(fd, expected, 0, 'utf8', function(err, written) {
fs.write(fd, expected, 0, 'utf8', common.mustCall(function(err, written) {
console.log('write done');
if (err) throw err;
assert.equal(Buffer.byteLength(expected), written);
fs.closeSync(fd);
found2 = fs.readFileSync(fn2, 'utf8');
const found = fs.readFileSync(fn2, 'utf8');
console.log('expected: "%s"', expected);
console.log('found: "%s"', found2);
console.log('found: "%s"', found);
fs.unlinkSync(fn2);
});
});
process.on('exit', function() {
assert.equal(expected, found);
assert.equal(expected, found2);
});
assert.strictEqual(expected, found);
}));
}));

View File

@ -1,11 +1,9 @@
'use strict';
require('../common');
const common = require('../common');
var http = require('http');
var assert = require('assert');
var server = http.createServer(function(req, res) {
assert(false); // should not be called
});
var server = http.createServer(common.fail);
server.listen(0, function() {
var req = http.request({

View File

@ -1,7 +1,6 @@
'use strict';
require('../common');
const common = require('../common');
var http = require('http');
var assert = require('assert');
var server = http.Server(function(req, res) {
console.log('Server accepted request.');
@ -11,13 +10,11 @@ var server = http.Server(function(req, res) {
res.destroy();
});
var responseClose = false;
server.listen(0, function() {
server.listen(0, common.mustCall(function() {
http.get({
port: this.address().port,
headers: { connection: 'keep-alive' }
}, function(res) {
}, common.mustCall(function(res) {
server.close();
console.log('Got res: ' + res.statusCode);
@ -41,14 +38,6 @@ server.listen(0, function() {
});
// it would be nice if this worked:
res.on('close', function() {
console.log('Response aborted');
responseClose = true;
});
});
});
process.on('exit', function() {
assert.ok(responseClose);
});
res.on('close', common.mustCall(function() {}));
}));
}));

View File

@ -1,20 +1,11 @@
'use strict';
require('../common');
var assert = require('assert');
const common = require('../common');
var http = require('http');
var url = require('url');
var request = 0;
var response = 0;
process.on('exit', function() {
assert.equal(1, request, 'http server "request" callback was not called');
assert.equal(1, response, 'http client "response" callback was not called');
});
var server = http.createServer(function(req, res) {
var server = http.createServer(common.mustCall(function(req, res) {
res.end();
request++;
}).listen(0, '127.0.0.1', function() {
})).listen(0, '127.0.0.1', common.mustCall(function() {
var opts = url.parse(`http://127.0.0.1:${this.address().port}/`);
// remove the `protocol` field… the `http` module should fall back
@ -22,9 +13,8 @@ var server = http.createServer(function(req, res) {
opts.agent = new http.Agent();
opts.agent.protocol = null;
http.get(opts, function(res) {
response++;
http.get(opts, common.mustCall(function(res) {
res.resume();
server.close();
});
});
}));
}));

View File

@ -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();
});
});
}));
}));

View File

@ -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;
});
});
}));
}));

View File

@ -1,13 +1,10 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var net = require('net');
var gotReq = false;
var server = http.createServer(function(req, res) {
gotReq = true;
var server = http.createServer(common.mustCall(function(req, res) {
assert.equal('GET', req.method);
assert.equal('/blah', req.url);
assert.deepStrictEqual({
@ -15,7 +12,7 @@ var server = http.createServer(function(req, res) {
origin: 'http://mapdevel.trolologames.ru',
cookie: ''
}, req.headers);
});
}));
server.listen(0, function() {
@ -38,8 +35,3 @@ server.listen(0, function() {
server.close();
});
});
process.on('exit', function() {
assert.ok(gotReq);
});

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
@ -42,9 +42,7 @@ var web = http.Server(function(req, res) {
});
});
var gotThanks = false;
web.listen(0, function() {
web.listen(0, common.mustCall(function() {
console.log('Making request');
var req = http.request({
@ -52,19 +50,17 @@ web.listen(0, function() {
method: 'GET',
path: '/',
headers: { 'content-length': buffer.length }
}, function(res) {
}, common.mustCall(function(res) {
console.log('Got response');
res.setEncoding('utf8');
res.on('data', function(string) {
res.on('data', common.mustCall(function(string) {
assert.equal('thanks', string);
gotThanks = true;
});
});
}));
}));
req.end(buffer);
});
}));
process.on('exit', function() {
assert.equal(bufferSize, measuredSize);
assert.ok(gotThanks);
});

View File

@ -1,24 +1,17 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var body = 'hello world\n';
var sawFinish = false;
process.on('exit', function() {
assert(sawFinish);
console.log('ok');
});
var httpServer = http.createServer(function(req, res) {
var httpServer = http.createServer(common.mustCall(function(req, res) {
httpServer.close();
res.on('finish', function() {
sawFinish = true;
res.on('finish', common.mustCall(function() {
assert(typeof req.connection.bytesWritten === 'number');
assert(req.connection.bytesWritten > 0);
});
}));
res.writeHead(200, { 'Content-Type': 'text/plain' });
// Write 1.5mb to cause some requests to buffer
@ -34,7 +27,7 @@ var httpServer = http.createServer(function(req, res) {
assert(res.connection.bytesWritten > 0);
res.end(body);
});
}));
httpServer.listen(0, function() {
http.get({ port: this.address().port });

View File

@ -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);
});
}));

View File

@ -1,24 +1,17 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var seen_req = false;
var server = http.createServer(function(req, res) {
var server = http.createServer(common.mustCall(function(req, res) {
assert.equal('GET', req.method);
assert.equal('/foo?bar', req.url);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('hello\n');
res.end();
server.close();
seen_req = true;
});
}));
server.listen(0, function() {
http.get(`http://127.0.0.1:${this.address().port}/foo?bar`);
});
process.on('exit', function() {
assert(seen_req);
});

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var util = require('util');
@ -39,22 +39,18 @@ FakeAgent.prototype.createConnection = function createConnection() {
};
var received = '';
var ended = 0;
var req = http.request({
agent: new FakeAgent()
}, function(res) {
}, common.mustCall(function(res) {
res.on('data', function(chunk) {
received += chunk;
});
res.on('end', function() {
ended++;
});
});
res.on('end', common.mustCall(function() {}));
}));
req.end();
process.on('exit', function() {
assert.equal(received, 'hello world');
assert.equal(ended, 1);
});

View File

@ -4,13 +4,8 @@ const assert = require('assert');
const http = require('http');
const domain = require('domain');
var gotDomainError = false;
var d;
process.on('exit', function() {
assert(gotDomainError);
});
common.refreshTmpDir();
// first fire up a simple HTTP server
@ -22,15 +17,14 @@ var server = http.createServer(function(req, res) {
server.listen(common.PIPE, function() {
// create a domain
d = domain.create();
d.run(test);
d.run(common.mustCall(test));
});
function test() {
d.on('error', function(err) {
gotDomainError = true;
d.on('error', common.mustCall(function(err) {
assert.equal('should be caught by domain', err.message);
});
}));
var req = http.get({
socketPath: common.PIPE,

View File

@ -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);
});
}));

View File

@ -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);
});
}));

View File

@ -1,11 +1,9 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var net = require('net');
var caughtError = false;
var options = {
host: '127.0.0.1',
port: undefined
@ -16,7 +14,7 @@ var server = net.createServer(function(client) {
client.destroy();
server.close();
});
server.listen(0, options.host, onListen);
server.listen(0, options.host, common.mustCall(onListen));
// do a GET request, expect it to fail
function onListen() {
@ -24,14 +22,8 @@ function onListen() {
var req = http.request(options, function(res) {
assert.ok(false, 'this should never run');
});
req.on('error', function(err) {
req.on('error', common.mustCall(function(err) {
assert.equal(err.code, 'ECONNRESET');
caughtError = true;
});
}));
req.end();
}
process.on('exit', function() {
assert.equal(caughtError, true);
});

View File

@ -3,9 +3,7 @@ const common = require('../common');
const assert = require('assert');
const http = require('http');
const server = http.createServer(function(req, res) {
assert(false);
});
const server = http.createServer(common.fail);
server.on('connect', common.mustCall(function(req, socket, firstBodyChunk) {
assert.equal(req.method, 'CONNECT');
assert.equal(req.url, 'example.com:443');
@ -33,9 +31,7 @@ server.listen(0, common.mustCall(function() {
port: this.address().port,
method: 'CONNECT',
path: 'example.com:443'
}, function(res) {
assert(false);
});
}, common.fail);
req.on('close', common.mustCall(function() { }));

View File

@ -1,14 +1,12 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var serverGotConnect = false;
var clientGotConnect = false;
var server = http.createServer(function(req, res) {
assert(false);
});
var server = http.createServer(common.fail);
server.on('connect', function(req, socket, firstBodyChunk) {
assert.equal(req.method, 'CONNECT');
assert.equal(req.url, 'google.com:443');
@ -30,9 +28,7 @@ server.listen(0, function() {
port: this.address().port,
method: 'CONNECT',
path: 'google.com:443'
}, function(res) {
assert(false);
});
}, common.fail);
var clientRequestClosed = false;
req.on('close', function() {

View File

@ -1,6 +1,5 @@
'use strict';
const common = require('../common');
const assert = require('assert');
// Make sure that throwing in 'end' handler doesn't lock
// up the socket forever.
@ -29,11 +28,4 @@ server.listen(0, common.mustCall(() => {
}
}));
let errors = 0;
process.on('uncaughtException', () => {
errors++;
});
process.on('exit', () => {
assert.equal(errors, 10);
});
process.on('uncaughtException', common.mustCall(() => {}, 10));

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var net = require('net');
@ -20,9 +20,6 @@ var fullResponse =
'\r\n' +
body;
var gotResponse = false;
var server = net.createServer(function(socket) {
var postBody = '';
@ -44,8 +41,8 @@ var server = net.createServer(function(socket) {
});
server.listen(0, function() {
http.get({ port: this.address().port }, function(res) {
server.listen(0, common.mustCall(function() {
http.get({ port: this.address().port }, common.mustCall(function(res) {
var buffer = '';
console.log('Got res code: ' + res.statusCode);
@ -54,17 +51,10 @@ server.listen(0, function() {
buffer += chunk;
});
res.on('end', function() {
res.on('end', common.mustCall(function() {
console.log('Response ended, read ' + buffer.length + ' bytes');
assert.equal(body, buffer);
server.close();
gotResponse = true;
});
});
});
process.on('exit', function() {
assert.ok(gotResponse);
});
}));
}));
}));

View File

@ -17,8 +17,6 @@ var server = http.createServer(function(req, res) {
res.end(body);
});
var runs = 0;
function runAb(opts, callback) {
var command = `ab ${opts} http://127.0.0.1:${server.address().port}/`;
exec(command, function(err, stdout, stderr) {
@ -43,28 +41,21 @@ function runAb(opts, callback) {
assert.equal(bodyLength, documentLength);
assert.equal(completeRequests * documentLength, htmlTransfered);
runs++;
if (callback) callback();
});
}
server.listen(0, function() {
runAb('-c 1 -n 10', function() {
server.listen(0, common.mustCall(function() {
runAb('-c 1 -n 10', common.mustCall(function() {
console.log('-c 1 -n 10 okay');
runAb('-c 1 -n 100', function() {
runAb('-c 1 -n 100', common.mustCall(function() {
console.log('-c 1 -n 100 okay');
runAb('-c 1 -n 1000', function() {
runAb('-c 1 -n 1000', common.mustCall(function() {
console.log('-c 1 -n 1000 okay');
server.close();
});
});
});
});
process.on('exit', function() {
assert.equal(3, runs);
});
}));
}));
}));
}));

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({

View File

@ -1,7 +1,5 @@
'use strict';
require('../common');
var assert = require('assert');
const common = require('../common');
var http = require('http');
// This test is to make sure that when the HTTP server
@ -14,23 +12,16 @@ var server = http.createServer(function(req, res) {
});
server.listen(0);
var responseComplete = false;
server.on('listening', function() {
server.on('listening', common.mustCall(function() {
var req = http.request({
port: this.address().port,
method: 'HEAD',
path: '/'
}, function(res) {
res.on('end', function() {
}, common.mustCall(function(res) {
res.on('end', common.mustCall(function() {
server.close();
responseComplete = true;
});
}));
res.resume();
});
}));
req.end();
});
process.on('exit', function() {
assert.ok(responseComplete);
});
}));

View File

@ -1,7 +1,5 @@
'use strict';
require('../common');
var assert = require('assert');
const common = require('../common');
var http = require('http');
// This test is to make sure that when the HTTP server
@ -14,23 +12,16 @@ var server = http.createServer(function(req, res) {
});
server.listen(0);
var responseComplete = false;
server.on('listening', function() {
server.on('listening', common.mustCall(function() {
var req = http.request({
port: this.address().port,
method: 'HEAD',
path: '/'
}, function(res) {
res.on('end', function() {
}, common.mustCall(function(res) {
res.on('end', common.mustCall(function() {
server.close();
responseComplete = true;
});
}));
res.resume();
});
}));
req.end();
});
process.on('exit', function() {
assert.ok(responseComplete);
});
}));

View File

@ -1,18 +1,10 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var expect = 'hex\nutf8\n';
var data = '';
var ended = false;
process.on('exit', function() {
assert(ended);
assert.equal(data, expect);
console.log('ok');
});
http.createServer(function(q, s) {
s.setHeader('content-length', expect.length);
@ -20,14 +12,17 @@ http.createServer(function(q, s) {
s.write('utf8\n');
s.end();
this.close();
}).listen(0, function() {
http.request({ port: this.address().port }).on('response', function(res) {
}).listen(0, common.mustCall(function() {
http.request({ port: this.address().port })
.on('response', common.mustCall(function(res) {
var data = '';
res.setEncoding('ascii');
res.on('data', function(c) {
data += c;
});
res.on('end', function() {
ended = true;
});
}).end();
});
res.on('end', common.mustCall(function() {
assert.strictEqual(data, expect);
}));
})).end();
}));

View File

@ -1,11 +1,10 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var url = require('url');
var responses_sent = 0;
var responses_recvd = 0;
var body0 = '';
var body1 = '';
@ -37,37 +36,32 @@ var server = http.createServer(function(req, res) {
req.resume();
});
server.listen(0, function() {
server.listen(0, common.mustCall(function() {
var client = http.createClient(this.address().port);
var req = client.request('/hello', {'Accept': '*/*', 'Foo': 'bar'});
setTimeout(function() {
req.end();
}, 100);
req.on('response', function(res) {
req.on('response', common.mustCall(function(res) {
assert.equal(200, res.statusCode);
responses_recvd += 1;
res.setEncoding('utf8');
res.on('data', function(chunk) { body0 += chunk; });
console.error('Got /hello response');
});
}));
setTimeout(function() {
setTimeout(common.mustCall(function() {
var req = client.request('POST', '/world');
req.end();
req.on('response', function(res) {
req.on('response', common.mustCall(function(res) {
assert.equal(200, res.statusCode);
responses_recvd += 1;
res.setEncoding('utf8');
res.on('data', function(chunk) { body1 += chunk; });
console.error('Got /world response');
});
}, 1);
});
}));
}), 1);
}));
process.on('exit', function() {
console.error('responses_recvd: ' + responses_recvd);
assert.equal(2, responses_recvd);
console.error('responses_sent: ' + responses_sent);
assert.equal(2, responses_sent);

View File

@ -1,10 +1,8 @@
'use strict';
const common = require('../common');
var assert = require('assert');
var http = require('http');
var invalidLocalAddress = '1.2.3.4';
var gotError = false;
var server = http.createServer(function(req, res) {
console.log('Connect from: ' + req.connection.remoteAddress);
@ -16,7 +14,7 @@ var server = http.createServer(function(req, res) {
req.resume();
});
server.listen(0, '127.0.0.1', function() {
server.listen(0, '127.0.0.1', common.mustCall(function() {
http.request({
host: 'localhost',
port: this.address().port,
@ -25,13 +23,8 @@ server.listen(0, '127.0.0.1', function() {
localAddress: invalidLocalAddress
}, function(res) {
common.fail('unexpectedly got response from server');
}).on('error', function(e) {
}).on('error', common.mustCall(function(e) {
console.log('client got error: ' + e.message);
gotError = true;
server.close();
}).end();
});
process.on('exit', function() {
assert.ok(gotError);
});
})).end();
}));

View File

@ -1,12 +1,10 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var net = require('net');
var gotResponse = false;
var server = net.createServer(function(conn) {
var body = 'Yet another node.js server.';
@ -24,15 +22,13 @@ var server = net.createServer(function(conn) {
server.close();
});
server.listen(0, function() {
http.get({host: '127.0.0.1', port: this.address().port}, function(res) {
server.listen(0, common.mustCall(function() {
http.get({
host: '127.0.0.1',
port: this.address().port
}, common.mustCall(function(res) {
assert.equal(res.headers['content-type'],
'text/plain; x-unix-mode=0600; name="hello.txt"');
gotResponse = true;
res.destroy();
});
});
process.on('exit', function() {
assert.ok(gotResponse);
});
}));
}));

View File

@ -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');
});
}));
}));
}));

View File

@ -1,6 +1,5 @@
'use strict';
require('../common');
var assert = require('assert');
const common = require('../common');
var http = require('http');
var server = http.Server(function(req, res) {
@ -9,32 +8,20 @@ var server = http.Server(function(req, res) {
server.close();
});
var dataCount = 0, endCount = 0;
server.listen(0, function() {
server.listen(0, common.mustCall(function() {
var opts = {
port: this.address().port,
headers: { connection: 'close' }
};
http.get(opts, function(res) {
res.on('data', function(chunk) {
dataCount++;
http.get(opts, common.mustCall(function(res) {
res.on('data', common.mustCall(function(chunk) {
res.pause();
setTimeout(function() {
res.resume();
});
});
}));
res.on('end', function() {
endCount++;
});
});
});
process.on('exit', function() {
assert.equal(1, dataCount);
assert.equal(1, endCount);
});
res.on('end', common.mustCall(function() {}));
}));
}));

View File

@ -1,6 +1,5 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var http = require('http');
var fs = require('fs');
var path = require('path');
@ -8,17 +7,15 @@ var path = require('path');
common.refreshTmpDir();
var file = path.join(common.tmpDir, 'http-pipe-fs-test.txt');
var requests = 0;
var server = http.createServer(function(req, res) {
++requests;
var server = http.createServer(common.mustCall(function(req, res) {
var stream = fs.createWriteStream(file);
req.pipe(stream);
stream.on('close', function() {
res.writeHead(200);
res.end();
});
}).listen(0, function() {
}, 2)).listen(0, function() {
http.globalAgent.maxSockets = 1;
for (var i = 0; i < 2; ++i) {
@ -45,7 +42,3 @@ var server = http.createServer(function(req, res) {
}(i + 1));
}
});
process.on('exit', function() {
assert.equal(requests, 2);
});

View File

@ -1,6 +1,5 @@
'use strict';
const common = require('../common');
const assert = require('assert');
// Here we are testing the HTTP server module's flood prevention mechanism.
// When writeable.write returns false (ie the underlying send() indicated the
@ -27,7 +26,6 @@ switch (process.argv[2]) {
function parent() {
const http = require('http');
const bigResponse = Buffer.alloc(10240, 'x');
var connections = 0;
var backloggedReqs = 0;
const server = http.createServer(function(req, res) {
@ -48,9 +46,7 @@ function parent() {
res.end();
});
server.on('connection', function(conn) {
connections++;
});
server.on('connection', common.mustCall(function(conn) {}));
server.listen(0, function() {
const spawn = require('child_process').spawn;
@ -64,10 +60,6 @@ function parent() {
child.kill();
}));
});
process.on('exit', function() {
assert.equal(connections, 1);
});
}
function child() {

View File

@ -4,15 +4,17 @@ var assert = require('assert');
var http = require('http');
var expected = 'Post Body For Test';
var result = '';
var server = http.Server(function(req, res) {
var result = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
result += chunk;
});
req.on('end', function() {
assert.strictEqual(expected, result);
server.close();
res.writeHead(200);
res.end('hello world\n');
@ -33,7 +35,3 @@ server.listen(0, function() {
process.exit(1);
}).end(expected);
});
process.on('exit', function() {
assert.equal(expected, result);
});

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var net = require('net');
var http = require('http');
@ -7,20 +7,18 @@ var http = require('http');
// Test that the DELETE, PATCH and PURGE verbs get passed through correctly
['DELETE', 'PATCH', 'PURGE'].forEach(function(method, index) {
var server_response = '';
var received_method = null;
var server = http.createServer(function(req, res) {
received_method = req.method;
var server = http.createServer(common.mustCall(function(req, res) {
assert.strictEqual(req.method, method);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('hello ');
res.write('world\n');
res.end();
});
}));
server.listen(0);
server.on('listening', function() {
server.on('listening', common.mustCall(function() {
var c = net.createConnection(this.address().port);
var server_response = '';
c.setEncoding('utf8');
@ -33,18 +31,14 @@ var http = require('http');
server_response += chunk;
});
c.on('end', function() {
c.on('end', common.mustCall(function() {
const m = server_response.split('\r\n\r\n');
assert.strictEqual(m[1], 'hello world\n');
c.end();
});
}));
c.on('close', function() {
server.close();
});
});
process.on('exit', function() {
var m = server_response.split('\r\n\r\n');
assert.equal(m[1], 'hello world\n');
assert.equal(received_method, method);
});
}));
});

View File

@ -1,29 +1,22 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var responseError;
var server = http.Server(function(req, res) {
res.on('error', function onResError(err) {
responseError = err;
});
var server = http.Server(common.mustCall(function(req, res) {
res.on('error', common.mustCall(function onResError(err) {
assert.strictEqual(err.message, 'write after end');
}));
res.write('This should write.');
res.end();
var r = res.write('This should raise an error.');
assert.equal(r, true, 'write after end should return true');
});
}));
server.listen(0, function() {
http.get({port: this.address().port}, function(res) {
server.close();
});
});
process.on('exit', function onProcessExit(code) {
assert(responseError, 'response should have emitted error');
assert.equal(responseError.message, 'write after end');
});

View File

@ -1,24 +1,18 @@
'use strict';
require('../common');
var assert = require('assert');
const common = require('../common');
var http = require('http');
var requestGotEnd = false;
var responseGotEnd = false;
var server = http.createServer(function(req, res) {
var server = http.createServer(common.mustCall(function(req, res) {
res.writeHead(200);
res.write('a');
req.on('close', function() {
req.on('close', common.mustCall(function() {
console.error('request aborted');
requestGotEnd = true;
});
res.on('close', function() {
}));
res.on('close', common.mustCall(function() {
console.error('response aborted');
responseGotEnd = true;
});
});
}));
}));
server.listen(0);
server.on('listening', function() {
@ -34,8 +28,3 @@ server.on('listening', function() {
});
});
});
process.on('exit', function() {
assert.ok(requestGotEnd);
assert.ok(responseGotEnd);
});

View File

@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
var assert = require('assert');
var http = require('http');
var net = require('net');
@ -10,13 +10,7 @@ var expected = {
'1.1': ''
};
var gotExpected = false;
function test(httpVersion, callback) {
process.on('exit', function() {
assert(gotExpected);
});
var server = net.createServer(function(conn) {
var reply = 'HTTP/' + httpVersion + ' 200 OK\r\n\r\n' +
expected[httpVersion];
@ -24,31 +18,30 @@ function test(httpVersion, callback) {
conn.end(reply);
});
server.listen(0, '127.0.0.1', function() {
server.listen(0, '127.0.0.1', common.mustCall(function() {
var options = {
host: '127.0.0.1',
port: this.address().port
};
var req = http.get(options, function(res) {
var req = http.get(options, common.mustCall(function(res) {
var body = '';
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
res.on('end', common.mustCall(function() {
assert.equal(body, expected[httpVersion]);
gotExpected = true;
server.close();
if (callback) process.nextTick(callback);
});
});
}));
}));
req.on('error', function(err) {
throw err;
});
});
}));
}
test('0.9', function() {

View File

@ -3,10 +3,6 @@ var common = require('../common');
var assert = require('assert');
var http = require('http');
var status_ok = false; // status code == 200?
var headers_ok = false;
var body_ok = false;
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain',
@ -19,19 +15,16 @@ var server = http.createServer(function(req, res) {
common.refreshTmpDir();
server.listen(common.PIPE, function() {
server.listen(common.PIPE, common.mustCall(function() {
var options = {
socketPath: common.PIPE,
path: '/'
};
var req = http.get(options, function(res) {
var req = http.get(options, common.mustCall(function(res) {
assert.equal(res.statusCode, 200);
status_ok = true;
assert.equal(res.headers['content-type'], 'text/plain');
headers_ok = true;
res.body = '';
res.setEncoding('utf8');
@ -40,17 +33,16 @@ server.listen(common.PIPE, function() {
res.body += chunk;
});
res.on('end', function() {
res.on('end', common.mustCall(function() {
assert.equal(res.body, 'hello world\n');
body_ok = true;
server.close(function(error) {
assert.equal(error, undefined);
server.close(function(error) {
assert.equal(error && error.message, 'Not running');
});
});
});
});
}));
}));
req.on('error', function(e) {
console.log(e.stack);
@ -59,10 +51,4 @@ server.listen(common.PIPE, function() {
req.end();
});
process.on('exit', function() {
assert.ok(status_ok);
assert.ok(headers_ok);
assert.ok(body_ok);
});
}));

Some files were not shown because too many files have changed in this diff Show More