• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4const common = require('../common');
5const assert = require('assert');
6const internal_async_hooks = require('internal/async_hooks');
7const { spawn } = require('child_process');
8const corruptedMsg = /async hook stack has become corrupted/;
9const heartbeatMsg = /heartbeat: still alive/;
10
11const {
12  newAsyncId, getDefaultTriggerAsyncId,
13  emitInit, emitBefore, emitAfter
14} = internal_async_hooks;
15
16const initHooks = require('./init-hooks');
17
18if (process.argv[2] === 'child') {
19  const hooks = initHooks();
20  hooks.enable();
21
22  // In both the below two cases 'before' of event2 is nested inside 'before'
23  // of event1.
24  // Therefore the 'after' of event2 needs to occur before the
25  // 'after' of event 1.
26  // The first test of the two below follows that rule,
27  // the second one doesn't.
28
29  const eventOneId = newAsyncId();
30  const eventTwoId = newAsyncId();
31  const triggerId = getDefaultTriggerAsyncId();
32  emitInit(eventOneId, 'event1', triggerId, {});
33  emitInit(eventTwoId, 'event2', triggerId, {});
34
35  // Proper unwind
36  emitBefore(eventOneId, triggerId);
37  emitBefore(eventTwoId, triggerId);
38  emitAfter(eventTwoId);
39  emitAfter(eventOneId);
40
41  // Improper unwind
42  emitBefore(eventOneId, triggerId);
43  emitBefore(eventTwoId, triggerId);
44
45  console.log('heartbeat: still alive');
46  emitAfter(eventOneId);
47} else {
48  const args = ['--expose-internals']
49    .concat(process.argv.slice(1))
50    .concat('child');
51  let errData = Buffer.from('');
52  let outData = Buffer.from('');
53
54  const child = spawn(process.execPath, args);
55  child.stderr.on('data', (d) => { errData = Buffer.concat([ errData, d ]); });
56  child.stdout.on('data', (d) => { outData = Buffer.concat([ outData, d ]); });
57
58  child.on('close', common.mustCall((code) => {
59    assert.strictEqual(code, 1);
60    assert.ok(heartbeatMsg.test(outData.toString()),
61              'did not crash until we reached offending line of code ' +
62              `(found ${outData})`);
63    assert.ok(corruptedMsg.test(errData.toString()),
64              'printed error contains corrupted message ' +
65              `(found ${errData})`);
66  }));
67}
68