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 for ThreadSafeFunction::New overloads #1251

Closed
wants to merge 3 commits into from
Closed
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
98 changes: 92 additions & 6 deletions test/threadsafe_function/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 @@ -8,7 +9,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 @@ -28,15 +29,13 @@ 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 @@ -59,11 +58,98 @@ TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap<TSFNWrap>(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
1 change: 1 addition & 0 deletions test/threadsafe_function/threadsafe_function_ctx.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ 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();
}