summaryrefslogtreecommitdiff
path: root/test/async-hooks
diff options
context:
space:
mode:
Diffstat (limited to 'test/async-hooks')
-rw-r--r--test/async-hooks/test-callback-error.js2
-rw-r--r--test/async-hooks/test-embedder.api.async-resource.js24
-rw-r--r--test/async-hooks/test-emit-after-on-destroyed.js (renamed from test/async-hooks/test-embedder.api.async-resource.after-on-destroyed.js)38
-rw-r--r--test/async-hooks/test-emit-before-on-destroyed.js (renamed from test/async-hooks/test-embedder.api.async-resource.before-on-destroyed.js)38
-rw-r--r--test/async-hooks/test-improper-order.js (renamed from test/async-hooks/test-embedder.api.async-resource.improper-order.js)35
-rw-r--r--test/async-hooks/test-improper-unwind.js (renamed from test/async-hooks/test-embedder.api.async-resource.improper-unwind.js)34
6 files changed, 112 insertions, 59 deletions
diff --git a/test/async-hooks/test-callback-error.js b/test/async-hooks/test-callback-error.js
index 07293e0315..06d8cb7224 100644
--- a/test/async-hooks/test-callback-error.js
+++ b/test/async-hooks/test-callback-error.js
@@ -19,7 +19,7 @@ switch (arg) {
onbefore: common.mustCall(() => { throw new Error(arg); })
}).enable();
const resource = new async_hooks.AsyncResource(`${arg}_type`);
- resource.emitBefore();
+ resource.runInAsyncScope(() => {});
return;
case 'test_callback_abort':
diff --git a/test/async-hooks/test-embedder.api.async-resource.js b/test/async-hooks/test-embedder.api.async-resource.js
index 74d6c478c8..19c1b7187a 100644
--- a/test/async-hooks/test-embedder.api.async-resource.js
+++ b/test/async-hooks/test-embedder.api.async-resource.js
@@ -47,16 +47,16 @@ assert.strictEqual(typeof alcaEvent.asyncId(), 'number');
assert.notStrictEqual(alcaEvent.asyncId(), alcaTriggerId);
assert.strictEqual(alcaEvent.triggerAsyncId(), alcaTriggerId);
-alcaEvent.emitBefore();
-checkInvocations(alcazares, { init: 1, before: 1 },
- 'alcazares emitted before');
-alcaEvent.emitAfter();
+alcaEvent.runInAsyncScope(() => {
+ checkInvocations(alcazares, { init: 1, before: 1 },
+ 'alcazares emitted before');
+});
checkInvocations(alcazares, { init: 1, before: 1, after: 1 },
'alcazares emitted after');
-alcaEvent.emitBefore();
-checkInvocations(alcazares, { init: 1, before: 2, after: 1 },
- 'alcazares emitted before again');
-alcaEvent.emitAfter();
+alcaEvent.runInAsyncScope(() => {
+ checkInvocations(alcazares, { init: 1, before: 2, after: 1 },
+ 'alcazares emitted before again');
+});
checkInvocations(alcazares, { init: 1, before: 2, after: 2 },
'alcazares emitted after again');
alcaEvent.emitDestroy();
@@ -75,11 +75,11 @@ function tick1() {
assert.strictEqual(typeof poblado.uid, 'number');
assert.strictEqual(poblado.triggerAsyncId, pobTriggerId);
checkInvocations(poblado, { init: 1 }, 'poblado constructed');
- pobEvent.emitBefore();
- checkInvocations(poblado, { init: 1, before: 1 },
- 'poblado emitted before');
+ pobEvent.runInAsyncScope(() => {
+ checkInvocations(poblado, { init: 1, before: 1 },
+ 'poblado emitted before');
+ });
- pobEvent.emitAfter();
checkInvocations(poblado, { init: 1, before: 1, after: 1 },
'poblado emitted after');
diff --git a/test/async-hooks/test-embedder.api.async-resource.after-on-destroyed.js b/test/async-hooks/test-emit-after-on-destroyed.js
index a389eddf42..b015dacddb 100644
--- a/test/async-hooks/test-embedder.api.async-resource.after-on-destroyed.js
+++ b/test/async-hooks/test-emit-after-on-destroyed.js
@@ -1,13 +1,18 @@
+// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
-const async_hooks = require('async_hooks');
-const { AsyncResource } = async_hooks;
+const internal_async_hooks = require('internal/async_hooks');
const { spawn } = require('child_process');
const corruptedMsg = /async hook stack has become corrupted/;
const heartbeatMsg = /heartbeat: still alive/;
+const {
+ newAsyncId, getDefaultTriggerAsyncId,
+ emitInit, emitBefore, emitAfter, emitDestroy
+} = internal_async_hooks;
+
const initHooks = require('./init-hooks');
if (process.argv[2] === 'child') {
@@ -17,20 +22,29 @@ if (process.argv[2] === 'child') {
// Once 'destroy' has been emitted, we can no longer emit 'after'
// Emitting 'before', 'after' and then 'destroy'
- const event1 = new AsyncResource('event1', async_hooks.executionAsyncId());
- event1.emitBefore();
- event1.emitAfter();
- event1.emitDestroy();
+ {
+ const asyncId = newAsyncId();
+ const triggerId = getDefaultTriggerAsyncId();
+ emitInit(asyncId, 'event1', triggerId, {});
+ emitBefore(asyncId, triggerId);
+ emitAfter(asyncId);
+ emitDestroy(asyncId);
+ }
// Emitting 'after' after 'destroy'
- const event2 = new AsyncResource('event2', async_hooks.executionAsyncId());
- event2.emitDestroy();
-
- console.log('heartbeat: still alive');
- event2.emitAfter();
+ {
+ const asyncId = newAsyncId();
+ const triggerId = getDefaultTriggerAsyncId();
+ emitInit(asyncId, 'event2', triggerId, {});
+ emitDestroy(asyncId);
+ console.log('heartbeat: still alive');
+ emitAfter(asyncId);
+ }
} else {
- const args = process.argv.slice(1).concat('child');
+ const args = ['--expose-internals']
+ .concat(process.argv.slice(1))
+ .concat('child');
let errData = Buffer.from('');
let outData = Buffer.from('');
diff --git a/test/async-hooks/test-embedder.api.async-resource.before-on-destroyed.js b/test/async-hooks/test-emit-before-on-destroyed.js
index d6465a8445..36a50a050f 100644
--- a/test/async-hooks/test-embedder.api.async-resource.before-on-destroyed.js
+++ b/test/async-hooks/test-emit-before-on-destroyed.js
@@ -1,13 +1,18 @@
+// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
-const async_hooks = require('async_hooks');
-const { AsyncResource } = async_hooks;
+const internal_async_hooks = require('internal/async_hooks');
const { spawn } = require('child_process');
const corruptedMsg = /async hook stack has become corrupted/;
const heartbeatMsg = /heartbeat: still alive/;
+const {
+ newAsyncId, getDefaultTriggerAsyncId,
+ emitInit, emitBefore, emitAfter, emitDestroy
+} = internal_async_hooks;
+
const initHooks = require('./init-hooks');
if (process.argv[2] === 'child') {
@@ -17,20 +22,29 @@ if (process.argv[2] === 'child') {
// Once 'destroy' has been emitted, we can no longer emit 'before'
// Emitting 'before', 'after' and then 'destroy'
- const event1 = new AsyncResource('event1', async_hooks.executionAsyncId());
- event1.emitBefore();
- event1.emitAfter();
- event1.emitDestroy();
+ {
+ const asyncId = newAsyncId();
+ const triggerId = getDefaultTriggerAsyncId();
+ emitInit(asyncId, 'event1', triggerId, {});
+ emitBefore(asyncId, triggerId);
+ emitAfter(asyncId);
+ emitDestroy(asyncId);
+ }
// Emitting 'before' after 'destroy'
- const event2 = new AsyncResource('event2', async_hooks.executionAsyncId());
- event2.emitDestroy();
-
- console.log('heartbeat: still alive');
- event2.emitBefore();
+ {
+ const asyncId = newAsyncId();
+ const triggerId = getDefaultTriggerAsyncId();
+ emitInit(asyncId, 'event2', triggerId, {});
+ emitDestroy(asyncId);
+ console.log('heartbeat: still alive');
+ emitBefore(asyncId, triggerId);
+ }
} else {
- const args = process.argv.slice(1).concat('child');
+ const args = ['--expose-internals']
+ .concat(process.argv.slice(1))
+ .concat('child');
let errData = Buffer.from('');
let outData = Buffer.from('');
diff --git a/test/async-hooks/test-embedder.api.async-resource.improper-order.js b/test/async-hooks/test-improper-order.js
index 4a38d87ce2..a9d46917df 100644
--- a/test/async-hooks/test-embedder.api.async-resource.improper-order.js
+++ b/test/async-hooks/test-improper-order.js
@@ -1,13 +1,18 @@
+// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
-const async_hooks = require('async_hooks');
-const { AsyncResource } = async_hooks;
+const internal_async_hooks = require('internal/async_hooks');
const { spawn } = require('child_process');
const corruptedMsg = /async hook stack has become corrupted/;
const heartbeatMsg = /heartbeat: still alive/;
+const {
+ newAsyncId, getDefaultTriggerAsyncId,
+ emitInit, emitBefore, emitAfter
+} = internal_async_hooks;
+
const initHooks = require('./init-hooks');
if (process.argv[2] === 'child') {
@@ -17,18 +22,28 @@ if (process.argv[2] === 'child') {
// Async hooks enforce proper order of 'before' and 'after' invocations
// Proper ordering
- const event1 = new AsyncResource('event1', async_hooks.executionAsyncId());
- event1.emitBefore();
- event1.emitAfter();
+ {
+ const asyncId = newAsyncId();
+ const triggerId = getDefaultTriggerAsyncId();
+ emitInit(asyncId, 'event1', triggerId, {});
+ emitBefore(asyncId, triggerId);
+ emitAfter(asyncId);
+ }
// Improper ordering
// Emitting 'after' without 'before' which is illegal
- const event2 = new AsyncResource('event2', async_hooks.executionAsyncId());
-
- console.log('heartbeat: still alive');
- event2.emitAfter();
+ {
+ const asyncId = newAsyncId();
+ const triggerId = getDefaultTriggerAsyncId();
+ emitInit(asyncId, 'event2', triggerId, {});
+
+ console.log('heartbeat: still alive');
+ emitAfter(asyncId);
+ }
} else {
- const args = process.argv.slice(1).concat('child');
+ const args = ['--expose-internals']
+ .concat(process.argv.slice(1))
+ .concat('child');
let errData = Buffer.from('');
let outData = Buffer.from('');
diff --git a/test/async-hooks/test-embedder.api.async-resource.improper-unwind.js b/test/async-hooks/test-improper-unwind.js
index cb9e338905..a62512db97 100644
--- a/test/async-hooks/test-embedder.api.async-resource.improper-unwind.js
+++ b/test/async-hooks/test-improper-unwind.js
@@ -1,13 +1,18 @@
+// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
-const async_hooks = require('async_hooks');
-const { AsyncResource } = async_hooks;
+const internal_async_hooks = require('internal/async_hooks');
const { spawn } = require('child_process');
const corruptedMsg = /async hook stack has become corrupted/;
const heartbeatMsg = /heartbeat: still alive/;
+const {
+ newAsyncId, getDefaultTriggerAsyncId,
+ emitInit, emitBefore, emitAfter
+} = internal_async_hooks;
+
const initHooks = require('./init-hooks');
if (process.argv[2] === 'child') {
@@ -21,23 +26,28 @@ if (process.argv[2] === 'child') {
// The first test of the two below follows that rule,
// the second one doesn't.
- const event1 = new AsyncResource('event1', async_hooks.executionAsyncId());
- const event2 = new AsyncResource('event2', async_hooks.executionAsyncId());
+ const eventOneId = newAsyncId();
+ const eventTwoId = newAsyncId();
+ const triggerId = getDefaultTriggerAsyncId();
+ emitInit(eventOneId, 'event1', triggerId, {});
+ emitInit(eventTwoId, 'event2', triggerId, {});
// Proper unwind
- event1.emitBefore();
- event2.emitBefore();
- event2.emitAfter();
- event1.emitAfter();
+ emitBefore(eventOneId, triggerId);
+ emitBefore(eventTwoId, triggerId);
+ emitAfter(eventTwoId);
+ emitAfter(eventOneId);
// Improper unwind
- event1.emitBefore();
- event2.emitBefore();
+ emitBefore(eventOneId, triggerId);
+ emitBefore(eventTwoId, triggerId);
console.log('heartbeat: still alive');
- event1.emitAfter();
+ emitAfter(eventOneId);
} else {
- const args = process.argv.slice(1).concat('child');
+ const args = ['--expose-internals']
+ .concat(process.argv.slice(1))
+ .concat('child');
let errData = Buffer.from('');
let outData = Buffer.from('');