Skip to content

Commit 8fcdb4d

Browse files
committed
test: add barebones new tsfn test for use in docs
1 parent 89181da commit 8fcdb4d

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

test/binding.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Object InitThreadSafeFunctionUnref(Env env);
5050
Object InitThreadSafeFunction(Env env);
5151
Object InitThreadSafeFunctionExCall(Env env);
5252
Object InitThreadSafeFunctionExContext(Env env);
53+
Object InitThreadSafeFunctionExExample(Env env);
5354
Object InitThreadSafeFunctionExSimple(Env env);
5455
Object InitThreadSafeFunctionExThreadSafe(Env env);
5556
#endif
@@ -111,6 +112,7 @@ Object Init(Env env, Object exports) {
111112
exports.Set("threadsafe_function", InitThreadSafeFunction(env));
112113
exports.Set("threadsafe_function_ex_call", InitThreadSafeFunctionExCall(env));
113114
exports.Set("threadsafe_function_ex_context", InitThreadSafeFunctionExContext(env));
115+
exports.Set("threadsafe_function_ex_example", InitThreadSafeFunctionExExample(env));
114116
exports.Set("threadsafe_function_ex_simple", InitThreadSafeFunctionExSimple(env));
115117
exports.Set("threadsafe_function_ex_threadsafe", InitThreadSafeFunctionExThreadSafe(env));
116118
#endif

test/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
'run_script.cc',
3838
'threadsafe_function_ex/call.cc',
3939
'threadsafe_function_ex/context.cc',
40+
'threadsafe_function_ex/example.cc',
4041
'threadsafe_function_ex/simple.cc',
4142
'threadsafe_function_ex/threadsafe.cc',
4243
'threadsafe_function/threadsafe_function_ctx.cc',

test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ let testModules = [
4444
'run_script',
4545
'threadsafe_function_ex/call',
4646
'threadsafe_function_ex/context',
47+
'threadsafe_function_ex/example',
4748
'threadsafe_function_ex/simple',
4849
'threadsafe_function_ex/threadsafe',
4950
'threadsafe_function/threadsafe_function_ctx',
@@ -80,6 +81,7 @@ if (napiVersion < 4) {
8081
testModules.splice(testModules.indexOf('threadsafe_function/threadsafe_function'), 1);
8182
testModules.splice(testModules.indexOf('threadsafe_function_ex/call'), 1);
8283
testModules.splice(testModules.indexOf('threadsafe_function_ex/context'), 1);
84+
testModules.splice(testModules.indexOf('threadsafe_function_ex/example'), 1);
8385
testModules.splice(testModules.indexOf('threadsafe_function_ex/simple'), 1);
8486
testModules.splice(testModules.indexOf('threadsafe_function_ex/threadsafe'), 1);
8587
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* This test is programmatically represents the example shown in
3+
* `doc/threadsafe_function_ex.md`
4+
*/
5+
6+
#include "napi.h"
7+
8+
#if (NAPI_VERSION > 3)
9+
10+
using namespace Napi;
11+
12+
namespace {
13+
14+
// Context of our TSFN.
15+
struct Context {};
16+
17+
// Data passed (as pointer) to ThreadSafeFunctionEx::[Non]BlockingCall
18+
using DataType = int;
19+
20+
// Callback function
21+
static void Callback(Napi::Env env, Napi::Function jsCallback, Context *context,
22+
DataType *data) {
23+
// Check that the threadsafe function has not been finalized. Node calls this
24+
// callback for items remaining on the queue once finalization has completed.
25+
if (!(env == nullptr || jsCallback == nullptr)) {
26+
}
27+
if (data != nullptr) {
28+
delete data;
29+
}
30+
}
31+
32+
// Full type of our ThreadSafeFunctionEx
33+
using TSFN = ThreadSafeFunctionEx<Context, DataType, Callback>;
34+
35+
// A JS-accessible wrap that holds a TSFN.
36+
class TSFNWrap : public ObjectWrap<TSFNWrap> {
37+
38+
public:
39+
static Object Init(Napi::Env env, Object exports);
40+
TSFNWrap(const CallbackInfo &info);
41+
42+
private:
43+
Napi::Value Start(const CallbackInfo &info);
44+
Napi::Value Release(const CallbackInfo &info);
45+
};
46+
47+
/**
48+
* @brief Initialize `TSFNWrap` on the environment.
49+
*
50+
* @param env
51+
* @param exports
52+
* @return Object
53+
*/
54+
Object TSFNWrap::Init(Napi::Env env, Object exports) {
55+
Function func = DefineClass(env, "TSFNWrap",
56+
{InstanceMethod("start", &TSFNWrap::Start),
57+
InstanceMethod("release", &TSFNWrap::Release)});
58+
59+
exports.Set("TSFNWrap", func);
60+
return exports;
61+
}
62+
63+
/**
64+
* @brief Construct a new TSFNWrap::TSFNWrap object
65+
*
66+
* @param info
67+
*/
68+
TSFNWrap::TSFNWrap(const CallbackInfo &info) : ObjectWrap<TSFNWrap>(info) {}
69+
} // namespace
70+
71+
/**
72+
* @brief Instance method `TSFNWrap#start`
73+
*
74+
* @param info
75+
* @return undefined
76+
*/
77+
Napi::Value TSFNWrap::Start(const CallbackInfo &info) {
78+
Napi::Env env = info.Env();
79+
return env.Undefined();
80+
};
81+
82+
/**
83+
* @brief Instance method `TSFNWrap#release`
84+
*
85+
* @param info
86+
* @return undefined
87+
*/
88+
Napi::Value TSFNWrap::Release(const CallbackInfo &info) {
89+
Napi::Env env = info.Env();
90+
return env.Undefined();
91+
};
92+
93+
/**
94+
* @brief Module initialization function
95+
*
96+
* @param env
97+
* @return Object
98+
*/
99+
Object InitThreadSafeFunctionExExample(Env env) {
100+
return TSFNWrap::Init(env, Object::New(env));
101+
}
102+
103+
#endif
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
/**
4+
* This test is programmatically represents the example shown in
5+
* `doc/threadsafe_function_ex.md`
6+
*/
7+
8+
const assert = require('assert');
9+
const buildType = process.config.target_defaults.default_configuration;
10+
11+
module.exports = Promise.all([
12+
test(require(`../build/${buildType}/binding.node`)),
13+
test(require(`../build/${buildType}/binding_noexcept.node`))
14+
]);
15+
16+
async function test(binding) {
17+
const tsfn = new binding.threadsafe_function_ex_example.TSFNWrap();
18+
await tsfn.start();
19+
await tsfn.release();
20+
}

0 commit comments

Comments
 (0)