|
| 1 | +// Flags: --expose-internals --no-async-context-frame |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const { AsyncLocalStorage } = require('async_hooks'); |
| 6 | +const assert = require('assert'); |
| 7 | +const { |
| 8 | + symbols: { |
| 9 | + async_local_storage_context_symbol, |
| 10 | + }, |
| 11 | +} = require('internal/async_hooks'); |
| 12 | + |
| 13 | +function getStore(resource, asyncLocalStorage) { |
| 14 | + return resource[async_local_storage_context_symbol]?.[asyncLocalStorage.kResourceStore]; |
| 15 | +} |
| 16 | + |
| 17 | +function assertNoStore(resource) { |
| 18 | + assert.strictEqual(resource[async_local_storage_context_symbol], undefined); |
| 19 | +} |
| 20 | + |
| 21 | +// Test that setTimeout does not retain a reference to the async store after |
| 22 | +// firing. The callback and arguments must stay on the Timeout object so that |
| 23 | +// refresh() can reactivate the timer. |
| 24 | +{ |
| 25 | + const asyncLocalStorage = new AsyncLocalStorage(); |
| 26 | + const store = {}; |
| 27 | + const arg = {}; |
| 28 | + asyncLocalStorage.run(store, common.mustCall(() => { |
| 29 | + const timeout = setTimeout(common.mustCall((received) => { |
| 30 | + assert.strictEqual(received, arg); |
| 31 | + setImmediate(common.mustCall(() => { |
| 32 | + assertNoStore(timeout); |
| 33 | + })); |
| 34 | + }), 1, arg); |
| 35 | + assert.strictEqual(getStore(timeout, asyncLocalStorage), store); |
| 36 | + })); |
| 37 | +} |
| 38 | + |
| 39 | +// Test that clearTimeout cleans up the store, callback, and arguments before |
| 40 | +// firing. |
| 41 | +{ |
| 42 | + const asyncLocalStorage = new AsyncLocalStorage(); |
| 43 | + const store = {}; |
| 44 | + const arg = {}; |
| 45 | + asyncLocalStorage.run(store, common.mustCall(() => { |
| 46 | + const timeout = setTimeout(common.mustNotCall(), common.platformTimeout(1000), arg); |
| 47 | + assert.strictEqual(getStore(timeout, asyncLocalStorage), store); |
| 48 | + clearTimeout(timeout); |
| 49 | + assertNoStore(timeout); |
| 50 | + assert.strictEqual(timeout._onTimeout, undefined); |
| 51 | + assert.strictEqual(timeout._timerArgs, undefined); |
| 52 | + })); |
| 53 | +} |
| 54 | + |
| 55 | +// Test that setInterval does not retain a reference to the async store, |
| 56 | +// callback, or arguments after it is cleared. |
| 57 | +{ |
| 58 | + const asyncLocalStorage = new AsyncLocalStorage(); |
| 59 | + const store = {}; |
| 60 | + const arg = {}; |
| 61 | + asyncLocalStorage.run(store, common.mustCall(() => { |
| 62 | + const interval = setInterval(common.mustCall((received) => { |
| 63 | + assert.strictEqual(received, arg); |
| 64 | + clearInterval(interval); |
| 65 | + assert.strictEqual(asyncLocalStorage.getStore(), store); |
| 66 | + setImmediate(common.mustCall(() => { |
| 67 | + assertNoStore(interval); |
| 68 | + assert.strictEqual(interval._onTimeout, undefined); |
| 69 | + assert.strictEqual(interval._timerArgs, undefined); |
| 70 | + })); |
| 71 | + }), 1, arg); |
| 72 | + assert.strictEqual(getStore(interval, asyncLocalStorage), store); |
| 73 | + })); |
| 74 | +} |
| 75 | + |
| 76 | +// Test that setImmediate does not retain a reference to the async store, |
| 77 | +// callback, or arguments after firing. |
| 78 | +{ |
| 79 | + const asyncLocalStorage = new AsyncLocalStorage(); |
| 80 | + const store = {}; |
| 81 | + const arg = {}; |
| 82 | + asyncLocalStorage.run(store, common.mustCall(() => { |
| 83 | + const immediate = setImmediate(common.mustCall((received) => { |
| 84 | + assert.strictEqual(received, arg); |
| 85 | + setImmediate(common.mustCall(() => { |
| 86 | + assertNoStore(immediate); |
| 87 | + assert.strictEqual(immediate._onImmediate, undefined); |
| 88 | + assert.strictEqual(immediate._argv, undefined); |
| 89 | + })); |
| 90 | + }), arg); |
| 91 | + assert.strictEqual(getStore(immediate, asyncLocalStorage), store); |
| 92 | + })); |
| 93 | +} |
| 94 | + |
| 95 | +// Test that clearImmediate cleans up the store, callback, and arguments before |
| 96 | +// firing. |
| 97 | +{ |
| 98 | + const asyncLocalStorage = new AsyncLocalStorage(); |
| 99 | + const store = {}; |
| 100 | + const arg = {}; |
| 101 | + asyncLocalStorage.run(store, common.mustCall(() => { |
| 102 | + const immediate = setImmediate(common.mustNotCall(), arg); |
| 103 | + assert.strictEqual(getStore(immediate, asyncLocalStorage), store); |
| 104 | + clearImmediate(immediate); |
| 105 | + assertNoStore(immediate); |
| 106 | + assert.strictEqual(immediate._onImmediate, undefined); |
| 107 | + assert.strictEqual(immediate._argv, undefined); |
| 108 | + })); |
| 109 | +} |
0 commit comments