2019-03-08 15:42:21 +00:00
|
|
|
// Flags: --expose-internals
|
2016-11-22 16:13:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2019-03-08 15:42:21 +00:00
|
|
|
const internal_async_hooks = require('internal/async_hooks');
|
2016-11-22 16:13:44 +00:00
|
|
|
const { spawn } = require('child_process');
|
|
|
|
const corruptedMsg = /async hook stack has become corrupted/;
|
|
|
|
const heartbeatMsg = /heartbeat: still alive/;
|
|
|
|
|
2019-03-08 15:42:21 +00:00
|
|
|
const {
|
|
|
|
newAsyncId, getDefaultTriggerAsyncId,
|
2022-11-21 17:43:47 +00:00
|
|
|
emitInit, emitBefore, emitAfter, emitDestroy,
|
2019-03-08 15:42:21 +00:00
|
|
|
} = internal_async_hooks;
|
|
|
|
|
2016-11-22 16:13:44 +00:00
|
|
|
const initHooks = require('./init-hooks');
|
|
|
|
|
|
|
|
if (process.argv[2] === 'child') {
|
|
|
|
const hooks = initHooks();
|
|
|
|
hooks.enable();
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Once 'destroy' has been emitted, we can no longer emit 'before'
|
2016-11-22 16:13:44 +00:00
|
|
|
|
|
|
|
// Emitting 'before', 'after' and then 'destroy'
|
2019-03-08 15:42:21 +00:00
|
|
|
{
|
|
|
|
const asyncId = newAsyncId();
|
|
|
|
const triggerId = getDefaultTriggerAsyncId();
|
|
|
|
emitInit(asyncId, 'event1', triggerId, {});
|
|
|
|
emitBefore(asyncId, triggerId);
|
|
|
|
emitAfter(asyncId);
|
|
|
|
emitDestroy(asyncId);
|
|
|
|
}
|
2016-11-22 16:13:44 +00:00
|
|
|
|
|
|
|
// Emitting 'before' after 'destroy'
|
2019-03-08 15:42:21 +00:00
|
|
|
{
|
|
|
|
const asyncId = newAsyncId();
|
|
|
|
const triggerId = getDefaultTriggerAsyncId();
|
|
|
|
emitInit(asyncId, 'event2', triggerId, {});
|
|
|
|
emitDestroy(asyncId);
|
2016-11-22 16:13:44 +00:00
|
|
|
|
2019-03-08 15:42:21 +00:00
|
|
|
console.log('heartbeat: still alive');
|
|
|
|
emitBefore(asyncId, triggerId);
|
|
|
|
}
|
2016-11-22 16:13:44 +00:00
|
|
|
} else {
|
2019-03-08 15:42:21 +00:00
|
|
|
const args = ['--expose-internals']
|
|
|
|
.concat(process.argv.slice(1))
|
|
|
|
.concat('child');
|
2016-11-22 16:13:44 +00: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 ]); });
|
|
|
|
|
|
|
|
child.on('close', common.mustCall((code) => {
|
2017-05-26 15:53:06 +00:00
|
|
|
assert.strictEqual(code, 1);
|
2021-08-29 08:14:22 +00: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 16:13:44 +00:00
|
|
|
}));
|
|
|
|
}
|