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.
child_process: add hasRef to ChildProcess
Refs: nodejs#42091 The issue above reported a specific use case about `Timer.hasRef()`. After reading the issue, I started thinking users may naturally expect `hasRef` method if `ref()` and `unref()` exist on an object they use. As of now, however, `hasRef()` exists on `Timer` only. This commit suggests adding `hasRef` method to `ChildProcess` first. There are more similar cases. (fs.FSWatcher, fs.StatWatcher, net.Socket, and so on)
- Loading branch information
Showing
3 changed files
with
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { spawn, ChildProcess } = require('child_process'); | ||
|
||
const command = common.isWindows ? 'dir' : 'ls'; | ||
const child = spawn(command); | ||
|
||
assert.strictEqual(child.hasRef(), true); | ||
|
||
child.unref(); | ||
assert.strictEqual(child.hasRef(), false); | ||
|
||
child.ref(); | ||
assert.strictEqual(child.hasRef(), true); | ||
|
||
function mustHasRef() { | ||
return common.mustCall(() => assert.strictEqual(child.hasRef(), true)); | ||
} | ||
|
||
function mustHasNoRef() { | ||
return common.mustCall(() => assert.strictEqual(child.hasRef(), false)); | ||
} | ||
|
||
child.stdout.on('data', mustHasRef()); | ||
child.stdout.on('end', mustHasRef()); | ||
child.stdout.on('close', mustHasNoRef()); | ||
child.on('exit', mustHasNoRef()); | ||
child.on('close', mustHasNoRef()); |