forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle_wrap: expose an
isRefed()
check to JS
This allows third-party tools to check whether or not a handle that can be unreferenced is unreferenced at a particular time. Notably, this should be helpful for inspection via AsyncWrap. Also, this is useful even to node's internals, particularly timers. Refs: nodejs#5828 Refs: nodejs#5827 PR-URL: nodejs#5834 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
- Loading branch information
1 parent
1879e1e
commit 7d8882b
Showing
11 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const strictEqual = require('assert').strictEqual; | ||
const spawn = require('child_process').spawn; | ||
|
||
function makeAssert(message) { | ||
return function(actual, expected) { | ||
strictEqual(actual, expected, message); | ||
}; | ||
} | ||
const assert = makeAssert('isRefed() not working on tty_wrap'); | ||
|
||
if (process.argv[2] === 'child') { | ||
// Test tty_wrap in piped child to guarentee stdin being a TTY. | ||
const ReadStream = require('tty').ReadStream; | ||
const tty = new ReadStream(0); | ||
assert(Object.getPrototypeOf(tty._handle).hasOwnProperty('isRefed'), true); | ||
assert(tty._handle.isRefed(), true); | ||
tty.unref(); | ||
assert(tty._handle.isRefed(), false); | ||
return; | ||
} | ||
|
||
// Use spawn so that we can be sure that stdin has a _handle property. | ||
// Refs: https://github.com/nodejs/node/pull/5916 | ||
const proc = spawn(process.execPath, [__filename, 'child'], { stdio: 'pipe' }); | ||
proc.stderr.pipe(process.stderr); | ||
proc.on('exit', common.mustCall(function(exitCode) { | ||
process.exitCode = exitCode; | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
'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.unref(); | ||
} | ||
|
||
|
||
// 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.unref(); | ||
|
||
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.unref(); | ||
} | ||
|
||
|
||
// 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.unref(); | ||
} | ||
|
||
|
||
// 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.unref(); | ||
} | ||
|
||
|
||
// 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.unref(); | ||
} |