summaryrefslogtreecommitdiff
path: root/test/node-api/test_worker_terminate/test.js
blob: 2bfaab8e8eb0a9bf29d90c671b0576712a746627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'use strict';
const common = require('../../common');
const assert = require('assert');
const { Worker, isMainThread, workerData } = require('worker_threads');

if (isMainThread) {
  const counter = new Int32Array(new SharedArrayBuffer(4));
  const worker = new Worker(__filename, { workerData: { counter } });
  worker.on('exit', common.mustCall(() => {
    assert.strictEqual(counter[0], 1);
  }));
  worker.on('error', common.mustNotCall());
} else {
  const { Test } = require(`./build/${common.buildType}/test_worker_terminate`);

  const { counter } = workerData;
  // Test() tries to call a function twice and asserts that the second call does
  // not work because of a pending exception.
  Test(() => {
    Atomics.add(counter, 0, 1);
    process.exit();
  });
}