Skip to content

Commit

Permalink
test: add common.noop, default for common.mustCall()
Browse files Browse the repository at this point in the history
Export a new common.noop no-operation function for general use.
Allow using common.mustCall() without a fn argument to simplify
test cases.

Replace various non-op functions throughout tests with common.noop

PR-URL: #12027
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
  • Loading branch information
jasnell authored and MylesBorins committed Aug 16, 2017
1 parent e79c054 commit a7b9450
Show file tree
Hide file tree
Showing 98 changed files with 236 additions and 225 deletions.
2 changes: 1 addition & 1 deletion test/addons/async-hello-world/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ const binding = require(`./build/${common.buildType}/binding`);
binding(5, common.mustCall(function(err, val) {
assert.strictEqual(err, null);
assert.strictEqual(val, 10);
process.nextTick(common.mustCall(function() {}));
process.nextTick(common.mustCall());
}));
16 changes: 13 additions & 3 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const Timer = process.binding('timer_wrap').Timer;
const testRoot = process.env.NODE_TEST_DIR ?
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');

const noop = () => {};

exports.noop = noop;
exports.fixturesDir = path.join(__dirname, '..', 'fixtures');
exports.tmpDirName = 'tmp';
// PORT should match the definition in test/testpy/__init__.py.
Expand Down Expand Up @@ -419,6 +422,13 @@ function runCallChecks(exitCode) {


exports.mustCall = function(fn, expected) {
if (typeof fn === 'number') {
expected = fn;
fn = noop;
} else if (fn === undefined) {
fn = noop;
}

if (expected === undefined)
expected = 1;
else if (typeof expected !== 'number')
Expand Down Expand Up @@ -487,9 +497,9 @@ util.inherits(ArrayStream, stream.Stream);
exports.ArrayStream = ArrayStream;
ArrayStream.prototype.readable = true;
ArrayStream.prototype.writable = true;
ArrayStream.prototype.pause = function() {};
ArrayStream.prototype.resume = function() {};
ArrayStream.prototype.write = function() {};
ArrayStream.prototype.pause = noop;
ArrayStream.prototype.resume = noop;
ArrayStream.prototype.write = noop;

// Returns true if the exit code "exitCode" and/or signal name "signal"
// represent the exit code and/or signal name of a node process that aborted,
Expand Down
4 changes: 2 additions & 2 deletions test/debugger/test-debugger-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ addTest(function(client, done) {

let connectCount = 0;
const script = 'setTimeout(function() { console.log("blah"); });' +
'setInterval(function() {}, 1000000);';
'setInterval(common.noop, 1000000);';

let nodeProcess;

Expand Down Expand Up @@ -172,7 +172,7 @@ function doTest(cb, done) {
console.error('>>> connecting...');
c.connect(debug.port);
c.on('break', function() {
c.reqContinue(function() {});
c.reqContinue(common.noop);
});
c.on('ready', function() {
connectCount++;
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-assert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const a = require('assert');

Expand Down Expand Up @@ -515,6 +515,7 @@ testAssertionMessage(/a/, '/a/');
testAssertionMessage(/abc/gim, '/abc/gim');
testAssertionMessage(function f() {}, '[Function: f]');
testAssertionMessage(function() {}, '[Function]');
testAssertionMessage(common.noop, '[Function: noop]');
testAssertionMessage({}, '{}');
testAssertionMessage(circular, '{ y: 1, x: [Circular] }');
testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-disconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if (process.argv[2] === 'child') {
}));

// the process should also self terminate without using signals
child.on('exit', common.mustCall(function() {}));
child.on('exit', common.mustCall());

// when child is listening
child.on('message', function(obj) {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const assert = require('assert');
const spawn = require('child_process').spawn;
const cat = spawn(common.isWindows ? 'cmd' : 'cat');

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

cat.on('exit', common.mustCall(function(code, signal) {
assert.strictEqual(code, null);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ cat.stdout.on('data', function(chunk) {
response += chunk;
});

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

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

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

cat.on('exit', common.mustCall(function(status) {
assert.strictEqual(0, status);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-cluster-eaccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ if (cluster.isMaster) {
const worker = cluster.fork();

// makes sure master is able to fork the worker
cluster.on('fork', common.mustCall(function() {}));
cluster.on('fork', common.mustCall());

// makes sure the worker is ready
worker.on('online', common.mustCall(function() {}));
worker.on('online', common.mustCall());

worker.on('message', common.mustCall(function(err) {
// disconnect first, so that we will not leave zombies
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-cluster-setup-master-emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ function emitAndCatch2(next) {
}

emitAndCatch(common.mustCall(function() {
emitAndCatch2(common.mustCall(function() {}));
emitAndCatch2(common.mustCall());
}));
4 changes: 2 additions & 2 deletions test/parallel/test-cluster-worker-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ if (cluster.isMaster) {
worker2 = cluster.fork();

[worker1, worker2].forEach(function(worker) {
worker.on('disconnect', common.mustCall(function() {}));
worker.on('exit', common.mustCall(function() {}));
worker.on('disconnect', common.mustCall());
worker.on('exit', common.mustCall());
});
} else {
if (cluster.worker.id === 1) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-cluster-worker-wait-server-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (cluster.isWorker) {

// Although not typical, the worker process can exit before the disconnect
// event fires. Use this to keep the process open until the event has fired.
const keepOpen = setInterval(function() {}, 9999);
const keepOpen = setInterval(common.noop, 9999);

// Check worker events and properties
process.once('disconnect', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ global.gc = 42; // Not a valid global unless --expose_gc is set.
assert.deepStrictEqual(common.leakedGlobals(), ['gc']);

assert.throws(function() {
common.mustCall(function() {}, 'foo');
common.mustCall(common.noop, 'foo');
}, /^TypeError: Invalid expected value: foo$/);

assert.throws(function() {
common.mustCall(function() {}, /foo/);
common.mustCall(common.noop, /foo/);
}, /^TypeError: Invalid expected value: \/foo\/$/);
2 changes: 1 addition & 1 deletion test/parallel/test-crypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach(function(f) {
[-1, undefined, null, false, true, {}, []].forEach(function(value) {
assert.throws(function() { f(value); }, expectedErrorRegexp);
assert.throws(function() { f(value, function() {}); }, expectedErrorRegexp);
assert.throws(function() { f(value, common.noop); }, expectedErrorRegexp);
});

[0, 1, 2, 4, 16, 256, 1024].forEach(function(len) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-dgram-close-is-not-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ const portGetter = dgram.createSocket('udp4')
socket.close('bad argument');
portGetter.close();

socket.on('close', common.mustCall(function() {}));
socket.on('close', common.mustCall());
}));
4 changes: 2 additions & 2 deletions test/parallel/test-dgram-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const portGetter = dgram.createSocket('udp4')
portGetter.address().port,
portGetter.address().address);

assert.strictEqual(socket.close(common.mustCall(function() {})), socket);
socket.on('close', common.mustCall(function() {}));
assert.strictEqual(socket.close(common.mustCall()), socket);
socket.on('close', common.mustCall());
socket = null;

// Verify that accessing handle after closure doesn't throw
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-dgram-send-empty-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ client.bind(0, common.mustCall(function() {

const buf = Buffer.alloc(0);
let interval = setInterval(function() {
client.send(buf, 0, 0, port, '127.0.0.1', common.mustCall(function() {}));
client.send(buf, 0, 0, port, '127.0.0.1', common.mustCall());
}, 10);
}));
6 changes: 3 additions & 3 deletions test/parallel/test-domain-crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ global.domain = require('domain');

// should not throw a 'TypeError: undefined is not a function' exception
crypto.randomBytes(8);
crypto.randomBytes(8, function() {});
crypto.randomBytes(8, common.noop);
crypto.pseudoRandomBytes(8);
crypto.pseudoRandomBytes(8, function() {});
crypto.pbkdf2('password', 'salt', 8, 8, function() {});
crypto.pseudoRandomBytes(8, common.noop);
crypto.pbkdf2('password', 'salt', 8, 8, common.noop);
2 changes: 1 addition & 1 deletion test/parallel/test-domain-exit-dispose.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function err() {
function err2() {
// this timeout should never be called, since the domain gets
// disposed when the error happens.
setTimeout(common.mustCall(() => {}, 0), 1);
setTimeout(common.mustNotCall(), 1);

// this function doesn't exist, and throws an error as a result.
err3(); // eslint-disable-line no-undef
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-domain-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ immediated.run(function() {
});
});

timeout = setTimeout(function() {}, 10 * 1000);
timeout = setTimeout(common.noop, 10 * 1000);
4 changes: 2 additions & 2 deletions test/parallel/test-domain.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// Simple tests of most basic domain functionality.

require('../common');
const common = require('../common');
const assert = require('assert');
const domain = require('domain');
const events = require('events');
Expand Down Expand Up @@ -238,7 +238,7 @@ let fst = fs.createReadStream('stream for nonexistent file');
d.add(fst);
expectCaught++;

[42, null, , false, function() {}, 'string'].forEach(function(something) {
[42, null, , false, common.noop, 'string'].forEach(function(something) {
const d = new domain.Domain();
d.run(function() {
process.nextTick(function() {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-env-var-no-warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if (process.argv[2] === 'child') {
test({ NODE_NO_WARNINGS: false });
test({ NODE_NO_WARNINGS: {} });
test({ NODE_NO_WARNINGS: [] });
test({ NODE_NO_WARNINGS: function() {} });
test({ NODE_NO_WARNINGS: common.noop });
test({ NODE_NO_WARNINGS: 0 });
test({ NODE_NO_WARNINGS: -1 });
test({ NODE_NO_WARNINGS: '0' });
Expand Down
32 changes: 16 additions & 16 deletions test/parallel/test-event-emitter-check-listener-leaks.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const events = require('events');

let e = new events.EventEmitter();

// default
for (let i = 0; i < 10; i++) {
e.on('default', function() {});
e.on('default', common.noop);
}
assert.ok(!e._events['default'].hasOwnProperty('warned'));
e.on('default', function() {});
e.on('default', common.noop);
assert.ok(e._events['default'].warned);

// symbol
const symbol = Symbol('symbol');
e.setMaxListeners(1);
e.on(symbol, function() {});
e.on(symbol, common.noop);
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
e.on(symbol, function() {});
e.on(symbol, common.noop);
assert.ok(e._events[symbol].hasOwnProperty('warned'));

// specific
e.setMaxListeners(5);
for (let i = 0; i < 5; i++) {
e.on('specific', function() {});
e.on('specific', common.noop);
}
assert.ok(!e._events['specific'].hasOwnProperty('warned'));
e.on('specific', function() {});
e.on('specific', common.noop);
assert.ok(e._events['specific'].warned);

// only one
e.setMaxListeners(1);
e.on('only one', function() {});
e.on('only one', common.noop);
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
e.on('only one', function() {});
e.on('only one', common.noop);
assert.ok(e._events['only one'].hasOwnProperty('warned'));

// unlimited
e.setMaxListeners(0);
for (let i = 0; i < 1000; i++) {
e.on('unlimited', function() {});
e.on('unlimited', common.noop);
}
assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));

Expand All @@ -49,26 +49,26 @@ events.EventEmitter.defaultMaxListeners = 42;
e = new events.EventEmitter();

for (let i = 0; i < 42; ++i) {
e.on('fortytwo', function() {});
e.on('fortytwo', common.noop);
}
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
e.on('fortytwo', function() {});
e.on('fortytwo', common.noop);
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
delete e._events['fortytwo'].warned;

events.EventEmitter.defaultMaxListeners = 44;
e.on('fortytwo', function() {});
e.on('fortytwo', common.noop);
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
e.on('fortytwo', function() {});
e.on('fortytwo', common.noop);
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));

// but _maxListeners still has precedence over defaultMaxListeners
events.EventEmitter.defaultMaxListeners = 42;
e = new events.EventEmitter();
e.setMaxListeners(1);
e.on('uno', function() {});
e.on('uno', common.noop);
assert.ok(!e._events['uno'].hasOwnProperty('warned'));
e.on('uno', function() {});
e.on('uno', common.noop);
assert.ok(e._events['uno'].hasOwnProperty('warned'));

// chainable
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-event-emitter-get-max-listeners.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');

Expand All @@ -15,5 +15,5 @@ assert.strictEqual(emitter.getMaxListeners(), 3);

// https://github.com/nodejs/node/issues/523 - second call should not throw.
const recv = {};
EventEmitter.prototype.on.call(recv, 'event', function() {});
EventEmitter.prototype.on.call(recv, 'event', function() {});
EventEmitter.prototype.on.call(recv, 'event', common.noop);
EventEmitter.prototype.on.call(recv, 'event', common.noop);
10 changes: 5 additions & 5 deletions test/parallel/test-event-emitter-listener-count.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');

const emitter = new EventEmitter();
emitter.on('foo', function() {});
emitter.on('foo', function() {});
emitter.on('baz', function() {});
emitter.on('foo', common.noop);
emitter.on('foo', common.noop);
emitter.on('baz', common.noop);
// Allow any type
emitter.on(123, function() {});
emitter.on(123, common.noop);

assert.strictEqual(EventEmitter.listenerCount(emitter, 'foo'), 2);
assert.strictEqual(emitter.listenerCount('foo'), 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ process.on('warning', common.mustCall((warning) => {
assert.ok(warning.message.includes('2 null listeners added.'));
}));

e.on(null, function() {});
e.on(null, function() {});
e.on(null, common.noop);
e.on(null, common.noop);
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ process.on('warning', common.mustCall((warning) => {
assert.ok(warning.message.includes('2 Symbol(symbol) listeners added.'));
}));

e.on(symbol, function() {});
e.on(symbol, function() {});
e.on(symbol, common.noop);
e.on(symbol, common.noop);
Loading

0 comments on commit a7b9450

Please sign in to comment.