Skip to content

Commit

Permalink
test: add common.mustNotCall()
Browse files Browse the repository at this point in the history
This commit adds a mustNotCall() helper for testing. This provides
an alternative to using common.fail() as a callback, or creating
a callback function for the sole purpose of calling common.fail().

PR-URL: nodejs#11152
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Santiago Gimeno <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
  • Loading branch information
cjihrig authored and italoacasas committed Feb 14, 2017
1 parent 44b17a2 commit f0eba78
Show file tree
Hide file tree
Showing 124 changed files with 243 additions and 234 deletions.
6 changes: 6 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@ function fail(msg) {
}
exports.fail = fail;

exports.mustNotCall = function(msg) {
return function mustNotCall() {
fail(msg || 'function should not have been called');
};
};

exports.skip = function(msg) {
console.log(`1..0 # Skipped: ${msg}`);
};
Expand Down
4 changes: 2 additions & 2 deletions test/debugger/test-debugger-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const interfacer = spawn(process.execPath, ['debug', `localhost:${PORT}`]);
interfacer.stdout.setEncoding('utf-8');

// fail the test if either of the processes exit normally
const debugBreakExit = common.fail.bind(null, 'child should not exit normally');
const debugExit = common.fail.bind(null, 'interfacer should not exit normally');
const debugBreakExit = common.mustNotCall('child should not exit normally');
const debugExit = common.mustNotCall('interfacer should not exit normally');
child.on('exit', debugBreakExit);
interfacer.on('exit', debugExit);

Expand Down
5 changes: 3 additions & 2 deletions test/inspector/inspector-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ function makeBufferingDataCallback(dataCallback) {
}

function timeout(message, multiplicator) {
return setTimeout(() => common.fail(message), TIMEOUT * (multiplicator || 1));
return setTimeout(common.mustNotCall(message),
TIMEOUT * (multiplicator || 1));
}

const TestSession = function(socket, harness) {
Expand Down Expand Up @@ -398,7 +399,7 @@ Harness.prototype.wsHandshake = function(devtoolsUrl, tests, readyCallback) {
});
}
enqueue(tests);
}).on('response', () => common.fail('Upgrade was not received'));
}).on('response', common.mustNotCall('Upgrade was not received'));
};

Harness.prototype.runFrontendSession = function(tests) {
Expand Down
2 changes: 1 addition & 1 deletion test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function checkWrap(req) {

TEST(function test_reverse_bogus(done) {
assert.throws(() => {
dns.reverse('bogus ip', common.fail);
dns.reverse('bogus ip', common.mustNotCall());
}, /^Error: getHostByAddr EINVAL$/);
done();
});
Expand Down
2 changes: 1 addition & 1 deletion test/internet/test-http-dns-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function httpreq(count) {
port: 80,
path: '/',
method: 'GET'
}, common.fail);
}, common.mustNotCall());

req.on('error', common.mustCall((e) => {
assert.strictEqual(e.code, 'ENOTFOUND');
Expand Down
2 changes: 1 addition & 1 deletion test/internet/test-net-connect-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ socket.on('timeout', common.mustCall(function() {
socket.destroy();
}));

socket.on('connect', common.fail);
socket.on('connect', common.mustNotCall());
4 changes: 2 additions & 2 deletions test/internet/test-net-connect-unref.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ const client = net.createConnection(53, '8.8.8.8', function() {
client.unref();
});

client.on('close', common.fail);
client.on('close', common.mustNotCall());

setTimeout(common.fail, TIMEOUT).unref();
setTimeout(common.mustNotCall(), TIMEOUT).unref();
4 changes: 2 additions & 2 deletions test/parallel/test-async-wrap-throw-from-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ if (typeof process.argv[2] === 'string') {
async_wrap.setupHooks({ init, pre, post, destroy });
async_wrap.enable();

process.on('uncaughtException', common.fail);
process.on('uncaughtException', common.mustNotCall());

const d = domain.create();
d.on('error', common.fail);
d.on('error', common.mustNotCall());
d.run(() => {
// Using randomBytes because timers are not yet supported.
crypto.randomBytes(0, () => { });
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const spawn = require('child_process').spawn;
const cat = spawn(common.isWindows ? 'cmd' : 'cat');

cat.stdout.on('end', common.mustCall(function() {}));
cat.stderr.on('data', common.fail);
cat.stderr.on('data', common.mustNotCall());
cat.stderr.on('end', common.mustCall(function() {}));

cat.on('exit', common.mustCall(function(code, signal) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-recv-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function master() {
});
proc.stdout.on('data', common.mustCall((data) => {
assert.strictEqual(data.toString(), 'ok\r\n');
net.createServer(common.fail).listen(0, function() {
net.createServer(common.mustNotCall()).listen(0, function() {
handle = this._handle;
proc.send('one');
proc.send('two', handle);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-send-returns-boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ s.on('exit', function() {
handle.close();
});

net.createServer(common.fail).listen(0, function() {
net.createServer(common.mustNotCall()).listen(0, function() {
handle = this._handle;
assert.strictEqual(s.send('one', handle), true);
assert.strictEqual(s.send('two', handle), true);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-spawn-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const cp = require('child_process');
const doesNotExist = cp.spawn('does-not-exist', {shell: true});

assert.notStrictEqual(doesNotExist.spawnfile, 'does-not-exist');
doesNotExist.on('error', common.fail);
doesNotExist.on('error', common.mustNotCall());
doesNotExist.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(signal, null);

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-spawn-typeerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const empty = common.fixturesDir + '/empty.js';

assert.throws(function() {
const child = spawn(invalidcmd, 'this is not an array');
child.on('error', common.fail);
child.on('error', common.mustNotCall());
}, TypeError);

// verify that valid argument combinations do not throw
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cat.stdout.on('data', function(chunk) {

cat.stdout.on('end', common.mustCall(function() {}));

cat.stderr.on('data', common.fail);
cat.stderr.on('data', common.mustNotCall());

cat.stderr.on('end', common.mustCall(function() {}));

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-stdout-flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const child = spawn(process.argv[0], [sub, n]);
let count = 0;

child.stderr.setEncoding('utf8');
child.stderr.on('data', common.fail);
child.stderr.on('data', common.mustNotCall());

child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-cluster-bind-privileged-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ if (cluster.isMaster) {
assert.strictEqual(exitCode, 0);
}));
} else {
const s = net.createServer(common.fail);
s.listen(42, common.fail.bind(null, 'listen should have failed'));
const s = net.createServer(common.mustNotCall());
s.listen(42, common.mustNotCall('listen should have failed'));
s.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'EACCES');
process.disconnect();
Expand Down
11 changes: 6 additions & 5 deletions test/parallel/test-cluster-bind-twice.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,22 @@ if (!id) {
} else if (id === 'one') {
if (cluster.isMaster) return startWorker();

http.createServer(common.fail).listen(common.PORT, common.mustCall(() => {
process.send('READY');
}));
http.createServer(common.mustNotCall())
.listen(common.PORT, common.mustCall(() => {
process.send('READY');
}));

process.on('message', common.mustCall((m) => {
if (m === 'QUIT') process.exit();
}));
} else if (id === 'two') {
if (cluster.isMaster) return startWorker();

const server = http.createServer(common.fail);
const server = http.createServer(common.mustNotCall());
process.on('message', common.mustCall((m) => {
if (m === 'QUIT') process.exit();
assert.strictEqual(m, 'START');
server.listen(common.PORT, common.fail);
server.listen(common.PORT, common.mustNotCall());
server.on('error', common.mustCall((e) => {
assert.strictEqual(e.code, 'EADDRINUSE');
process.send(e.code);
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-cluster-eaddrinuse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const net = require('net');
const id = '' + process.argv[2];

if (id === 'undefined') {
const server = net.createServer(common.fail);
const server = net.createServer(common.mustNotCall());
server.listen(common.PORT, function() {
const worker = fork(__filename, ['worker']);
worker.on('message', function(msg) {
Expand All @@ -22,14 +22,14 @@ if (id === 'undefined') {
});
});
} else if (id === 'worker') {
let server = net.createServer(common.fail);
server.listen(common.PORT, common.fail);
let server = net.createServer(common.mustNotCall());
server.listen(common.PORT, common.mustNotCall());
server.on('error', common.mustCall(function(e) {
assert(e.code, 'EADDRINUSE');
process.send('stop-listening');
process.once('message', function(msg) {
if (msg !== 'stopped-listening') return;
server = net.createServer(common.fail);
server = net.createServer(common.mustNotCall());
server.listen(common.PORT, common.mustCall(function() {
server.close();
}));
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-cluster-listening-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ if (cluster.isMaster) {
worker.kill();
}));
} else {
net.createServer(common.fail).listen(0);
net.createServer(common.mustNotCall()).listen(0);
}
2 changes: 1 addition & 1 deletion test/parallel/test-cluster-net-listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ if (cluster.isMaster) {
}));
} else {
// listen() without port should not trigger a libuv assert
net.createServer(common.fail).listen(process.exit);
net.createServer(common.mustNotCall()).listen(process.exit);
}
2 changes: 1 addition & 1 deletion test/parallel/test-cluster-rr-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (cluster.isMaster) {
if (msg === 'done') this.kill();
});
} else {
const server = net.createServer(common.fail);
const server = net.createServer(common.mustNotCall());
server.listen(common.PORT, function() {
server.unref();
server.ref();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-cluster-setup-master-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');

setTimeout(common.fail.bind(assert, 'setup not emitted'), 1000).unref();
setTimeout(common.mustNotCall('setup not emitted'), 1000).unref();

cluster.on('setup', common.mustCall(function() {
const clusterArgs = cluster.settings.args;
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-cluster-shared-handle-bind-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (cluster.isMaster) {
// Master opens and binds the socket and shares it with the worker.
cluster.schedulingPolicy = cluster.SCHED_NONE;
// Hog the TCP port so that when the worker tries to bind, it'll fail.
const server = net.createServer(common.fail);
const server = net.createServer(common.mustNotCall());

server.listen(common.PORT, common.mustCall(() => {
const worker = cluster.fork();
Expand All @@ -18,8 +18,8 @@ if (cluster.isMaster) {
}));
}));
} else {
const s = net.createServer(common.fail);
s.listen(common.PORT, common.fail.bind(null, 'listen should have failed'));
const s = net.createServer(common.mustNotCall());
s.listen(common.PORT, common.mustNotCall('listen should have failed'));
s.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'EADDRINUSE');
process.disconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ if (cluster.isMaster) {
assert.strictEqual(exitCode, 0);
}));
} else {
const s = net.createServer(common.fail);
s.listen(42, common.fail.bind(null, 'listen should have failed'));
const s = net.createServer(common.mustNotCall());
s.listen(42, common.mustNotCall('listen should have failed'));
s.on('error', common.mustCall(function(err) {
assert.strictEqual(err.code, 'EACCES');
process.disconnect();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-console-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const err = new Stream();

// ensure the Console instance doesn't write to the
// process' "stdout" or "stderr" streams
process.stdout.write = process.stderr.write = common.fail;
process.stdout.write = process.stderr.write = common.mustNotCall();

// make sure that the "Console" function exists
assert.strictEqual('function', typeof Console);
Expand Down
13 changes: 8 additions & 5 deletions test/parallel/test-crypto-pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,30 @@ assert.throws(function() {

// Should not work with Infinity key length
assert.throws(function() {
crypto.pbkdf2('password', 'salt', 1, Infinity, 'sha256', common.fail);
crypto.pbkdf2('password', 'salt', 1, Infinity, 'sha256',
common.mustNotCall());
}, /^TypeError: Bad key length$/);

// Should not work with negative Infinity key length
assert.throws(function() {
crypto.pbkdf2('password', 'salt', 1, -Infinity, 'sha256', common.fail);
crypto.pbkdf2('password', 'salt', 1, -Infinity, 'sha256',
common.mustNotCall());
}, /^TypeError: Bad key length$/);

// Should not work with NaN key length
assert.throws(function() {
crypto.pbkdf2('password', 'salt', 1, NaN, 'sha256', common.fail);
crypto.pbkdf2('password', 'salt', 1, NaN, 'sha256', common.mustNotCall());
}, /^TypeError: Bad key length$/);

// Should not work with negative key length
assert.throws(function() {
crypto.pbkdf2('password', 'salt', 1, -1, 'sha256', common.fail);
crypto.pbkdf2('password', 'salt', 1, -1, 'sha256', common.mustNotCall());
}, /^TypeError: Bad key length$/);

// Should not work with key length that does not fit into 32 signed bits
assert.throws(function() {
crypto.pbkdf2('password', 'salt', 1, 4073741824, 'sha256', common.fail);
crypto.pbkdf2('password', 'salt', 1, 4073741824, 'sha256',
common.mustNotCall());
}, /^TypeError: Bad key length$/);

// Should not get FATAL ERROR with empty password and salt
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-crypto-verify-failure.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ server.listen(0, common.mustCall(() => {
}, common.mustCall(() => {
verify();
}))
.on('error', common.fail)
.on('error', common.mustNotCall())
.on('close', common.mustCall(() => {
server.close();
})).resume();
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-dgram-error-message-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const dgram = require('dgram');
// IPv4 Test
const socket_ipv4 = dgram.createSocket('udp4');

socket_ipv4.on('listening', common.fail);
socket_ipv4.on('listening', common.mustNotCall());

socket_ipv4.on('error', common.mustCall(function(e) {
assert.strictEqual(e.port, undefined);
Expand All @@ -21,7 +21,7 @@ socket_ipv4.bind(0, '1.1.1.1');
// IPv6 Test
const socket_ipv6 = dgram.createSocket('udp6');

socket_ipv6.on('listening', common.fail);
socket_ipv6.on('listening', common.mustNotCall());

socket_ipv6.on('error', common.mustCall(function(e) {
// EAFNOSUPPORT or EPROTONOSUPPORT means IPv6 is disabled on this system.
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-dgram-unref.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ const s = dgram.createSocket('udp4');
s.bind();
s.unref();

setTimeout(common.fail, 1000).unref();
setTimeout(common.mustNotCall(), 1000).unref();
2 changes: 1 addition & 1 deletion test/parallel/test-domain-multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const http = require('http');
const a = domain.create();
a.enter(); // this will be our "root" domain

a.on('error', common.fail);
a.on('error', common.mustNotCall());

const server = http.createServer((req, res) => {
// child domain of a.
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-event-emitter-remove-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function listener2() {}
{
const ee = new EventEmitter();
ee.on('hello', listener1);
ee.on('removeListener', common.fail);
ee.on('removeListener', common.mustNotCall());
ee.removeListener('hello', listener2);
assert.deepStrictEqual([listener1], ee.listeners('hello'));
}
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-force-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const spawn = require('child_process').spawn;

// spawn a node child process in "interactive" mode (force the repl)
const cp = spawn(process.execPath, ['-i']);
const timeoutId = setTimeout(function() {
common.fail('timeout!');
}, common.platformTimeout(5000)); // give node + the repl 5 seconds to start
// give node + the repl 5 seconds to start
const timeoutId = setTimeout(common.mustNotCall(),
common.platformTimeout(5000));

cp.stdout.setEncoding('utf8');

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fs.access(readOnlyFile, fs.W_OK, common.mustCall((err) => {
}));

assert.throws(() => {
fs.access(100, fs.F_OK, () => { common.fail('callback should not run'); });
fs.access(100, fs.F_OK, common.mustNotCall());
}, /^TypeError: path must be a string or Buffer$/);

assert.throws(() => {
Expand Down
Loading

0 comments on commit f0eba78

Please sign in to comment.