node/test/parallel/test-handle-wrap-isrefed.js
Jeremiah Senkpiel 96a9439fd2 Revert "handle_wrap: IsRefed -> Unrefed, no isAlive check"
This reverts commit 9bb5a5e2a1.

This API is not suitable because it depended on being able to
potentially access the handle's flag after the handle was already
cleaned up. Since this is not actually possible (obviously, oops)
this newer API no longer makes much sense, and the older API is more
suitable.

API comparison:
IsRefed -> Has a strong reference AND is alive. (Deterministic)
Unrefed -> Has a weak reference OR is dead. (Less deterministic)

Refs: https://github.com/nodejs/node/pull/6395
Refs: https://github.com/nodejs/node/pull/6204
Refs: https://github.com/nodejs/node/pull/6401
Refs: https://github.com/nodejs/node/pull/6382
Fixes: https://github.com/nodejs/node/pull/6381

PR-URL: https://github.com/nodejs/node/pull/6546
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>

Conflicts:
	src/handle_wrap.cc
	test/parallel/test-handle-wrap-isrefed-tty.js
	test/parallel/test-handle-wrap-isrefed.js
2016-05-11 17:43:48 -04:00

102 lines
3.0 KiB
JavaScript

'use strict';
const common = require('../common');
const strictEqual = require('assert').strictEqual;
function makeAssert(message) {
return function(actual, expected) {
strictEqual(actual, expected, message);
};
}
// child_process
{
const assert = makeAssert('isRefed() not working on process_wrap');
const spawn = require('child_process').spawn;
const cmd = common.isWindows ? 'rundll32' : 'ls';
const cp = spawn(cmd);
assert(Object.getPrototypeOf(cp._handle).hasOwnProperty('isRefed'), true);
assert(cp._handle.isRefed(), true);
cp.unref();
assert(cp._handle.isRefed(), false);
cp.ref();
assert(cp._handle.isRefed(), true);
cp._handle.close(common.mustCall(() => assert(cp._handle.isRefed(), false)));
}
// dgram
{
const assert = makeAssert('isRefed() not working on udp_wrap');
const dgram = require('dgram');
const sock4 = dgram.createSocket('udp4');
assert(Object.getPrototypeOf(sock4._handle).hasOwnProperty('isRefed'), true);
assert(sock4._handle.isRefed(), true);
sock4.unref();
assert(sock4._handle.isRefed(), false);
sock4.ref();
assert(sock4._handle.isRefed(), true);
sock4._handle.close(
common.mustCall(() => assert(sock4._handle.isRefed(), false)));
const sock6 = dgram.createSocket('udp6');
assert(Object.getPrototypeOf(sock6._handle).hasOwnProperty('isRefed'), true);
assert(sock6._handle.isRefed(), true);
sock6.unref();
assert(sock6._handle.isRefed(), false);
sock6.ref();
assert(sock6._handle.isRefed(), true);
sock6._handle.close(
common.mustCall(() => assert(sock6._handle.isRefed(), false)));
}
// pipe
{
const assert = makeAssert('isRefed() not working on pipe_wrap');
const Pipe = process.binding('pipe_wrap').Pipe;
const handle = new Pipe();
assert(Object.getPrototypeOf(handle).hasOwnProperty('isRefed'), true);
assert(handle.isRefed(), true);
handle.unref();
assert(handle.isRefed(), false);
handle.ref();
assert(handle.isRefed(), true);
handle.close(common.mustCall(() => assert(handle.isRefed(), false)));
}
// tcp
{
const assert = makeAssert('isRefed() not working on tcp_wrap');
const net = require('net');
const server = net.createServer(() => {}).listen(common.PORT);
assert(Object.getPrototypeOf(server._handle).hasOwnProperty('isRefed'), true);
assert(server._handle.isRefed(), true);
assert(server._unref, false);
server.unref();
assert(server._handle.isRefed(), false);
assert(server._unref, true);
server.ref();
assert(server._handle.isRefed(), true);
assert(server._unref, false);
server._handle.close(
common.mustCall(() => assert(server._handle.isRefed(), false)));
}
// timers
{
const assert = makeAssert('isRefed() not working on timer_wrap');
const timer = setTimeout(() => {}, 500);
timer.unref();
assert(Object.getPrototypeOf(timer._handle).hasOwnProperty('isRefed'), true);
assert(timer._handle.isRefed(), false);
timer.ref();
assert(timer._handle.isRefed(), true);
timer._handle.close(
common.mustCall(() => assert(timer._handle.isRefed(), false)));
}