Skip to content

Commit

Permalink
test: Add test coverage to TSFN::New() overloads (#1201)
Browse files Browse the repository at this point in the history
* test: Testing to ensure that the overloads return the correct contextes

* test: Remove extraneous comments

* test: Add test for new overloads
  • Loading branch information
JckXia authored Nov 15, 2022
1 parent c849ad3 commit e9db2ad
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
62 changes: 57 additions & 5 deletions test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include "napi.h"

#if (NAPI_VERSION > 3)
Expand All @@ -11,7 +12,7 @@ namespace {

class TSFNWrap : public ObjectWrap<TSFNWrap> {
public:
static Object Init(Napi::Env env, Object exports);
static Function Init(Napi::Env env);
TSFNWrap(const CallbackInfo& info);

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

Object TSFNWrap::Init(Napi::Env env, Object exports) {
Function TSFNWrap::Init(Napi::Env env) {
Function func =
DefineClass(env,
"TSFNWrap",
{InstanceMethod("getContext", &TSFNWrap::GetContext),
InstanceMethod("release", &TSFNWrap::Release)});

exports.Set("TSFNWrap", func);
return exports;
return func;
}

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

} // namespace

struct SimpleTestContext {
SimpleTestContext(int val) : _val(val) {}
int _val = -1;
};

// A simple test to check that the context has been set successfully
void AssertGetContextFromTSFNNoFinalizerIsCorrect(const CallbackInfo& info) {
// Test the overload where we provide a resource name but no finalizer
using TSFN = TypedThreadSafeFunction<SimpleTestContext>;
SimpleTestContext* ctx = new SimpleTestContext(42);
TSFN tsfn = TSFN::New(info.Env(), "testRes", 1, 1, ctx);

assert(tsfn.GetContext() == ctx);
delete ctx;
tsfn.Release();

// Test the other overload where we provide a async resource object, res name
// but no finalizer
ctx = new SimpleTestContext(52);
tsfn = TSFN::New(
info.Env(), Object::New(info.Env()), "testResourceObject", 1, 1, ctx);

assert(tsfn.GetContext() == ctx);
delete ctx;
tsfn.Release();

ctx = new SimpleTestContext(52);
tsfn = TSFN::New(info.Env(),
"resStrings",
1,
1,
ctx,
[](Napi::Env, void*, SimpleTestContext*) {});

assert(tsfn.GetContext() == ctx);
delete ctx;
tsfn.Release();

ctx = new SimpleTestContext(52);
Function emptyFunc;
tsfn = TSFN::New(info.Env(), emptyFunc, "resString", 1, 1, ctx);
assert(tsfn.GetContext() == ctx);
delete ctx;
tsfn.Release();
}

Object InitTypedThreadSafeFunctionCtx(Env env) {
return TSFNWrap::Init(env, Object::New(env));
Object exports = Object::New(env);
Function tsfnWrap = TSFNWrap::Init(env);

exports.Set("TSFNWrap", tsfnWrap);
exports.Set("AssertTSFNReturnCorrectCxt",
Function::New(env, AssertGetContextFromTSFNNoFinalizerIsCorrect));
return exports;
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ async function test (binding) {
const tsfn = new binding.typed_threadsafe_function_ctx.TSFNWrap(ctx);
assert(tsfn.getContext() === ctx);
await tsfn.release();

binding.typed_threadsafe_function_ctx.AssertTSFNReturnCorrectCxt();
}

0 comments on commit e9db2ad

Please sign in to comment.