|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const { AsyncLocalStorage } = require('async_hooks'); |
| 5 | + |
| 6 | +const store = new AsyncLocalStorage(); |
| 7 | +let checked = 0; |
| 8 | + |
| 9 | +function thenable(expected, count) { |
| 10 | + return { |
| 11 | + then: common.mustCall((cb) => { |
| 12 | + assert.strictEqual(expected, store.getStore()); |
| 13 | + checked++; |
| 14 | + cb(); |
| 15 | + }, count) |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function main(n) { |
| 20 | + const firstData = Symbol('first-data'); |
| 21 | + const secondData = Symbol('second-data'); |
| 22 | + |
| 23 | + const first = thenable(firstData, 1); |
| 24 | + const second = thenable(secondData, 1); |
| 25 | + const third = thenable(firstData, 2); |
| 26 | + |
| 27 | + return store.run(firstData, common.mustCall(async () => { |
| 28 | + assert.strictEqual(firstData, store.getStore()); |
| 29 | + await first; |
| 30 | + |
| 31 | + await store.run(secondData, common.mustCall(async () => { |
| 32 | + assert.strictEqual(secondData, store.getStore()); |
| 33 | + await second; |
| 34 | + assert.strictEqual(secondData, store.getStore()); |
| 35 | + })); |
| 36 | + |
| 37 | + await Promise.all([ third, third ]); |
| 38 | + assert.strictEqual(firstData, store.getStore()); |
| 39 | + })); |
| 40 | +} |
| 41 | + |
| 42 | +const outerData = Symbol('outer-data'); |
| 43 | + |
| 44 | +Promise.all([ |
| 45 | + store.run(outerData, () => Promise.resolve(thenable(outerData))), |
| 46 | + Promise.resolve(3).then(common.mustCall(main)), |
| 47 | + main(1), |
| 48 | + main(2) |
| 49 | +]).then(common.mustCall(() => { |
| 50 | + assert.strictEqual(checked, 13); |
| 51 | +})); |
0 commit comments