• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4
5const spawn = require('child_process').spawn;
6
7const child = spawn(process.execPath, [ '-i' ], {
8  stdio: [null, null, 2]
9});
10
11let stdout = '';
12child.stdout.setEncoding('utf8');
13child.stdout.on('data', function(c) {
14  process.stdout.write(c);
15  stdout += c;
16  if (stdout.includes('> THROW 2'))
17    child.stdin.end();
18});
19
20child.stdin.write = function(original) {
21  return function(c) {
22    process.stderr.write(c);
23    return original.call(child.stdin, c);
24  };
25}(child.stdin.write);
26
27child.stdout.once('data', function() {
28  child.stdin.write('let throws = 0;');
29  child.stdin.write('process.on("exit",function(){console.log(throws)});');
30  child.stdin.write('function thrower(){console.log("THROW",throws++);XXX};');
31  child.stdin.write('setTimeout(thrower);""\n');
32
33  setTimeout(fsTest, 50);
34  function fsTest() {
35    const f = JSON.stringify(__filename);
36    child.stdin.write(`fs.readFile(${f}, thrower);\n`);
37    setTimeout(eeTest, 50);
38  }
39
40  function eeTest() {
41    child.stdin.write('setTimeout(function() {\n' +
42                      '  const events = require("events");\n' +
43                      '  let e = new events.EventEmitter;\n' +
44                      '  process.nextTick(function() {\n' +
45                      '    e.on("x", thrower);\n' +
46                      '    setTimeout(function() {\n' +
47                      '      e.emit("x");\n' +
48                      '    });\n' +
49                      '  });\n' +
50                      '});"";\n');
51  }
52});
53
54child.on('close', function(c) {
55  assert.strictEqual(c, 0);
56  // Make sure we got 3 throws, in the end.
57  const lastLine = stdout.trim().split(/\r?\n/).pop();
58  assert.strictEqual(lastLine, '> 3');
59});
60