2019-03-08 16:42:21 +01:00
|
|
|
// Flags: --expose-internals
|
2016-11-22 13:13:44 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2019-03-08 16:42:21 +01:00
|
|
|
const internal_async_hooks = require('internal/async_hooks');
|
2016-11-22 13:13:44 -03:00
|
|
|
const { spawn } = require('child_process');
|
|
|
|
const corruptedMsg = /async hook stack has become corrupted/;
|
|
|
|
const heartbeatMsg = /heartbeat: still alive/;
|
|
|
|
|
2019-03-08 16:42:21 +01:00
|
|
|
const {
|
|
|
|
newAsyncId, getDefaultTriggerAsyncId,
|
2022-11-21 12:43:47 -05:00
|
|
|
emitInit, emitBefore, emitAfter,
|
2019-03-08 16:42:21 +01:00
|
|
|
} = internal_async_hooks;
|
|
|
|
|
2016-11-22 13:13:44 -03:00
|
|
|
const initHooks = require('./init-hooks');
|
|
|
|
|
|
|
|
if (process.argv[2] === 'child') {
|
|
|
|
const hooks = initHooks();
|
|
|
|
hooks.enable();
|
|
|
|
|
|
|
|
// In both the below two cases 'before' of event2 is nested inside 'before'
|
|
|
|
// of event1.
|
|
|
|
// Therefore the 'after' of event2 needs to occur before the
|
|
|
|
// 'after' of event 1.
|
|
|
|
// The first test of the two below follows that rule,
|
2018-10-06 21:09:29 -04:00
|
|
|
// the second one doesn't.
|
2016-11-22 13:13:44 -03:00
|
|
|
|
2019-03-08 16:42:21 +01:00
|
|
|
const eventOneId = newAsyncId();
|
|
|
|
const eventTwoId = newAsyncId();
|
|
|
|
const triggerId = getDefaultTriggerAsyncId();
|
|
|
|
emitInit(eventOneId, 'event1', triggerId, {});
|
|
|
|
emitInit(eventTwoId, 'event2', triggerId, {});
|
2016-11-22 13:13:44 -03:00
|
|
|
|
|
|
|
// Proper unwind
|
2019-03-08 16:42:21 +01:00
|
|
|
emitBefore(eventOneId, triggerId);
|
|
|
|
emitBefore(eventTwoId, triggerId);
|
|
|
|
emitAfter(eventTwoId);
|
|
|
|
emitAfter(eventOneId);
|
2016-11-22 13:13:44 -03:00
|
|
|
|
|
|
|
// Improper unwind
|
2019-03-08 16:42:21 +01:00
|
|
|
emitBefore(eventOneId, triggerId);
|
|
|
|
emitBefore(eventTwoId, triggerId);
|
2016-11-22 13:13:44 -03:00
|
|
|
|
|
|
|
console.log('heartbeat: still alive');
|
2019-03-08 16:42:21 +01:00
|
|
|
emitAfter(eventOneId);
|
2016-11-22 13:13:44 -03:00
|
|
|
} else {
|
2019-03-08 16:42:21 +01:00
|
|
|
const args = ['--expose-internals']
|
|
|
|
.concat(process.argv.slice(1))
|
|
|
|
.concat('child');
|
2016-11-22 13:13:44 -03:00
|
|
|
let errData = Buffer.from('');
|
|
|
|
let outData = Buffer.from('');
|
|
|
|
|
|
|
|
const child = spawn(process.execPath, args);
|
|
|
|
child.stderr.on('data', (d) => { errData = Buffer.concat([ errData, d ]); });
|
|
|
|
child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });
|
|
|
|
|
2025-05-29 13:10:11 +05:30
|
|
|
child.on('close', common.mustCall((code, signal) => {
|
2025-06-08 18:47:08 +05:30
|
|
|
if ((common.isAIX ||
|
|
|
|
(common.isLinux && process.arch === 'x64')) &&
|
|
|
|
signal === 'SIGABRT') {
|
|
|
|
// XXX: The child process could be aborted due to unknown reasons. Work around it.
|
2025-05-29 13:10:11 +05:30
|
|
|
} else {
|
2025-06-08 18:47:08 +05:30
|
|
|
assert.strictEqual(signal, null);
|
2025-05-29 13:10:11 +05:30
|
|
|
assert.strictEqual(code, 1);
|
|
|
|
}
|
2021-08-29 10:14:22 +02:00
|
|
|
assert.match(outData.toString(), heartbeatMsg,
|
|
|
|
'did not crash until we reached offending line of code ' +
|
|
|
|
`(found ${outData})`);
|
|
|
|
assert.match(errData.toString(), corruptedMsg,
|
|
|
|
'printed error contains corrupted message ' +
|
|
|
|
`(found ${errData})`);
|
2016-11-22 13:13:44 -03:00
|
|
|
}));
|
|
|
|
}
|