summaryrefslogtreecommitdiff
path: root/test/parallel/test-async-wrap-destroyid.js
blob: 25a33246e7756c0390f76ac8e67d25fbf686469a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Flags: --expose-internals
'use strict';

const common = require('../common');
const { internalBinding } = require('internal/test/binding');
const async_wrap = internalBinding('async_wrap');
const assert = require('assert');
const async_hooks = require('async_hooks');
const RUNS = 5;
let test_id = null;
let run_cntr = 0;
let hooks = null;

process.on('beforeExit', common.mustCall(() => {
  process.removeAllListeners('uncaughtException');
  hooks.disable();
  assert.strictEqual(test_id, null);
  assert.strictEqual(run_cntr, RUNS);
}));


hooks = async_hooks.createHook({
  destroy(id) {
    if (id === test_id) {
      run_cntr++;
      test_id = null;
    }
  },
}).enable();


(function runner(n) {
  assert.strictEqual(test_id, null);
  if (n <= 0) return;

  test_id = (Math.random() * 1e9) >>> 0;
  async_wrap.queueDestroyAsyncId(test_id);
  setImmediate(common.mustCall(runner), n - 1);
})(RUNS);