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 <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Teddy Katz <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
  • Loading branch information
jasnell committed Mar 26, 2017
1 parent d13bd4a commit 4f2e372
Show file tree
Hide file tree
Showing 184 changed files with 492 additions and 490 deletions.
16 changes: 15 additions & 1 deletion test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ Gets IP of localhost

Array of IPV6 hosts.

### mustCall(fn[, expected])
### mustCall([fn][, expected])
* fn [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
* expected [&lt;Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1
* return [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
Expand All @@ -333,13 +333,27 @@ Returns a function that calls `fn`. If the returned function has not been called
exactly `expected` number of times when the test is complete, then the test will
fail.

If `fn` is not provided, `common.noop` will be used.

### nodeProcessAborted(exitCode, signal)
* `exitCode` [&lt;Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)
* `signal` [&lt;String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
* return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)

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, `false` otherwise.

### noop

A non-op `Function` that can be used for a variety of scenarios.

For instance,

```js
const common = require('../common');

someAsyncAPI('foo', common.mustCall(common.noop));
```

### opensslCli
* return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)

Expand Down
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());
}));
2 changes: 1 addition & 1 deletion test/addons/heap-profiler/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const common = require('../../common');
const binding = require(`./build/${common.buildType}/binding`);

// Create an AsyncWrap object.
const timer = setTimeout(function() {}, 1);
const timer = setTimeout(common.noop, 1);
timer.unref();

// Stress-test the heap profiler.
Expand Down
16 changes: 13 additions & 3 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const execSync = require('child_process').execSync;
const testRoot = process.env.NODE_TEST_DIR ?
fs.realpathSync(process.env.NODE_TEST_DIR) : __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 @@ -429,6 +432,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 @@ -525,9 +535,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 @@ -150,7 +150,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 @@ -193,7 +193,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
4 changes: 2 additions & 2 deletions test/message/unhandled_promise_trace_warnings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Flags: --trace-warnings
'use strict';
require('../common');
const common = require('../common');
const p = Promise.reject(new Error('This was rejected'));
setImmediate(() => p.catch(() => {}));
setImmediate(() => p.catch(common.noop));
10 changes: 5 additions & 5 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const a = require('assert');

Expand Down Expand Up @@ -505,22 +505,22 @@ a.throws(makeBlock(a.deepEqual, args, []));
// check messages from assert.throws()
{
assert.throws(
() => { a.throws(() => {}); },
() => { a.throws(common.noop); },
/^AssertionError: Missing expected exception\.$/
);

assert.throws(
() => { a.throws(() => {}, TypeError); },
() => { a.throws(common.noop, TypeError); },
/^AssertionError: Missing expected exception \(TypeError\)\.$/
);

assert.throws(
() => { a.throws(() => {}, 'fhqwhgads'); },
() => { a.throws(common.noop, 'fhqwhgads'); },
/^AssertionError: Missing expected exception: fhqwhgads$/
);

assert.throws(
() => { a.throws(() => {}, TypeError, 'fhqwhgads'); },
() => { a.throws(common.noop, TypeError, 'fhqwhgads'); },
/^AssertionError: Missing expected exception \(TypeError\): fhqwhgads$/
);
}
Expand Down
22 changes: 10 additions & 12 deletions test/parallel/test-async-wrap-check-providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,48 @@ function init(id, provider) {
keyList = keyList.filter((e) => e !== pkeys[provider]);
}

function noop() { }

async_wrap.setupHooks({ init });

async_wrap.enable();


setTimeout(function() { }, 1);
setTimeout(common.noop, 1);

fs.stat(__filename, noop);
fs.stat(__filename, common.noop);

if (!common.isAix) {
// fs-watch currently needs special configuration on AIX and we
// want to improve under https://github.com/nodejs/node/issues/5085.
// strip out fs watch related parts for now
fs.watchFile(__filename, noop);
fs.watchFile(__filename, common.noop);
fs.unwatchFile(__filename);
fs.watch(__filename).close();
}

dns.lookup('localhost', noop);
dns.lookupService('::', 0, noop);
dns.resolve('localhost', noop);
dns.lookup('localhost', common.noop);
dns.lookupService('::', 0, common.noop);
dns.resolve('localhost', common.noop);

new StreamWrap(new net.Socket());

new (process.binding('tty_wrap').TTY)();

crypto.randomBytes(1, noop);
crypto.randomBytes(1, common.noop);

common.refreshTmpDir();

net.createServer(function(c) {
c.end();
this.close();
}).listen(common.PIPE, function() {
net.connect(common.PIPE, noop);
net.connect(common.PIPE, common.noop);
});

net.createServer(function(c) {
c.end();
this.close(checkTLS);
}).listen(0, function() {
net.connect(this.address().port, noop);
net.connect(this.address().port, common.noop);
});

dgram.createSocket('udp4').bind(0, function() {
Expand All @@ -99,7 +97,7 @@ function checkTLS() {
key: fs.readFileSync(common.fixturesDir + '/keys/ec-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/ec-cert.pem')
};
const server = tls.createServer(options, noop)
const server = tls.createServer(options, common.noop)
.listen(0, function() {
const connectOpts = { rejectUnauthorized: false };
tls.connect(this.address().port, connectOpts, function() {
Expand Down
6 changes: 2 additions & 4 deletions test/parallel/test-async-wrap-disabled-propagate-parent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
const async_wrap = process.binding('async_wrap');
Expand Down Expand Up @@ -28,8 +28,6 @@ function init(uid, type, parentUid, parentHandle) {
}
}

function noop() { }

async_wrap.setupHooks({ init });
async_wrap.enable();

Expand All @@ -41,7 +39,7 @@ const server = net.createServer(function(c) {
this.close();
});
}).listen(0, function() {
net.connect(this.address().port, noop);
net.connect(this.address().port, common.noop);
});

async_wrap.disable();
Expand Down
6 changes: 2 additions & 4 deletions test/parallel/test-async-wrap-propagate-parent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
const async_wrap = process.binding('async_wrap');
Expand Down Expand Up @@ -28,8 +28,6 @@ function init(uid, type, parentUid, parentHandle) {
}
}

function noop() { }

async_wrap.setupHooks({ init });
async_wrap.enable();

Expand All @@ -41,7 +39,7 @@ const server = net.createServer(function(c) {
this.close();
});
}).listen(0, function() {
net.connect(this.address().port, noop);
net.connect(this.address().port, common.noop);
});


Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-async-wrap-throw-from-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if (typeof process.argv[2] === 'string') {
d.on('error', common.mustNotCall());
d.run(() => {
// Using randomBytes because timers are not yet supported.
crypto.randomBytes(0, () => { });
crypto.randomBytes(0, common.noop);
});

} else {
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-async-wrap-throw-no-init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const async_wrap = process.binding('async_wrap');

Expand All @@ -17,9 +17,9 @@ assert.throws(function() {
}, /init callback is not assigned to a function/);

// Should not throw
async_wrap.setupHooks({ init: () => {} });
async_wrap.setupHooks({ init: common.noop });
async_wrap.enable();

assert.throws(function() {
async_wrap.setupHooks(() => {});
async_wrap.setupHooks(common.noop);
}, /hooks should not be set while also enabled/);
4 changes: 2 additions & 2 deletions test/parallel/test-async-wrap-uid.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

require('../common');
const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const async_wrap = process.binding('async_wrap');

// Give the event loop time to clear out the final uv_close().
let si_cntr = 3;
process.on('beforeExit', () => {
if (--si_cntr > 0) setImmediate(() => {});
if (--si_cntr > 0) setImmediate(common.noop);
});

const storage = new Map();
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-buffer-includes.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 Buffer = require('buffer').Buffer;
Expand Down Expand Up @@ -278,7 +278,7 @@ for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
const expectedError =
/^TypeError: "val" argument must be string, number, Buffer or Uint8Array$/;
assert.throws(() => {
b.includes(() => {});
b.includes(common.noop);
}, expectedError);
assert.throws(() => {
b.includes({});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-bad-stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const assert = require('assert');
const cp = require('child_process');

if (process.argv[2] === 'child') {
setTimeout(() => {}, common.platformTimeout(100));
setTimeout(common.noop, common.platformTimeout(100));
return;
}

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 @@ -78,7 +78,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-fork-ref2.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const fork = require('child_process').fork;

if (process.argv[2] === 'child') {
Expand All @@ -29,7 +29,7 @@ if (process.argv[2] === 'child') {

setTimeout(function() {
console.log('child -> will this keep it alive?');
process.on('message', function() { });
process.on('message', common.noop);
}, 400);

} else {
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 @@ -25,9 +25,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
Loading

0 comments on commit 4f2e372

Please sign in to comment.