xref: /third_party/node/test/parallel/test-worker-ref.js
  • Home
  • History
  • Annotate Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
  • only in /third_party/node/test/parallel/
11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst { Worker } = require('worker_threads');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Test that calling worker.unref() leads to 'beforeExit' being emitted, and
61cb0ef41Sopenharmony_ci// that we can resurrect the worker using worker.ref() from there.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst w = new Worker(`
91cb0ef41Sopenharmony_ciconst { parentPort } = require('worker_threads');
101cb0ef41Sopenharmony_ciparentPort.once('message', (msg) => {
111cb0ef41Sopenharmony_ci  parentPort.postMessage(msg);
121cb0ef41Sopenharmony_ci});
131cb0ef41Sopenharmony_ci`, { eval: true });
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciprocess.once('beforeExit', common.mustCall(() => {
161cb0ef41Sopenharmony_ci  console.log('beforeExit');
171cb0ef41Sopenharmony_ci  w.ref();
181cb0ef41Sopenharmony_ci  w.postMessage({ hello: 'world' });
191cb0ef41Sopenharmony_ci}));
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciw.once('message', common.mustCall((msg) => {
221cb0ef41Sopenharmony_ci  console.log('message', msg);
231cb0ef41Sopenharmony_ci}));
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciw.on('exit', common.mustCall(() => {
261cb0ef41Sopenharmony_ci  console.log('exit');
271cb0ef41Sopenharmony_ci}));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciw.unref();
30

Indexes created Thu Nov 07 10:32:03 CST 2024