node/test/parallel/test-handle-wrap-isrefed.js
Rich Trott 7f2b5d305d test: remove common.noop
This change removes `common.noop` from the Node.js internal testing
common module.

Over the last few weeks, I've grown to dislike the `common.noop`
abstraction.

First, new (and experienced) contributors are unaware of it and so it
results in a large number of low-value nits on PRs. It also increases
the number of things newcomers and infrequent contributors have to be
aware of to be effective on the project.

Second, it is confusing. Is it a singleton/property or a getter? Which
should be expected? This can lead to subtle and hard-to-find bugs. (To
my knowledge, none have landed on master. But I also think it's only a
matter of time.)

Third, the abstraction is low-value in my opinion. What does it really
get us? A case could me made that it is without value at all.

Lastly, and this is minor, but the abstraction is wordier than not using
the abstraction. `common.noop` doesn't save anything over `() => {}`.

So, I propose removing it.

PR-URL: https://github.com/nodejs/node/pull/12822
Backport-PR-URL: https://github.com/nodejs/node/pull/14174
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-07-19 15:02:25 -04:00

130 lines
4.4 KiB
JavaScript

'use strict';
const common = require('../common');
const strictEqual = require('assert').strictEqual;
// child_process
{
const spawn = require('child_process').spawn;
const cmd = common.isWindows ? 'rundll32' : 'ls';
const cp = spawn(cmd);
strictEqual(Object.getPrototypeOf(cp._handle).hasOwnProperty('hasRef'),
true, 'process_wrap: hasRef() missing');
strictEqual(cp._handle.hasRef(),
true, 'process_wrap: not initially refed');
cp.unref();
strictEqual(cp._handle.hasRef(),
false, 'process_wrap: unref() ineffective');
cp.ref();
strictEqual(cp._handle.hasRef(),
true, 'process_wrap: ref() ineffective');
cp._handle.close(common.mustCall(() =>
strictEqual(cp._handle.hasRef(),
false, 'process_wrap: not unrefed on close')));
}
// dgram ipv4
{
const dgram = require('dgram');
const sock4 = dgram.createSocket('udp4');
strictEqual(Object.getPrototypeOf(sock4._handle).hasOwnProperty('hasRef'),
true, 'udp_wrap: ipv4: hasRef() missing');
strictEqual(sock4._handle.hasRef(),
true, 'udp_wrap: ipv4: not initially refed');
sock4.unref();
strictEqual(sock4._handle.hasRef(),
false, 'udp_wrap: ipv4: unref() ineffective');
sock4.ref();
strictEqual(sock4._handle.hasRef(),
true, 'udp_wrap: ipv4: ref() ineffective');
sock4._handle.close(common.mustCall(() =>
strictEqual(sock4._handle.hasRef(),
false, 'udp_wrap: ipv4: not unrefed on close')));
}
// dgram ipv6
{
const dgram = require('dgram');
const sock6 = dgram.createSocket('udp6');
strictEqual(Object.getPrototypeOf(sock6._handle).hasOwnProperty('hasRef'),
true, 'udp_wrap: ipv6: hasRef() missing');
strictEqual(sock6._handle.hasRef(),
true, 'udp_wrap: ipv6: not initially refed');
sock6.unref();
strictEqual(sock6._handle.hasRef(),
false, 'udp_wrap: ipv6: unref() ineffective');
sock6.ref();
strictEqual(sock6._handle.hasRef(),
true, 'udp_wrap: ipv6: ref() ineffective');
sock6._handle.close(common.mustCall(() =>
strictEqual(sock6._handle.hasRef(),
false, 'udp_wrap: ipv6: not unrefed on close')));
}
// pipe
{
const Pipe = process.binding('pipe_wrap').Pipe;
const handle = new Pipe();
strictEqual(Object.getPrototypeOf(handle).hasOwnProperty('hasRef'),
true, 'pipe_wrap: hasRef() missing');
strictEqual(handle.hasRef(),
true, 'pipe_wrap: not initially refed');
handle.unref();
strictEqual(handle.hasRef(),
false, 'pipe_wrap: unref() ineffective');
handle.ref();
strictEqual(handle.hasRef(),
true, 'pipe_wrap: ref() ineffective');
handle.close(common.mustCall(() =>
strictEqual(handle.hasRef(),
false, 'pipe_wrap: not unrefed on close')));
}
// tcp
{
const net = require('net');
const server = net.createServer(() => {}).listen(0);
strictEqual(Object.getPrototypeOf(server._handle).hasOwnProperty('hasRef'),
true, 'tcp_wrap: hasRef() missing');
strictEqual(server._handle.hasRef(),
true, 'tcp_wrap: not initially refed');
strictEqual(server._unref,
false, 'tcp_wrap: _unref initially incorrect');
server.unref();
strictEqual(server._handle.hasRef(),
false, 'tcp_wrap: unref() ineffective');
strictEqual(server._unref,
true, 'tcp_wrap: _unref not updated on unref()');
server.ref();
strictEqual(server._handle.hasRef(),
true, 'tcp_wrap: ref() ineffective');
strictEqual(server._unref,
false, 'tcp_wrap: _unref not updated on ref()');
server._handle.close(common.mustCall(() =>
strictEqual(server._handle.hasRef(),
false, 'tcp_wrap: not unrefed on close')));
}
// timers
{
const timer = setTimeout(() => {}, 500);
timer.unref();
strictEqual(Object.getPrototypeOf(timer._handle).hasOwnProperty('hasRef'),
true, 'timer_wrap: hasRef() missing');
strictEqual(timer._handle.hasRef(),
false, 'timer_wrap: unref() ineffective');
timer.ref();
strictEqual(timer._handle.hasRef(),
true, 'timer_wrap: ref() ineffective');
timer._handle.close(common.mustCall(() =>
strictEqual(timer._handle.hasRef(),
false, 'timer_wrap: not unrefed on close')));
}
// see also test/pseudo-tty/test-handle-wrap-isrefed-tty.js