Skip to content

Commit a01c3c8

Browse files
committed
test: refactor the the 'empty' tsfnex test
It should actually check for empty jsCallback
1 parent 7467a3f commit a01c3c8

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

test/threadsafe_function_ex/test/basic.cc

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include <array>
2-
#include "napi.h"
31
#include "../util/util.h"
2+
#include "napi.h"
3+
#include <array>
44

55
#if (NAPI_VERSION > 3)
66

@@ -60,12 +60,12 @@ class TSFNWrap : public base {
6060

6161
Napi::Value Call(const CallbackInfo &info) {
6262
Napi::Env env = info.Env();
63-
DataType *data = new DataType{Napi::Reference<Napi::Value>(Persistent(info[0])),
64-
Promise::Deferred::New(env)};
63+
DataType *data =
64+
new DataType{Napi::Reference<Napi::Value>(Persistent(info[0])),
65+
Promise::Deferred::New(env)};
6566
_tsfn.NonBlockingCall(data);
6667
return data->deferred.Promise();
6768
};
68-
6969
};
7070

7171
} // namespace call
@@ -145,18 +145,17 @@ using ContextType = std::nullptr_t;
145145
// Data passed (as pointer) to [Non]BlockingCall
146146
struct DataType {
147147
Promise::Deferred deferred;
148-
bool reject;
149148
};
150149

151150
// CallJs callback function
152-
static void CallJs(Napi::Env env, Function jsCallback, ContextType * /*context*/,
153-
DataType *data) {
151+
static void CallJs(Napi::Env env, Function jsCallback,
152+
ContextType * /*context*/, DataType *data) {
154153
if (env != nullptr) {
155154
if (data != nullptr) {
156-
if (data->reject) {
157-
data->deferred.Reject(env.Undefined());
155+
if (jsCallback.IsEmpty()) {
156+
data->deferred.Resolve(Boolean::New(env, true));
158157
} else {
159-
data->deferred.Resolve(env.Undefined());
158+
data->deferred.Reject(String::New(env, "jsCallback is not empty"));
160159
}
161160
}
162161
}
@@ -193,14 +192,7 @@ class TSFNWrap : public base {
193192
}
194193

195194
Napi::Value Call(const CallbackInfo &info) {
196-
if (info.Length() == 0 || !info[0].IsBoolean()) {
197-
NAPI_THROW(
198-
Napi::TypeError::New(info.Env(), "Expected argument 0 to be boolean"),
199-
Value());
200-
}
201-
202-
auto *data =
203-
new DataType{Promise::Deferred::New(info.Env()), info[0].ToBoolean()};
195+
auto data = new DataType{Promise::Deferred::New(info.Env())};
204196
_tsfn.NonBlockingCall(data);
205197
return data->deferred.Promise();
206198
};
@@ -323,8 +315,8 @@ namespace simple {
323315

324316
using ContextType = std::nullptr_t;
325317

326-
// Full type of our ThreadSafeFunctionEx. We don't specify the `ContextType` here
327-
// (even though the _default_ for the type argument is `std::nullptr_t`) to
318+
// Full type of our ThreadSafeFunctionEx. We don't specify the `ContextType`
319+
// here (even though the _default_ for the type argument is `std::nullptr_t`) to
328320
// demonstrate construction with no type arguments.
329321
using TSFN = ThreadSafeFunctionEx<>;
330322

test/threadsafe_function_ex/test/basic.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,13 @@ class BasicTest extends TestRunner {
5454
* that handles all of its JavaScript processing on the callJs instead of the
5555
* callback.
5656
* - Creates a threadsafe function with no JavaScript context or callback.
57-
* - Makes two calls, waiting for each, and expecting the first to resolve
58-
* and the second to reject.
57+
* - Makes one call, waiting for completion. The internal `CallJs` resolves the call if jsCallback is empty, otherwise rejects.
5958
*/
6059
async empty({ TSFNWrap }) {
6160
debugger;
6261
if (typeof TSFNWrap === 'function') {
6362
const tsfn = new TSFNWrap();
64-
await tsfn.call(false /* reject */);
65-
let caught = false;
66-
try {
67-
await tsfn.call(true /* reject */);
68-
} catch (ex) {
69-
caught = true;
70-
}
71-
72-
assert.ok(caught, 'The promise rejection was not caught');
63+
await tsfn.call();
7364
return await tsfn.release();
7465
}
7566
return true;

0 commit comments

Comments
 (0)