|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// This test verifies that when multiple REPL instances exist concurrently, |
| 4 | +// async errors are correctly routed to the REPL instance that created them. |
| 5 | + |
| 6 | +const common = require('../common'); |
| 7 | +const assert = require('assert'); |
| 8 | +const repl = require('repl'); |
| 9 | +const { Writable, PassThrough } = require('stream'); |
| 10 | + |
| 11 | +// Create two REPLs with separate inputs and outputs |
| 12 | +let output1 = ''; |
| 13 | +let output2 = ''; |
| 14 | + |
| 15 | +const input1 = new PassThrough(); |
| 16 | +const input2 = new PassThrough(); |
| 17 | + |
| 18 | +const writable1 = new Writable({ |
| 19 | + write(chunk, encoding, callback) { |
| 20 | + output1 += chunk.toString(); |
| 21 | + callback(); |
| 22 | + } |
| 23 | +}); |
| 24 | + |
| 25 | +const writable2 = new Writable({ |
| 26 | + write(chunk, encoding, callback) { |
| 27 | + output2 += chunk.toString(); |
| 28 | + callback(); |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +const r1 = repl.start({ |
| 33 | + input: input1, |
| 34 | + output: writable1, |
| 35 | + terminal: false, |
| 36 | + prompt: 'R1> ', |
| 37 | +}); |
| 38 | + |
| 39 | +const r2 = repl.start({ |
| 40 | + input: input2, |
| 41 | + output: writable2, |
| 42 | + terminal: false, |
| 43 | + prompt: 'R2> ', |
| 44 | +}); |
| 45 | + |
| 46 | +// Create async error in REPL 1 |
| 47 | +input1.write('setTimeout(() => { throw new Error("error from repl1") }, 10)\n'); |
| 48 | + |
| 49 | +// Create async error in REPL 2 |
| 50 | +input2.write('setTimeout(() => { throw new Error("error from repl2") }, 20)\n'); |
| 51 | + |
| 52 | +setTimeout(common.mustCall(() => { |
| 53 | + r1.close(); |
| 54 | + r2.close(); |
| 55 | + |
| 56 | + // Verify error from REPL 1 went to REPL 1's output |
| 57 | + assert.match(output1, /error from repl1/, |
| 58 | + 'REPL 1 should have received its own async error'); |
| 59 | + |
| 60 | + // Verify error from REPL 2 went to REPL 2's output |
| 61 | + assert.match(output2, /error from repl2/, |
| 62 | + 'REPL 2 should have received its own async error'); |
| 63 | + |
| 64 | + // Verify errors did not cross over to wrong REPL |
| 65 | + assert.doesNotMatch(output1, /error from repl2/, |
| 66 | + 'REPL 1 should not have received REPL 2\'s error'); |
| 67 | + assert.doesNotMatch(output2, /error from repl1/, |
| 68 | + 'REPL 2 should not have received REPL 1\'s error'); |
| 69 | +}), 100); |
0 commit comments