11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst { Worker } = require('worker_threads'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// Do not use isMainThread so that this test itself can be run inside a Worker. 71cb0ef41Sopenharmony_ciif (!process.env.HAS_STARTED_WORKER) { 81cb0ef41Sopenharmony_ci process.env.HAS_STARTED_WORKER = 1; 91cb0ef41Sopenharmony_ci const w = new Worker(__filename); 101cb0ef41Sopenharmony_ci w.on('message', common.mustNotCall()); 111cb0ef41Sopenharmony_ci w.on('error', common.mustCall((err) => { 121cb0ef41Sopenharmony_ci console.log(err.message); 131cb0ef41Sopenharmony_ci assert.match(String(err), /^Error: foo$/); 141cb0ef41Sopenharmony_ci })); 151cb0ef41Sopenharmony_ci w.on('exit', common.mustCall((code) => { 161cb0ef41Sopenharmony_ci // uncaughtException is code 1 171cb0ef41Sopenharmony_ci assert.strictEqual(code, 1); 181cb0ef41Sopenharmony_ci })); 191cb0ef41Sopenharmony_ci} else { 201cb0ef41Sopenharmony_ci // Cannot use common.mustCall as it cannot catch this 211cb0ef41Sopenharmony_ci let called = false; 221cb0ef41Sopenharmony_ci process.on('exit', (code) => { 231cb0ef41Sopenharmony_ci if (!called) { 241cb0ef41Sopenharmony_ci called = true; 251cb0ef41Sopenharmony_ci } else { 261cb0ef41Sopenharmony_ci assert.fail('Exit callback called twice in worker'); 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci }); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci setTimeout(() => assert.fail('Timeout executed after uncaughtException'), 311cb0ef41Sopenharmony_ci 2000); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci setImmediate(() => { 341cb0ef41Sopenharmony_ci throw new Error('foo'); 351cb0ef41Sopenharmony_ci }); 361cb0ef41Sopenharmony_ci} 37