/
test-diagnostics-channel-bind-store.js
108 lines (85 loc) Β· 3.1 KB
/
test-diagnostics-channel-bind-store.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
const common = require('../common');
const assert = require('assert');
const dc = require('diagnostics_channel');
const { AsyncLocalStorage } = require('async_hooks');
let n = 0;
const thisArg = new Date();
const inputs = [
{ foo: 'bar' },
{ baz: 'buz' },
];
const channel = dc.channel('test');
// Bind a storage directly to published data
const store1 = new AsyncLocalStorage();
channel.bindStore(store1);
let store1bound = true;
// Bind a store with transformation of published data
const store2 = new AsyncLocalStorage();
channel.bindStore(store2, common.mustCall((data) => {
assert.strictEqual(data, inputs[n]);
return { data };
}, 4));
// Regular subscribers should see publishes from runStores calls
channel.subscribe(common.mustCall((data) => {
if (store1bound) {
assert.deepStrictEqual(data, store1.getStore());
}
assert.deepStrictEqual({ data }, store2.getStore());
assert.strictEqual(data, inputs[n]);
}, 4));
// Verify stores are empty before run
assert.strictEqual(store1.getStore(), undefined);
assert.strictEqual(store2.getStore(), undefined);
channel.runStores(inputs[n], common.mustCall(function(a, b) {
// Verify this and argument forwarding
assert.strictEqual(this, thisArg);
assert.strictEqual(a, 1);
assert.strictEqual(b, 2);
// Verify store 1 state matches input
assert.strictEqual(store1.getStore(), inputs[n]);
// Verify store 2 state has expected transformation
assert.deepStrictEqual(store2.getStore(), { data: inputs[n] });
// Should support nested contexts
n++;
channel.runStores(inputs[n], common.mustCall(function() {
// Verify this and argument forwarding
assert.strictEqual(this, undefined);
// Verify store 1 state matches input
assert.strictEqual(store1.getStore(), inputs[n]);
// Verify store 2 state has expected transformation
assert.deepStrictEqual(store2.getStore(), { data: inputs[n] });
}));
n--;
// Verify store 1 state matches input
assert.strictEqual(store1.getStore(), inputs[n]);
// Verify store 2 state has expected transformation
assert.deepStrictEqual(store2.getStore(), { data: inputs[n] });
}), thisArg, 1, 2);
// Verify stores are empty after run
assert.strictEqual(store1.getStore(), undefined);
assert.strictEqual(store2.getStore(), undefined);
// Verify unbinding works
assert.ok(channel.unbindStore(store1));
store1bound = false;
// Verify unbinding a store that is not bound returns false
assert.ok(!channel.unbindStore(store1));
n++;
channel.runStores(inputs[n], common.mustCall(() => {
// Verify after unbinding store 1 will remain undefined
assert.strictEqual(store1.getStore(), undefined);
// Verify still bound store 2 receives expected data
assert.deepStrictEqual(store2.getStore(), { data: inputs[n] });
}));
// Contain transformer errors and emit on next tick
const fail = new Error('fail');
channel.bindStore(store1, () => {
throw fail;
});
let calledRunStores = false;
process.once('uncaughtException', common.mustCall((err) => {
assert.strictEqual(calledRunStores, true);
assert.strictEqual(err, fail);
}));
channel.runStores(inputs[n], common.mustCall());
calledRunStores = true;