2019-03-08 16:42:21 +01:00
|
|
|
// Flags: --expose-internals
|
2016-11-22 17:13:44 +01: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 17:13:44 +01: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,
|
|
|
|
emitInit, emitBefore, emitAfter, emitDestroy
|
|
|
|
} = internal_async_hooks;
|
|
|
|
|
2016-11-22 17:13:44 +01:00
|
|
|
const initHooks = require('./init-hooks');
|
|
|
|
|
|
|
|
if (process.argv[2] === 'child') {
|
|
|
|
const hooks = initHooks();
|
|
|
|
hooks.enable();
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// Once 'destroy' has been emitted, we can no longer emit 'after'
|
2016-11-22 17:13:44 +01:00
|
|
|
|
|
|
|
// Emitting 'before', 'after' and then 'destroy'
|
2019-03-08 16:42:21 +01:00
|
|
|
{
|
|
|
|
const asyncId = newAsyncId();
|
|
|
|
const triggerId = getDefaultTriggerAsyncId();
|
|
|
|
emitInit(asyncId, 'event1', triggerId, {});
|
|
|
|
emitBefore(asyncId, triggerId);
|
|
|
|
emitAfter(asyncId);
|
|
|
|
emitDestroy(asyncId);
|
|
|
|
}
|
2016-11-22 17:13:44 +01:00
|
|
|
|
|
|
|
// Emitting 'after' after 'destroy'
|
2019-03-08 16:42:21 +01:00
|
|
|
{
|
|
|
|
const asyncId = newAsyncId();
|
|
|
|
const triggerId = getDefaultTriggerAsyncId();
|
|
|
|
emitInit(asyncId, 'event2', triggerId, {});
|
|
|
|
emitDestroy(asyncId);
|
2016-11-22 17:13:44 +01:00
|
|
|
|
2019-03-08 16:42:21 +01:00
|
|
|
console.log('heartbeat: still alive');
|
|
|
|
emitAfter(asyncId);
|
|
|
|
}
|
2016-11-22 17:13:44 +01:00
|
|
|
} else {
|
2019-03-08 16:42:21 +01:00
|
|
|
const args = ['--expose-internals']
|
|
|
|
.concat(process.argv.slice(1))
|
|
|
|
.concat('child');
|
2016-11-22 17:13:44 +01: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 17:53:06 +02:00
|
|
|
assert.strictEqual(code, 1);
|
2016-11-22 17:13:44 +01:00
|
|
|
assert.ok(heartbeatMsg.test(outData.toString()),
|
2017-05-26 17:53:06 +02:00
|
|
|
'did not crash until we reached offending line of code ' +
|
|
|
|
`(found ${outData})`);
|
2016-11-22 17:13:44 +01:00
|
|
|
assert.ok(corruptedMsg.test(errData.toString()),
|
2017-05-26 17:53:06 +02:00
|
|
|
'printed error contains corrupted message ' +
|
|
|
|
`(found ${errData})`);
|
2016-11-22 17:13:44 +01:00
|
|
|
}));
|
|
|
|
}
|