Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add test coverage to TSFN::New() overloads #1201

Merged
merged 4 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}