1'use strict'; 2const common = require('../../common'); 3const assert = require('assert'); 4const { Worker, isMainThread, workerData } = require('worker_threads'); 5 6if (isMainThread) { 7 // Load the addon in the main thread first. 8 // This checks that N-API addons can be loaded from multiple contexts 9 // when they are not loaded through NAPI_MODULE(). 10 require(`./build/${common.buildType}/test_worker_terminate`); 11 12 const counter = new Int32Array(new SharedArrayBuffer(4)); 13 const worker = new Worker(__filename, { workerData: { counter } }); 14 worker.on('exit', common.mustCall(() => { 15 assert.strictEqual(counter[0], 1); 16 })); 17 worker.on('error', common.mustNotCall()); 18} else { 19 const { Test } = require(`./build/${common.buildType}/test_worker_terminate`); 20 21 const { counter } = workerData; 22 // Test() tries to call a function twice and asserts that the second call does 23 // not work because of a pending exception. 24 Test(() => { 25 Atomics.add(counter, 0, 1); 26 process.exit(); 27 }); 28} 29