Skip to content

Commit 5d58d52

Browse files
authored
test(node): Add some async tests to async strategies (#7808)
1 parent 4856685 commit 5d58d52

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

packages/node/test/async/domain.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,37 @@ describe('domains', () => {
3838
});
3939
});
4040

41+
test('async hub scope inheritance', async () => {
42+
setDomainAsyncContextStrategy();
43+
44+
async function addRandomExtra(hub: Hub, key: string): Promise<void> {
45+
return new Promise(resolve => {
46+
setTimeout(() => {
47+
hub.setExtra(key, Math.random());
48+
resolve();
49+
}, 100);
50+
});
51+
}
52+
53+
const globalHub = getCurrentHub();
54+
await addRandomExtra(globalHub, 'a');
55+
56+
await runWithAsyncContext(async hub1 => {
57+
expect(hub1).toEqual(globalHub);
58+
59+
await addRandomExtra(hub1, 'b');
60+
expect(hub1).not.toEqual(globalHub);
61+
62+
await runWithAsyncContext(async hub2 => {
63+
expect(hub2).toEqual(hub1);
64+
expect(hub2).not.toEqual(globalHub);
65+
66+
await addRandomExtra(hub1, 'c');
67+
expect(hub2).not.toEqual(hub1);
68+
});
69+
});
70+
});
71+
4172
test('hub single instance', () => {
4273
setDomainAsyncContextStrategy();
4374

packages/node/test/async/hooks.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ conditionalTest({ min: 12 })('async_hooks', () => {
4444
});
4545
});
4646

47+
test('async hub scope inheritance', async () => {
48+
setHooksAsyncContextStrategy();
49+
50+
async function addRandomExtra(hub: Hub, key: string): Promise<void> {
51+
return new Promise(resolve => {
52+
setTimeout(() => {
53+
hub.setExtra(key, Math.random());
54+
resolve();
55+
}, 100);
56+
});
57+
}
58+
59+
const globalHub = getCurrentHub();
60+
await addRandomExtra(globalHub, 'a');
61+
62+
await runWithAsyncContext(async hub1 => {
63+
expect(hub1).toEqual(globalHub);
64+
65+
await addRandomExtra(hub1, 'b');
66+
expect(hub1).not.toEqual(globalHub);
67+
68+
await runWithAsyncContext(async hub2 => {
69+
expect(hub2).toEqual(hub1);
70+
expect(hub2).not.toEqual(globalHub);
71+
72+
await addRandomExtra(hub1, 'c');
73+
expect(hub2).not.toEqual(hub1);
74+
});
75+
});
76+
});
77+
4778
test('context single instance', () => {
4879
setHooksAsyncContextStrategy();
4980

0 commit comments

Comments
 (0)