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