11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst { Worker, isMainThread, workerData } = require('worker_threads'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciif (isMainThread) { 71cb0ef41Sopenharmony_ci // Load the addon in the main thread first. 81cb0ef41Sopenharmony_ci // This checks that N-API addons can be loaded from multiple contexts 91cb0ef41Sopenharmony_ci // when they are not loaded through NAPI_MODULE(). 101cb0ef41Sopenharmony_ci require(`./build/${common.buildType}/test_worker_terminate`); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci const counter = new Int32Array(new SharedArrayBuffer(4)); 131cb0ef41Sopenharmony_ci const worker = new Worker(__filename, { workerData: { counter } }); 141cb0ef41Sopenharmony_ci worker.on('exit', common.mustCall(() => { 151cb0ef41Sopenharmony_ci assert.strictEqual(counter[0], 1); 161cb0ef41Sopenharmony_ci })); 171cb0ef41Sopenharmony_ci worker.on('error', common.mustNotCall()); 181cb0ef41Sopenharmony_ci} else { 191cb0ef41Sopenharmony_ci const { Test } = require(`./build/${common.buildType}/test_worker_terminate`); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci const { counter } = workerData; 221cb0ef41Sopenharmony_ci // Test() tries to call a function and asserts it fails because of a 231cb0ef41Sopenharmony_ci // pending termination exception. 241cb0ef41Sopenharmony_ci Test(() => { 251cb0ef41Sopenharmony_ci Atomics.add(counter, 0, 1); 261cb0ef41Sopenharmony_ci process.exit(); 271cb0ef41Sopenharmony_ci }); 281cb0ef41Sopenharmony_ci} 29