Skip to content

Commit a70564c

Browse files
JckXiamhdawson
authored andcommitted
test: add cov for ThreadSafeFunction new overloads
PR-URL: #1251 Reviewed-By: Michael Dawson <[email protected]
1 parent 53f7cf1 commit a70564c

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

test/threadsafe_function/threadsafe_function_ctx.cc

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <assert.h>
12
#include "napi.h"
23

34
#if (NAPI_VERSION > 3)
@@ -8,7 +9,7 @@ namespace {
89

910
class TSFNWrap : public ObjectWrap<TSFNWrap> {
1011
public:
11-
static Object Init(Napi::Env env, Object exports);
12+
static Function Init(Napi::Env env);
1213
TSFNWrap(const CallbackInfo& info);
1314

1415
Napi::Value GetContext(const CallbackInfo& /*info*/) {
@@ -28,15 +29,13 @@ class TSFNWrap : public ObjectWrap<TSFNWrap> {
2829
std::unique_ptr<Promise::Deferred> _deferred;
2930
};
3031

31-
Object TSFNWrap::Init(Napi::Env env, Object exports) {
32+
Function TSFNWrap::Init(Napi::Env env) {
3233
Function func =
3334
DefineClass(env,
3435
"TSFNWrap",
3536
{InstanceMethod("getContext", &TSFNWrap::GetContext),
3637
InstanceMethod("release", &TSFNWrap::Release)});
37-
38-
exports.Set("TSFNWrap", func);
39-
return exports;
38+
return func;
4039
}
4140

4241
TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap<TSFNWrap>(info) {
@@ -59,11 +58,98 @@ TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap<TSFNWrap>(info) {
5958
delete ctx;
6059
});
6160
}
61+
struct SimpleTestContext {
62+
SimpleTestContext(int val) : _val(val) {}
63+
int _val = -1;
64+
};
65+
66+
void AssertGetContextFromVariousTSOverloads(const CallbackInfo& info) {
67+
Env env = info.Env();
68+
Function emptyFunc;
69+
70+
SimpleTestContext* ctx = new SimpleTestContext(42);
71+
ThreadSafeFunction fn =
72+
ThreadSafeFunction::New(env, emptyFunc, "testResource", 1, 1, ctx);
73+
74+
assert(fn.GetContext() == ctx);
75+
delete ctx;
76+
fn.Release();
77+
78+
fn = ThreadSafeFunction::New(env, emptyFunc, "testRes", 1, 1, [](Env) {});
79+
fn.Release();
80+
81+
ctx = new SimpleTestContext(42);
82+
fn = ThreadSafeFunction::New(env,
83+
emptyFunc,
84+
Object::New(env),
85+
"resStrObj",
86+
1,
87+
1,
88+
ctx,
89+
[](Env, SimpleTestContext*) {});
90+
assert(fn.GetContext() == ctx);
91+
delete ctx;
92+
fn.Release();
93+
94+
fn = ThreadSafeFunction::New(
95+
env, emptyFunc, Object::New(env), "resStrObj", 1, 1);
96+
fn.Release();
97+
98+
ctx = new SimpleTestContext(42);
99+
fn = ThreadSafeFunction::New(
100+
env, emptyFunc, Object::New(env), "resStrObj", 1, 1, ctx);
101+
assert(fn.GetContext() == ctx);
102+
delete ctx;
103+
fn.Release();
104+
105+
using FinalizerDataType = int;
106+
FinalizerDataType* finalizerData = new int(42);
107+
fn = ThreadSafeFunction::New(
108+
env,
109+
emptyFunc,
110+
Object::New(env),
111+
"resObject",
112+
1,
113+
1,
114+
[](Env, FinalizerDataType* data) {
115+
assert(*data == 42);
116+
delete data;
117+
},
118+
finalizerData);
119+
fn.Release();
120+
121+
ctx = new SimpleTestContext(42);
122+
FinalizerDataType* finalizerDataB = new int(42);
123+
124+
fn = ThreadSafeFunction::New(
125+
env,
126+
emptyFunc,
127+
Object::New(env),
128+
"resObject",
129+
1,
130+
1,
131+
ctx,
132+
[](Env, FinalizerDataType* _data, SimpleTestContext* _ctx) {
133+
assert(*_data == 42);
134+
assert(_ctx->_val == 42);
135+
delete _data;
136+
delete _ctx;
137+
},
138+
finalizerDataB);
139+
assert(fn.GetContext() == ctx);
140+
fn.Release();
141+
}
62142

63143
} // namespace
64144

65145
Object InitThreadSafeFunctionCtx(Env env) {
66-
return TSFNWrap::Init(env, Object::New(env));
146+
Object exports = Object::New(env);
147+
Function tsfnWrap = TSFNWrap::Init(env);
148+
exports.Set("TSFNWrap", tsfnWrap);
149+
exports.Set("AssertFnReturnCorrectCxt",
150+
Function::New(env, AssertGetContextFromVariousTSOverloads));
151+
152+
return exports;
67153
}
68154

69155
#endif

test/threadsafe_function/threadsafe_function_ctx.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ async function test (binding) {
99
const tsfn = new binding.threadsafe_function_ctx.TSFNWrap(ctx);
1010
assert(tsfn.getContext() === ctx);
1111
await tsfn.release();
12+
binding.threadsafe_function_ctx.AssertFnReturnCorrectCxt();
1213
}

0 commit comments

Comments
 (0)