From e9db2adef21e4c6386c558ffb3517e26dadf5961 Mon Sep 17 00:00:00 2001 From: Jack <32422811+JckXia@users.noreply.github.com> Date: Tue, 15 Nov 2022 12:28:18 -0500 Subject: [PATCH] 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 --- .../typed_threadsafe_function_ctx.cc | 62 +++++++++++++++++-- .../typed_threadsafe_function_ctx.js | 2 + 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc b/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc index ee70bb352..7cf2209dc 100644 --- a/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc +++ b/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc @@ -1,3 +1,4 @@ +#include #include "napi.h" #if (NAPI_VERSION > 3) @@ -11,7 +12,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*/) { @@ -31,15 +32,14 @@ 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) { @@ -61,8 +61,60 @@ TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap(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* 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 diff --git a/test/typed_threadsafe_function/typed_threadsafe_function_ctx.js b/test/typed_threadsafe_function/typed_threadsafe_function_ctx.js index b8c842bc6..ddbddccb9 100644 --- a/test/typed_threadsafe_function/typed_threadsafe_function_ctx.js +++ b/test/typed_threadsafe_function/typed_threadsafe_function_ctx.js @@ -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(); }