2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2012-12-16 21:47:01 +01:00
|
|
|
// Make sure that sync writes to stderr get processed before exiting.
|
|
|
|
|
2015-12-24 01:02:12 +01:00
|
|
|
require('../common');
|
2012-12-16 21:47:01 +01:00
|
|
|
|
|
|
|
function parent() {
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var assert = require('assert');
|
|
|
|
var i = 0;
|
|
|
|
children.forEach(function(_, c) {
|
|
|
|
var child = spawn(process.execPath, [__filename, '' + c]);
|
|
|
|
var err = '';
|
|
|
|
|
|
|
|
child.stderr.on('data', function(c) {
|
|
|
|
err += c;
|
|
|
|
});
|
|
|
|
|
|
|
|
child.on('close', function() {
|
|
|
|
assert.equal(err, 'child ' + c + '\nfoo\nbar\nbaz\n');
|
|
|
|
console.log('ok %d child #%d', ++i, c);
|
|
|
|
if (i === children.length)
|
|
|
|
console.log('1..' + i);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// using console.error
|
2014-01-16 13:17:01 +01:00
|
|
|
function child0() {
|
|
|
|
console.error('child 0');
|
2012-12-16 21:47:01 +01:00
|
|
|
console.error('foo');
|
|
|
|
console.error('bar');
|
|
|
|
console.error('baz');
|
|
|
|
}
|
|
|
|
|
|
|
|
// using process.stderr
|
2014-01-16 13:17:01 +01:00
|
|
|
function child1() {
|
|
|
|
process.stderr.write('child 1\n');
|
2012-12-16 21:47:01 +01:00
|
|
|
process.stderr.write('foo\n');
|
|
|
|
process.stderr.write('bar\n');
|
|
|
|
process.stderr.write('baz\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
// using a net socket
|
2014-01-16 13:17:01 +01:00
|
|
|
function child2() {
|
2012-12-16 21:47:01 +01:00
|
|
|
var net = require('net');
|
2014-02-26 15:03:59 +01:00
|
|
|
var socket = new net.Socket({
|
|
|
|
fd: 2,
|
|
|
|
readable: false,
|
|
|
|
writable: true});
|
2014-01-16 13:17:01 +01:00
|
|
|
socket.write('child 2\n');
|
2012-12-16 21:47:01 +01:00
|
|
|
socket.write('foo\n');
|
|
|
|
socket.write('bar\n');
|
|
|
|
socket.write('baz\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-16 13:17:01 +01:00
|
|
|
function child3() {
|
|
|
|
console.error('child 3\nfoo\nbar\nbaz');
|
2012-12-16 21:47:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-16 13:17:01 +01:00
|
|
|
function child4() {
|
|
|
|
process.stderr.write('child 4\nfoo\nbar\nbaz\n');
|
2012-12-16 21:47:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-16 13:17:01 +01:00
|
|
|
var children = [ child0, child1, child2, child3, child4 ];
|
2012-12-16 21:47:01 +01:00
|
|
|
|
|
|
|
if (!process.argv[2]) {
|
|
|
|
parent();
|
|
|
|
} else {
|
|
|
|
children[process.argv[2]]();
|
|
|
|
// immediate process.exit to kill any waiting stuff.
|
|
|
|
process.exit();
|
|
|
|
}
|