diff options
author | cjihrig <cjihrig@gmail.com> | 2016-02-16 22:18:10 -0500 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2016-02-22 11:55:30 -0500 |
commit | e854f60585061c81702cf680ad8079a447d19ef1 (patch) | |
tree | f56592e463fecd041d428d6181be9fa418ababb9 /test/parallel/test-child-process-send-keep-open.js | |
parent | 1952844f45f8944f7c6920e836d0deb3de210efd (diff) | |
download | node-new-e854f60585061c81702cf680ad8079a447d19ef1.tar.gz |
child_process: add keepOpen option to send()
This option allows an instance of net.Socket to be kept open in
the sending process.
Fixes: https://github.com/nodejs/node/issues/4271
PR-URL: https://github.com/nodejs/node/pull/5283
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'test/parallel/test-child-process-send-keep-open.js')
-rw-r--r-- | test/parallel/test-child-process-send-keep-open.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/parallel/test-child-process-send-keep-open.js b/test/parallel/test-child-process-send-keep-open.js new file mode 100644 index 0000000000..5771f4677c --- /dev/null +++ b/test/parallel/test-child-process-send-keep-open.js @@ -0,0 +1,52 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const cp = require('child_process'); +const net = require('net'); + +if (process.argv[2] !== 'child') { + // The parent process forks a child process, starts a TCP server, and connects + // to the server. The accepted connection is passed to the child process, + // where the socket is written. Then, the child signals the parent process to + // write to the same socket. + let result = ''; + + process.on('exit', () => { + assert.strictEqual(result, 'childparent'); + }); + + const child = cp.fork(__filename, ['child']); + + // Verify that the child exits successfully + child.on('exit', common.mustCall((exitCode, signalCode) => { + assert.strictEqual(exitCode, 0); + assert.strictEqual(signalCode, null); + })); + + const server = net.createServer((socket) => { + child.on('message', common.mustCall((msg) => { + assert.strictEqual(msg, 'child_done'); + socket.end('parent', () => { + server.close(); + child.disconnect(); + }); + })); + + child.send('socket', socket, {keepOpen: true}, common.mustCall((err) => { + assert.ifError(err); + })); + }); + + server.listen(common.PORT, () => { + const socket = net.connect(common.PORT, common.localhostIPv4); + socket.setEncoding('utf8'); + socket.on('data', (data) => result += data); + }); +} else { + // The child process receives the socket from the parent, writes data to + // the socket, then signals the parent process to write + process.on('message', common.mustCall((msg, socket) => { + assert.strictEqual(msg, 'socket'); + socket.write('child', () => process.send('child_done')); + })); +} |