• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23// Make sure that sync writes to stderr get processed before exiting.
24
25require('../common');
26
27function parent() {
28  const spawn = require('child_process').spawn;
29  const assert = require('assert');
30  let i = 0;
31  children.forEach(function(_, c) {
32    const child = spawn(process.execPath, [__filename, String(c)]);
33    let err = '';
34
35    child.stderr.on('data', function(c) {
36      err += c;
37    });
38
39    child.on('close', function() {
40      assert.strictEqual(err, `child ${c}\nfoo\nbar\nbaz\n`);
41      console.log(`ok ${++i} child #${c}`);
42      if (i === children.length)
43        console.log(`1..${i}`);
44    });
45  });
46}
47
48// using console.error
49function child0() {
50  console.error('child 0');
51  console.error('foo');
52  console.error('bar');
53  console.error('baz');
54}
55
56// Using process.stderr
57function child1() {
58  process.stderr.write('child 1\n');
59  process.stderr.write('foo\n');
60  process.stderr.write('bar\n');
61  process.stderr.write('baz\n');
62}
63
64// using a net socket
65function child2() {
66  const net = require('net');
67  const socket = new net.Socket({
68    fd: 2,
69    readable: false,
70    writable: true,
71  });
72  socket.write('child 2\n');
73  socket.write('foo\n');
74  socket.write('bar\n');
75  socket.write('baz\n');
76}
77
78
79function child3() {
80  console.error('child 3\nfoo\nbar\nbaz');
81}
82
83function child4() {
84  process.stderr.write('child 4\nfoo\nbar\nbaz\n');
85}
86
87const children = [ child0, child1, child2, child3, child4 ];
88
89if (!process.argv[2]) {
90  parent();
91} else {
92  children[process.argv[2]]();
93  // Immediate process.exit to kill any waiting stuff.
94  process.exit();
95}
96