@@ -5,12 +5,12 @@ const common = require('./common');
5
5
6
6
// we only check async hooks on 8.x an higher were
7
7
// they are closer to working properly
8
- const nodeVersion = process . versions . node . split ( '.' ) [ 0 ]
9
- let async_hooks = undefined ;
10
- function checkAsyncHooks ( ) {
8
+ const nodeVersion = process . versions . node . split ( '.' ) [ 0 ] ;
9
+ let asyncHooks ;
10
+ function checkAsyncHooks ( ) {
11
11
if ( nodeVersion >= 8 ) {
12
- if ( async_hooks == undefined ) {
13
- async_hooks = require ( 'async_hooks' ) ;
12
+ if ( asyncHooks = == undefined ) {
13
+ asyncHooks = require ( 'async_hooks' ) ;
14
14
}
15
15
return true ;
16
16
}
@@ -19,68 +19,104 @@ function checkAsyncHooks() {
19
19
20
20
module . exports = common . runTest ( test ) ;
21
21
22
- function installAsyncHooksForTest ( ) {
22
+ function installAsyncHooksForTest ( resName ) {
23
23
return new Promise ( ( resolve , reject ) => {
24
24
let id ;
25
25
const events = [ ] ;
26
26
/**
27
27
* TODO(legendecas): investigate why resolving & disabling hooks in
28
28
* destroy callback causing crash with case 'callbackscope.js'.
29
29
*/
30
- let hook ;
31
30
let destroyed = false ;
32
- const interval = setInterval ( ( ) => {
33
- if ( destroyed ) {
34
- hook . disable ( ) ;
35
- clearInterval ( interval ) ;
36
- resolve ( events ) ;
37
- }
38
- } , 10 ) ;
39
-
40
- hook = async_hooks . createHook ( {
41
- init ( asyncId , type , triggerAsyncId , resource ) {
42
- if ( id === undefined && type === 'async_context_test' ) {
31
+ const hook = asyncHooks . createHook ( {
32
+ init ( asyncId , type , triggerAsyncId , resource ) {
33
+ if ( id === undefined && type === resName ) {
43
34
id = asyncId ;
44
35
events . push ( { eventName : 'init' , type, triggerAsyncId, resource } ) ;
45
36
}
46
37
} ,
47
- before ( asyncId ) {
38
+ before ( asyncId ) {
48
39
if ( asyncId === id ) {
49
40
events . push ( { eventName : 'before' } ) ;
50
41
}
51
42
} ,
52
- after ( asyncId ) {
43
+ after ( asyncId ) {
53
44
if ( asyncId === id ) {
54
45
events . push ( { eventName : 'after' } ) ;
55
46
}
56
47
} ,
57
- destroy ( asyncId ) {
48
+ destroy ( asyncId ) {
58
49
if ( asyncId === id ) {
59
50
events . push ( { eventName : 'destroy' } ) ;
60
51
destroyed = true ;
61
52
}
62
53
}
63
54
} ) . enable ( ) ;
55
+
56
+ const interval = setInterval ( ( ) => {
57
+ if ( destroyed ) {
58
+ hook . disable ( ) ;
59
+ clearInterval ( interval ) ;
60
+ resolve ( events ) ;
61
+ }
62
+ } , 10 ) ;
64
63
} ) ;
65
64
}
66
65
67
- function test ( binding ) {
68
- if ( ! checkAsyncHooks ( ) ) {
69
- return ;
70
- }
71
-
72
- const hooks = installAsyncHooksForTest ( ) ;
73
- const triggerAsyncId = async_hooks . executionAsyncId ( ) ;
74
- binding . asynccontext . makeCallback ( common . mustCall ( ) , { foo : 'foo' } ) ;
75
- return hooks . then ( actual => {
66
+ async function makeCallbackWithResource ( binding ) {
67
+ const hooks = installAsyncHooksForTest ( 'async_context_test' ) ;
68
+ const triggerAsyncId = asyncHooks . executionAsyncId ( ) ;
69
+ await new Promise ( ( resolve , reject ) => {
70
+ binding . asynccontext . makeCallback ( common . mustCall ( ) , { foo : 'foo' } ) ;
71
+ hooks . then ( actual => {
76
72
assert . deepStrictEqual ( actual , [
77
- { eventName : 'init' ,
73
+ {
74
+ eventName : 'init' ,
78
75
type : 'async_context_test' ,
79
76
triggerAsyncId : triggerAsyncId ,
80
- resource : { foo : 'foo' } } ,
77
+ resource : { foo : 'foo' }
78
+ } ,
79
+ { eventName : 'before' } ,
80
+ { eventName : 'after' } ,
81
+ { eventName : 'destroy' }
82
+ ] ) ;
83
+ } ) . catch ( common . mustNotCall ( ) ) ;
84
+ resolve ( ) ;
85
+ } ) ;
86
+ }
87
+
88
+ async function makeCallbackWithoutResource ( binding ) {
89
+ const hooks = installAsyncHooksForTest ( 'async_context_no_res_test' ) ;
90
+ const triggerAsyncId = asyncHooks . executionAsyncId ( ) ;
91
+ await new Promise ( ( resolve , reject ) => {
92
+ binding . asynccontext . makeCallbackNoResource ( common . mustCall ( ) ) ;
93
+ hooks . then ( actual => {
94
+ assert . deepStrictEqual ( actual , [
95
+ {
96
+ eventName : 'init' ,
97
+ type : 'async_context_no_res_test' ,
98
+ triggerAsyncId : triggerAsyncId ,
99
+ resource : { }
100
+ } ,
81
101
{ eventName : 'before' } ,
82
102
{ eventName : 'after' } ,
83
103
{ eventName : 'destroy' }
84
104
] ) ;
85
- } ) . catch ( common . mustNotCall ( ) ) ;
105
+ } ) . catch ( common . mustNotCall ( ) ) ;
106
+ resolve ( ) ;
107
+ } ) ;
108
+ }
109
+
110
+ function assertAsyncContextReturnsCorrectEnv ( binding ) {
111
+ assert . strictEqual ( binding . asynccontext . asyncCxtReturnCorrectEnv ( ) , true ) ;
112
+ }
113
+
114
+ async function test ( binding ) {
115
+ if ( ! checkAsyncHooks ( ) ) {
116
+ return ;
117
+ }
118
+
119
+ await makeCallbackWithResource ( binding ) ;
120
+ await makeCallbackWithoutResource ( binding ) ;
121
+ assertAsyncContextReturnsCorrectEnv ( binding ) ;
86
122
}
0 commit comments