• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_close() {
34    const fh = await fs.open(__filename);
35    await fh.close();
36    await scheduler.yield();
37    await open_close();
38  }
39
40  // These async function calls never return as they are meant to continually
41  // open and close files until the worker is terminated.
42  open_close();
43  open_close();
44
45  parentPort.postMessage('terminate');
46}
47