Skip to content

Commit 9431980

Browse files
committed
fixup! test: add main-thread name test
1 parent 8ebe0d7 commit 9431980

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

test/addons/uv-thread-name/binding.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#include <node.h>
2-
#include <v8.h>
32
#include <uv.h>
3+
#include <v8.h>
44

55
using v8::FunctionCallbackInfo;
66
using v8::Isolate;
77
using v8::String;
88
using v8::Value;
99

10-
void GetMainThreadName(const FunctionCallbackInfo<Value>& args) {
10+
void GetThreadName(const FunctionCallbackInfo<Value>& args) {
1111
Isolate* isolate = args.GetIsolate();
1212
uv_thread_t thread;
1313
char thread_name[16];
@@ -23,7 +23,7 @@ void GetMainThreadName(const FunctionCallbackInfo<Value>& args) {
2323
}
2424

2525
void init(v8::Local<v8::Object> exports) {
26-
NODE_SET_METHOD(exports, "getMainThreadName", GetMainThreadName);
26+
NODE_SET_METHOD(exports, "getThreadName", GetThreadName);
2727
}
2828

29-
NODE_MODULE(NODE_GYP_MODULE_NAME, init)
29+
NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, init)

test/addons/uv-thread-name/test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
'use strict';
22
const common = require('../../common');
33
const assert = require('node:assert');
4+
const { parentPort, Worker, isMainThread } = require('node:worker_threads');
45
const bindingPath = require.resolve(`./build/${common.buildType}/binding`);
56
const binding = require(bindingPath);
67

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

Comments
 (0)