1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const fs = require('fs/promises'); 6const { scheduler } = require('timers/promises'); 7const { parentPort, Worker } = require('worker_threads'); 8 9const MAX_ITERATIONS = 5; 10const MAX_THREADS = 6; 11 12// Do not use isMainThread so that this test itself can be run inside a Worker. 13if (!process.env.HAS_STARTED_WORKER) { 14 process.env.HAS_STARTED_WORKER = 1; 15 16 function spinWorker(iter) { 17 const w = new Worker(__filename); 18 w.on('message', common.mustCall((msg) => { 19 assert.strictEqual(msg, 'terminate'); 20 w.terminate(); 21 })); 22 23 w.on('exit', common.mustCall(() => { 24 if (iter < MAX_ITERATIONS) 25 spinWorker(++iter); 26 })); 27 } 28 29 for (let i = 0; i < MAX_THREADS; i++) { 30 spinWorker(0); 31 } 32} else { 33 async function open_nok() { 34 await assert.rejects( 35 fs.open('this file does not exist'), 36 { 37 code: 'ENOENT', 38 syscall: 'open' 39 } 40 ); 41 await scheduler.yield(); 42 await open_nok(); 43 } 44 45 // These async function calls never return as they are meant to continually 46 // open nonexistent files until the worker is terminated. 47 open_nok(); 48 open_nok(); 49 50 parentPort.postMessage('terminate'); 51} 52