Skip to content

Commit

Permalink
test: test and docs for detached fork process
Browse files Browse the repository at this point in the history
This tests child process fork component in detached mode
by spawning a parent process that creates a child process.
We kill the parent process and check if the child is still
running.

Fixes: #17592

PR-URL: #24524
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
nanomosfet authored and BethGriggs committed Feb 12, 2019
1 parent 8c93bd4 commit 5966dbe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ changes:
* `args` {string[]} List of string arguments.
* `options` {Object}
* `cwd` {string} Current working directory of the child process.
* `detached` {boolean} Prepare child to run independently of its parent
process. Specific behavior depends on the platform, see
[`options.detached`][]).
* `env` {Object} Environment key-value pairs.
* `execPath` {string} Executable used to create the child process.
* `execArgv` {string[]} List of string arguments passed to the executable.
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/parent-process-nonpersistent-fork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fork = require('child_process').fork;
const path = require('path');

const child = fork(
path.join(__dirname, 'child-process-persistent.js'),
[],
{ detached: true, stdio: 'ignore' }
);

console.log(child.pid);

child.unref();
23 changes: 23 additions & 0 deletions test/parallel/test-child-process-fork-detached.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
require('../common');
const assert = require('assert');
const fork = require('child_process').fork;
const fixtures = require('../common/fixtures');

const nonPersistentNode = fork(
fixtures.path('parent-process-nonpersistent-fork.js'),
[],
{ silent: true });

let childId = -1;

nonPersistentNode.stdout.on('data', (data) => {
childId = parseInt(data, 10);
nonPersistentNode.kill();
});

process.on('exit', () => {
assert.notStrictEqual(childId, -1);
// Killing the child process should not throw an error
process.kill(childId);
});

0 comments on commit 5966dbe

Please sign in to comment.