Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test to re-enter an AsyncResource
  • Loading branch information
Flarna committed Dec 16, 2019
commit 9ec397756e3fd5bcb05ce445a64233324bbf6fb1
45 changes: 45 additions & 0 deletions test/async-hooks/test-async-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,48 @@ const { AsyncLocal, AsyncResource } = require('async_hooks');
}));
}));
}

{
const exp = [
[ undefined, 'A', false ],
[ 'A', 'B', false ],
[ 'B', 'A', true ],
[ 'A', 'B', true ],
[ 'B', 'A', true ],
];

const act = [];
const asyncLocal = new AsyncLocal({
onChangedCb: (p, c, t) => act.push([p, c, t])
});

asyncLocal.value = 'A';
const asyncRes1 = new AsyncResource('Resource1');
const asyncRes2 = new AsyncResource('Resource2');
assert.strictEqual(act.length, 1);

asyncRes1.runInAsyncScope(common.mustCall(() => {
assert.strictEqual(asyncLocal.value, 'A');
asyncRes1.runInAsyncScope(common.mustCall(() => {
assert.strictEqual(asyncLocal.value, 'A');
asyncRes2.runInAsyncScope(common.mustCall(() => {
assert.strictEqual(asyncLocal.value, 'A');
asyncLocal.value = 'B';
assert.strictEqual(act.length, 2);
asyncRes1.runInAsyncScope(common.mustCall(() => {
assert.strictEqual(asyncLocal.value, 'A');
assert.strictEqual(act.length, 3);
}));
assert.strictEqual(act.length, 4);
assert.strictEqual(asyncLocal.value, 'B');
}));
assert.strictEqual(act.length, 5);
assert.strictEqual(asyncLocal.value, 'A');
}));
assert.strictEqual(asyncLocal.value, 'A');
}));

assert.strictEqual(act.length, 5);

assert.deepStrictEqual(act, exp);
}