|
| 1 | +'use strict'; |
| 2 | +const common = require('../../common'); |
| 3 | + |
| 4 | +if (common.isAIX) { |
| 5 | + common.skip('AIX is not supported by libuv'); |
| 6 | +} |
| 7 | + |
| 8 | +const assert = require('node:assert'); |
| 9 | +const { parentPort, Worker, isMainThread } = require('node:worker_threads'); |
| 10 | +const bindingPath = require.resolve(`./build/${common.buildType}/binding`); |
| 11 | +const binding = require(bindingPath); |
| 12 | + |
| 13 | +if (isMainThread) { |
| 14 | + assert.strictEqual(binding.getThreadName(), 'MainThread'); |
| 15 | + |
| 16 | + const worker = new Worker(__filename); |
| 17 | + worker.on('message', common.mustCall((data) => { |
| 18 | + assert.strictEqual(data, 'WorkerThread'); |
| 19 | + })); |
| 20 | + worker.on('error', common.mustNotCall()); |
| 21 | + worker.on('exit', common.mustCall((code) => { |
| 22 | + assert.strictEqual(code, 0); |
| 23 | + })); |
| 24 | + |
| 25 | + const namedWorker = new Worker(__filename, { name: 'NamedThread' }); |
| 26 | + namedWorker.on('message', common.mustCall((data) => { |
| 27 | + assert.strictEqual(data, 'NamedThread'); |
| 28 | + })); |
| 29 | + namedWorker.on('error', common.mustNotCall()); |
| 30 | + namedWorker.on('exit', common.mustCall((code) => { |
| 31 | + assert.strictEqual(code, 0); |
| 32 | + })); |
| 33 | +} else { |
| 34 | + parentPort.postMessage(binding.getThreadName()); |
| 35 | +} |
0 commit comments