Skip to content

Commit e9db2ad

Browse files
authored
test: Add test coverage to TSFN::New() overloads (#1201)
* test: Testing to ensure that the overloads return the correct contextes * test: Remove extraneous comments * test: Add test for new overloads
1 parent c849ad3 commit e9db2ad

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc

Lines changed: 57 additions & 5 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)
@@ -11,7 +12,7 @@ namespace {
1112

1213
class TSFNWrap : public ObjectWrap<TSFNWrap> {
1314
public:
14-
static Object Init(Napi::Env env, Object exports);
15+
static Function Init(Napi::Env env);
1516
TSFNWrap(const CallbackInfo& info);
1617

1718
Napi::Value GetContext(const CallbackInfo& /*info*/) {
@@ -31,15 +32,14 @@ class TSFNWrap : public ObjectWrap<TSFNWrap> {
3132
std::unique_ptr<Promise::Deferred> _deferred;
3233
};
3334

34-
Object TSFNWrap::Init(Napi::Env env, Object exports) {
35+
Function TSFNWrap::Init(Napi::Env env) {
3536
Function func =
3637
DefineClass(env,
3738
"TSFNWrap",
3839
{InstanceMethod("getContext", &TSFNWrap::GetContext),
3940
InstanceMethod("release", &TSFNWrap::Release)});
4041

41-
exports.Set("TSFNWrap", func);
42-
return exports;
42+
return func;
4343
}
4444

4545
TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap<TSFNWrap>(info) {
@@ -61,8 +61,60 @@ TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap<TSFNWrap>(info) {
6161

6262
} // namespace
6363

64+
struct SimpleTestContext {
65+
SimpleTestContext(int val) : _val(val) {}
66+
int _val = -1;
67+
};
68+
69+
// A simple test to check that the context has been set successfully
70+
void AssertGetContextFromTSFNNoFinalizerIsCorrect(const CallbackInfo& info) {
71+
// Test the overload where we provide a resource name but no finalizer
72+
using TSFN = TypedThreadSafeFunction<SimpleTestContext>;
73+
SimpleTestContext* ctx = new SimpleTestContext(42);
74+
TSFN tsfn = TSFN::New(info.Env(), "testRes", 1, 1, ctx);
75+
76+
assert(tsfn.GetContext() == ctx);
77+
delete ctx;
78+
tsfn.Release();
79+
80+
// Test the other overload where we provide a async resource object, res name
81+
// but no finalizer
82+
ctx = new SimpleTestContext(52);
83+
tsfn = TSFN::New(
84+
info.Env(), Object::New(info.Env()), "testResourceObject", 1, 1, ctx);
85+
86+
assert(tsfn.GetContext() == ctx);
87+
delete ctx;
88+
tsfn.Release();
89+
90+
ctx = new SimpleTestContext(52);
91+
tsfn = TSFN::New(info.Env(),
92+
"resStrings",
93+
1,
94+
1,
95+
ctx,
96+
[](Napi::Env, void*, SimpleTestContext*) {});
97+
98+
assert(tsfn.GetContext() == ctx);
99+
delete ctx;
100+
tsfn.Release();
101+
102+
ctx = new SimpleTestContext(52);
103+
Function emptyFunc;
104+
tsfn = TSFN::New(info.Env(), emptyFunc, "resString", 1, 1, ctx);
105+
assert(tsfn.GetContext() == ctx);
106+
delete ctx;
107+
tsfn.Release();
108+
}
109+
64110
Object InitTypedThreadSafeFunctionCtx(Env env) {
65-
return TSFNWrap::Init(env, Object::New(env));
111+
Object exports = Object::New(env);
112+
Function tsfnWrap = TSFNWrap::Init(env);
113+
114+
exports.Set("TSFNWrap", tsfnWrap);
115+
exports.Set("AssertTSFNReturnCorrectCxt",
116+
Function::New(env, AssertGetContextFromTSFNNoFinalizerIsCorrect));
117+
return exports;
66118
}
67119

68120
#endif

test/typed_threadsafe_function/typed_threadsafe_function_ctx.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ async function test (binding) {
99
const tsfn = new binding.typed_threadsafe_function_ctx.TSFNWrap(ctx);
1010
assert(tsfn.getContext() === ctx);
1111
await tsfn.release();
12+
13+
binding.typed_threadsafe_function_ctx.AssertTSFNReturnCorrectCxt();
1214
}

0 commit comments

Comments
 (0)