From 359ad0b2a38dee95410e71f04f03a379676bcced Mon Sep 17 00:00:00 2001 From: JckXia Date: Thu, 15 Dec 2022 20:07:34 -0500 Subject: [PATCH 1/3] test: Add coverage for ThreadSafeFunction new overloads --- .../threadsafe_function_ctx.cc | 99 +++++++++++++++++-- .../threadsafe_function_ctx.js | 2 + 2 files changed, 95 insertions(+), 6 deletions(-) diff --git a/test/threadsafe_function/threadsafe_function_ctx.cc b/test/threadsafe_function/threadsafe_function_ctx.cc index 470de12fd..120d49a4e 100644 --- a/test/threadsafe_function/threadsafe_function_ctx.cc +++ b/test/threadsafe_function/threadsafe_function_ctx.cc @@ -1,3 +1,5 @@ +#include +#include #include "napi.h" #if (NAPI_VERSION > 3) @@ -8,7 +10,7 @@ namespace { class TSFNWrap : public ObjectWrap { 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*/) { @@ -28,15 +30,13 @@ class TSFNWrap : public ObjectWrap { std::unique_ptr _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(info) { @@ -59,11 +59,98 @@ TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap(info) { delete ctx; }); } +struct SimpleTestContext { + SimpleTestContext(int val) : _val(val) {} + int _val = -1; +}; + +void AssertGetContextFromVariousTSOverloads(const CallbackInfo& info) { + Env env = info.Env(); + Function emptyFunc; + + SimpleTestContext* ctx = new SimpleTestContext(42); + ThreadSafeFunction fn = + ThreadSafeFunction::New(env, emptyFunc, "testResource", 1, 1, ctx); + + assert(fn.GetContext() == ctx); + delete ctx; + fn.Release(); + + fn = ThreadSafeFunction::New(env, emptyFunc, "testRes", 1, 1, [](Env) {}); + fn.Release(); + + ctx = new SimpleTestContext(42); + fn = ThreadSafeFunction::New(env, + emptyFunc, + Object::New(env), + "resStrObj", + 1, + 1, + ctx, + [](Env, SimpleTestContext*) {}); + assert(fn.GetContext() == ctx); + delete ctx; + fn.Release(); + + fn = ThreadSafeFunction::New( + env, emptyFunc, Object::New(env), "resStrObj", 1, 1); + fn.Release(); + + ctx = new SimpleTestContext(42); + fn = ThreadSafeFunction::New( + env, emptyFunc, Object::New(env), "resStrObj", 1, 1, ctx); + assert(fn.GetContext() == ctx); + delete ctx; + fn.Release(); + + using FinalizerDataType = int; + FinalizerDataType* finalizerData = new int(42); + fn = ThreadSafeFunction::New( + env, + emptyFunc, + Object::New(env), + "resObject", + 1, + 1, + [](Env, FinalizerDataType* data) { + assert(*data == 42); + delete data; + }, + finalizerData); + fn.Release(); + + ctx = new SimpleTestContext(42); + FinalizerDataType* finalizerDataB = new int(42); + + fn = ThreadSafeFunction::New( + env, + emptyFunc, + Object::New(env), + "resObject", + 1, + 1, + ctx, + [](Env, FinalizerDataType* _data, SimpleTestContext* _ctx) { + assert(*_data == 42); + assert(_ctx->_val == 42); + delete _data; + delete _ctx; + }, + finalizerDataB); + assert(fn.GetContext() == ctx); + fn.Release(); +} } // namespace Object InitThreadSafeFunctionCtx(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("AssertFnReturnCorrectCxt", + Function::New(env, AssertGetContextFromVariousTSOverloads)); + + return exports; } #endif diff --git a/test/threadsafe_function/threadsafe_function_ctx.js b/test/threadsafe_function/threadsafe_function_ctx.js index 258e220b1..4f311d86c 100644 --- a/test/threadsafe_function/threadsafe_function_ctx.js +++ b/test/threadsafe_function/threadsafe_function_ctx.js @@ -9,4 +9,6 @@ async function test (binding) { const tsfn = new binding.threadsafe_function_ctx.TSFNWrap(ctx); assert(tsfn.getContext() === ctx); await tsfn.release(); + binding.threadsafe_function_ctx.AssertFnReturnCorrectCxt(); + // console.log(binding.threadsafe_function_ctx); } From cc2a6f94eddfc1381e066bb92ef848126e661092 Mon Sep 17 00:00:00 2001 From: JckXia Date: Thu, 15 Dec 2022 20:08:48 -0500 Subject: [PATCH 2/3] test: Remove comments --- test/threadsafe_function/threadsafe_function_ctx.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/threadsafe_function/threadsafe_function_ctx.js b/test/threadsafe_function/threadsafe_function_ctx.js index 4f311d86c..4ba707b79 100644 --- a/test/threadsafe_function/threadsafe_function_ctx.js +++ b/test/threadsafe_function/threadsafe_function_ctx.js @@ -10,5 +10,4 @@ async function test (binding) { assert(tsfn.getContext() === ctx); await tsfn.release(); binding.threadsafe_function_ctx.AssertFnReturnCorrectCxt(); - // console.log(binding.threadsafe_function_ctx); } From cb4e3855f213738ec1ae514eb8c17e63b12af61f Mon Sep 17 00:00:00 2001 From: Jack <32422811+JckXia@users.noreply.github.com> Date: Sun, 15 Jan 2023 22:44:21 -0500 Subject: [PATCH 3/3] test: Remove unused iostream import --- test/threadsafe_function/threadsafe_function_ctx.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/test/threadsafe_function/threadsafe_function_ctx.cc b/test/threadsafe_function/threadsafe_function_ctx.cc index 120d49a4e..abe92fdf0 100644 --- a/test/threadsafe_function/threadsafe_function_ctx.cc +++ b/test/threadsafe_function/threadsafe_function_ctx.cc @@ -1,5 +1,4 @@ #include -#include #include "napi.h" #if (NAPI_VERSION > 3)