11'use strict' ;
2- const buildType = process . config . target_defaults . default_configuration ;
2+
33const assert = require ( 'assert' ) ;
44const common = require ( './common' ) ;
55
@@ -17,14 +17,27 @@ function checkAsyncHooks() {
1717 return false ;
1818}
1919
20- test ( require ( `./build/${ buildType } /binding.node` ) ) ;
21- test ( require ( `./build/${ buildType } /binding_noexcept.node` ) ) ;
20+ module . exports = common . runTest ( test ) ;
2221
2322function installAsyncHooksForTest ( ) {
2423 return new Promise ( ( resolve , reject ) => {
2524 let id ;
2625 const events = [ ] ;
27- const hook = async_hooks . createHook ( {
26+ /**
27+ * TODO(legendecas): investigate why resolving & disabling hooks in
28+ * destroy callback causing crash with case 'callbackscope.js'.
29+ */
30+ let hook ;
31+ 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 ( {
2841 init ( asyncId , type , triggerAsyncId , resource ) {
2942 if ( id === undefined && type === 'async_context_test' ) {
3043 id = asyncId ;
@@ -44,30 +57,30 @@ function installAsyncHooksForTest() {
4457 destroy ( asyncId ) {
4558 if ( asyncId === id ) {
4659 events . push ( { eventName : 'destroy' } ) ;
47- hook . disable ( ) ;
48- resolve ( events ) ;
60+ destroyed = true ;
4961 }
5062 }
5163 } ) . enable ( ) ;
5264 } ) ;
5365}
5466
5567function test ( binding ) {
56- binding . asynccontext . makeCallback ( common . mustCall ( ) , { foo : 'foo' } ) ;
57- if ( ! checkAsyncHooks ( ) )
68+ if ( ! checkAsyncHooks ( ) ) {
5869 return ;
70+ }
5971
6072 const hooks = installAsyncHooksForTest ( ) ;
6173 const triggerAsyncId = async_hooks . executionAsyncId ( ) ;
62- hooks . then ( actual => {
63- assert . deepStrictEqual ( actual , [
64- { eventName : 'init' ,
65- type : 'async_context_test' ,
66- triggerAsyncId : triggerAsyncId ,
67- resource : { foo : 'foo' } } ,
68- { eventName : 'before' } ,
69- { eventName : 'after' } ,
70- { eventName : 'destroy' }
71- ] ) ;
74+ binding . asynccontext . makeCallback ( common . mustCall ( ) , { foo : 'foo' } ) ;
75+ return hooks . then ( actual => {
76+ assert . deepStrictEqual ( actual , [
77+ { eventName : 'init' ,
78+ type : 'async_context_test' ,
79+ triggerAsyncId : triggerAsyncId ,
80+ resource : { foo : 'foo' } } ,
81+ { eventName : 'before' } ,
82+ { eventName : 'after' } ,
83+ { eventName : 'destroy' }
84+ ] ) ;
7285 } ) . catch ( common . mustNotCall ( ) ) ;
7386}
0 commit comments