|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const dc = require('diagnostics_channel'); |
| 6 | +const { AsyncLocalStorage } = require('async_hooks'); |
| 7 | + |
| 8 | +let n = 0; |
| 9 | +const thisArg = new Date(); |
| 10 | +const inputs = [ |
| 11 | + { foo: 'bar' }, |
| 12 | + { baz: 'buz' }, |
| 13 | +]; |
| 14 | + |
| 15 | +const channel = dc.channel('test'); |
| 16 | + |
| 17 | +// Bind a storage directly to published data |
| 18 | +const store1 = new AsyncLocalStorage(); |
| 19 | +channel.bindStore(store1); |
| 20 | +let store1bound = true; |
| 21 | + |
| 22 | +// Bind a store with transformation of published data |
| 23 | +const store2 = new AsyncLocalStorage(); |
| 24 | +channel.bindStore(store2, common.mustCall((data) => { |
| 25 | + assert.strictEqual(data, inputs[n]); |
| 26 | + return { data }; |
| 27 | +}, 4)); |
| 28 | + |
| 29 | +// Regular subscribers should see publishes from runStores calls |
| 30 | +channel.subscribe(common.mustCall((data) => { |
| 31 | + if (store1bound) { |
| 32 | + assert.deepStrictEqual(data, store1.getStore()); |
| 33 | + } |
| 34 | + assert.deepStrictEqual({ data }, store2.getStore()); |
| 35 | + assert.strictEqual(data, inputs[n]); |
| 36 | +}, 4)); |
| 37 | + |
| 38 | +// Verify stores are empty before run |
| 39 | +assert.strictEqual(store1.getStore(), undefined); |
| 40 | +assert.strictEqual(store2.getStore(), undefined); |
| 41 | + |
| 42 | +channel.runStores(inputs[n], common.mustCall(function(a, b) { |
| 43 | + // Verify this and argument forwarding |
| 44 | + assert.strictEqual(this, thisArg); |
| 45 | + assert.strictEqual(a, 1); |
| 46 | + assert.strictEqual(b, 2); |
| 47 | + |
| 48 | + // Verify store 1 state matches input |
| 49 | + assert.strictEqual(store1.getStore(), inputs[n]); |
| 50 | + |
| 51 | + // Verify store 2 state has expected transformation |
| 52 | + assert.deepStrictEqual(store2.getStore(), { data: inputs[n] }); |
| 53 | + |
| 54 | + // Should support nested contexts |
| 55 | + n++; |
| 56 | + channel.runStores(inputs[n], common.mustCall(function() { |
| 57 | + // Verify this and argument forwarding |
| 58 | + assert.strictEqual(this, undefined); |
| 59 | + |
| 60 | + // Verify store 1 state matches input |
| 61 | + assert.strictEqual(store1.getStore(), inputs[n]); |
| 62 | + |
| 63 | + // Verify store 2 state has expected transformation |
| 64 | + assert.deepStrictEqual(store2.getStore(), { data: inputs[n] }); |
| 65 | + })); |
| 66 | + n--; |
| 67 | + |
| 68 | + // Verify store 1 state matches input |
| 69 | + assert.strictEqual(store1.getStore(), inputs[n]); |
| 70 | + |
| 71 | + // Verify store 2 state has expected transformation |
| 72 | + assert.deepStrictEqual(store2.getStore(), { data: inputs[n] }); |
| 73 | +}), thisArg, 1, 2); |
| 74 | + |
| 75 | +// Verify stores are empty after run |
| 76 | +assert.strictEqual(store1.getStore(), undefined); |
| 77 | +assert.strictEqual(store2.getStore(), undefined); |
| 78 | + |
| 79 | +// Verify unbinding works |
| 80 | +assert.ok(channel.unbindStore(store1)); |
| 81 | +store1bound = false; |
| 82 | + |
| 83 | +// Verify unbinding a store that is not bound returns false |
| 84 | +assert.ok(!channel.unbindStore(store1)); |
| 85 | + |
| 86 | +n++; |
| 87 | +channel.runStores(inputs[n], common.mustCall(() => { |
| 88 | + // Verify after unbinding store 1 will remain undefined |
| 89 | + assert.strictEqual(store1.getStore(), undefined); |
| 90 | + |
| 91 | + // Verify still bound store 2 receives expected data |
| 92 | + assert.deepStrictEqual(store2.getStore(), { data: inputs[n] }); |
| 93 | +})); |
| 94 | + |
| 95 | +// Contain transformer errors and emit on next tick |
| 96 | +const fail = new Error('fail'); |
| 97 | +channel.bindStore(store1, () => { |
| 98 | + throw fail; |
| 99 | +}); |
| 100 | + |
| 101 | +let calledRunStores = false; |
| 102 | +process.once('uncaughtException', common.mustCall((err) => { |
| 103 | + assert.strictEqual(calledRunStores, true); |
| 104 | + assert.strictEqual(err, fail); |
| 105 | +})); |
| 106 | + |
| 107 | +channel.runStores(inputs[n], common.mustCall()); |
| 108 | +calledRunStores = true; |
0 commit comments