Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Add test-child-process-fork2 and fixes to make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Oct 7, 2011
1 parent 4561d9e commit 899358e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/child_process_uv.js
Expand Up @@ -95,8 +95,12 @@ function setupChannel(target, channel) {
if (!target._channel) throw new Error("channel closed");

// Open up net.Socket instances
if (sendStream instanceof require('net').Socket) {
if (sendStream instanceof require('net').Socket ||
sendStream instanceof require('net').Server) {
sendStream = sendStream._handle;
if (!sendStream) {
throw new Error("sendStream handle not yet opened");
}
}

// For overflow protection don't write if channel queue is too deep.
Expand Down Expand Up @@ -397,8 +401,6 @@ function setStreamOption(name, index, options) {
ChildProcess.prototype.spawn = function(options) {
var self = this;

debugger;

setStreamOption('stdinStream', 0, options);
setStreamOption('stdoutStream', 1, options);
setStreamOption('stderrStream', 2, options);
Expand Down
6 changes: 6 additions & 0 deletions src/node.js
Expand Up @@ -389,6 +389,12 @@
var fd = parseInt(process.env.NODE_CHANNEL_FD);
assert(fd >= 0);
var cp = NativeModule.require('child_process');

// Load tcp_wrap to avoid situation where we might immediately receive
// a message.
// FIXME is this really necessary?
process.binding('tcp_wrap')

cp._forkChild(fd);
assert(process.send);
}
Expand Down
3 changes: 1 addition & 2 deletions src/stream_wrap.cc
Expand Up @@ -183,8 +183,6 @@ void StreamWrap::OnReadCommon(uv_stream_t* handle, ssize_t nread,
uv_buf_t buf, uv_handle_type pending) {
HandleScope scope;

assert(pending == UV_UNKNOWN_HANDLE); // TODO

StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);

// We should not be getting this callback if someone as already called
Expand Down Expand Up @@ -220,6 +218,7 @@ void StreamWrap::OnReadCommon(uv_stream_t* handle, ssize_t nread,
Integer::New(nread)
};


if (pending == UV_TCP) {
// Instantiate the client javascript object and handle.
Local<Object> pending_obj = TCPWrap::Instantiate();
Expand Down
8 changes: 7 additions & 1 deletion src/tcp_wrap.cc
Expand Up @@ -56,8 +56,14 @@ typedef class ReqWrap<uv_connect_t> ConnectWrap;


Local<Object> TCPWrap::Instantiate() {
// If this assert fire then process.binding('tcp_wrap') hasn't been
// called yet.
assert(tcpConstructor.IsEmpty() == false);

HandleScope scope;
return scope.Close(tcpConstructor->NewInstance());
Local<Object> obj = tcpConstructor->NewInstance();

return scope.Close(obj);
}


Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/fork2.js
@@ -0,0 +1,9 @@
var assert = require('assert');

process.on('message', function(m, server) {
console.log('CHILD got message:', m);
assert.ok(m.hello);
assert.ok(server);
process.send({ gotHandle: true });
});

27 changes: 27 additions & 0 deletions test/simple/test-child-process-fork2.js
@@ -0,0 +1,27 @@
var assert = require('assert');
var common = require('../common');
var fork = require('child_process').fork;
var net = require('net');

var n = fork(common.fixturesDir + '/fork2.js');

var messageCount = 0;

var server = new net.Server();
server.listen(common.PORT, function() {
console.log('PARENT send child server handle');
n.send({ hello: 'world' }, server);
});


n.on('message', function(m) {
console.log('PARENT got message:', m);
assert.ok(m.gotHandle);
messageCount++;
n.kill();
server.close();
});

process.on('exit', function() {
assert.equal(1, messageCount);
});

0 comments on commit 899358e

Please sign in to comment.