forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-diagnostics-channel-bounded-channel-run-transform-error.js
More file actions
66 lines (52 loc) · 2.08 KB
/
test-diagnostics-channel-bounded-channel-run-transform-error.js
File metadata and controls
66 lines (52 loc) · 2.08 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
const common = require('../common');
const assert = require('node:assert');
const dc = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');
// Test BoundedChannel.run() with store transform error
// Transform errors are scheduled via process.nextTick(triggerUncaughtException)
const boundedChannel = dc.boundedChannel('test-run-transform-error');
const store = new AsyncLocalStorage();
const events = [];
const transformError = new Error('transform failed');
// Set up uncaughtException handler to catch the transform error
process.on('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err, transformError);
events.push('uncaughtException');
}));
boundedChannel.subscribe({
start(message) {
events.push({ type: 'start', data: message });
},
end(message) {
events.push({ type: 'end', data: message });
},
});
// Bind store with a transform that throws
boundedChannel.start.bindStore(store, () => {
throw transformError;
});
// Store should remain undefined since transform will fail
assert.strictEqual(store.getStore(), undefined);
const result = boundedChannel.run({ operationId: '123' }, common.mustCall(() => {
// Store should still be undefined because transform threw
assert.strictEqual(store.getStore(), undefined);
events.push('inside-run');
return 42;
}));
// Should still return the result despite transform error
assert.strictEqual(result, 42);
// Store should still be undefined after run
assert.strictEqual(store.getStore(), undefined);
// Start and end events should still be published despite transform error
assert.strictEqual(events.length, 3);
assert.strictEqual(events[0].type, 'start');
assert.strictEqual(events[0].data.operationId, '123');
assert.strictEqual(events[1], 'inside-run');
assert.strictEqual(events[2].type, 'end');
assert.strictEqual(events[2].data.operationId, '123');
// Validate uncaughtException was triggered via nextTick
process.on('beforeExit', common.mustCall(() => {
assert.strictEqual(events.length, 4);
assert.strictEqual(events[3], 'uncaughtException');
}));