summaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-hasref.js
blob: 51593b14725f5b1cfc9a613ce0e3644dde4d55e8 (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
'use strict';
const common = require('../common');

const { Worker } = require('worker_threads');
const { createHook } = require('async_hooks');
const { strictEqual } = require('assert');

let handle;

createHook({
  init(asyncId, type, triggerAsyncId, resource) {
    if (type === 'WORKER') {
      handle = resource;
      this.disable();
    }
  }
}).enable();

const w = new Worker('', { eval: true });

strictEqual(handle.hasRef(), true);
w.unref();
strictEqual(handle.hasRef(), false);
w.ref();
strictEqual(handle.hasRef(), true);

w.on('exit', common.mustCall((exitCode) => {
  strictEqual(exitCode, 0);
  strictEqual(handle.hasRef(), true);
  setTimeout(common.mustCall(() => {
    strictEqual(handle.hasRef(), undefined);
  }), 0);
}));