forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-duplex-error.js
More file actions
32 lines (24 loc) · 941 Bytes
/
test-duplex-error.js
File metadata and controls
32 lines (24 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';
const common = require('../common');
const assert = require('assert');
const { duplexPair } = require('stream');
const [sideA, sideB] = duplexPair();
// Side A should receive the error because we called .destroy(err) on it.
sideA.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'Simulated error');
}));
// Side B should NOT necessarily emit an error (to avoid crashing
// existing code), but it MUST be destroyed.
sideB.on('error', common.mustNotCall('Side B should not emit an error event'));
sideB.on('close', common.mustCall(() => {
assert.strictEqual(sideB.destroyed, true);
}));
sideA.resume();
sideB.resume();
// Trigger the destruction
sideA.destroy(new Error('Simulated error'));
// Check the state in the next tick to allow nextTick/microtasks to run
setImmediate(common.mustCall(() => {
assert.strictEqual(sideA.destroyed, true);
assert.strictEqual(sideB.destroyed, true);
}));