• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4const common = require('../common');
5const { internalBinding } = require('internal/test/binding');
6const async_wrap = internalBinding('async_wrap');
7const assert = require('assert');
8const async_hooks = require('async_hooks');
9const RUNS = 5;
10let test_id = null;
11let run_cntr = 0;
12let hooks = null;
13
14process.on('beforeExit', common.mustCall(() => {
15  process.removeAllListeners('uncaughtException');
16  hooks.disable();
17  assert.strictEqual(test_id, null);
18  assert.strictEqual(run_cntr, RUNS);
19}));
20
21
22hooks = async_hooks.createHook({
23  destroy(id) {
24    if (id === test_id) {
25      run_cntr++;
26      test_id = null;
27    }
28  },
29}).enable();
30
31
32(function runner(n) {
33  assert.strictEqual(test_id, null);
34  if (n <= 0) return;
35
36  test_id = (Math.random() * 1e9) >>> 0;
37  async_wrap.queueDestroyAsyncId(test_id);
38  setImmediate(common.mustCall(runner), n - 1);
39})(RUNS);
40