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 tests for cb scope #1262

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
21 changes: 19 additions & 2 deletions test/callbackscope.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "assert.h"
#include "napi.h"

using namespace Napi;

#if (NAPI_VERSION > 2)

namespace {

static void RunInCallbackScope(const CallbackInfo& info) {
Expand All @@ -12,11 +13,27 @@ static void RunInCallbackScope(const CallbackInfo& info) {
callback.Call({});
}

} // end anonymous namespace
static void RunInCallbackScopeFromExisting(const CallbackInfo& info) {
Function callback = info[0].As<Function>();
Env env = info.Env();

AsyncContext ctx(env, "existing_callback_scope_test");
napi_callback_scope scope;
napi_open_callback_scope(env, Object::New(env), ctx, &scope);

CallbackScope existingScope(env, scope);
assert(existingScope.Env() == env);

callback.Call({});
}

} // namespace

Object InitCallbackScope(Env env) {
Object exports = Object::New(env);
exports["runInCallbackScope"] = Function::New(env, RunInCallbackScope);
exports["runInPreExistingCbScope"] =
Function::New(env, RunInCallbackScopeFromExisting);
return exports;
}
#endif
9 changes: 6 additions & 3 deletions test/callbackscope.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function test (binding) {
let insideHook = false;
const hook = asyncHooks.createHook({
init (asyncId, type, triggerAsyncId, resource) {
if (id === undefined && type === 'callback_scope_test') {
if (id === undefined && (type === 'callback_scope_test' || type === 'existing_callback_scope_test')) {
id = asyncId;
}
},
Expand All @@ -39,8 +39,11 @@ function test (binding) {
return new Promise(resolve => {
binding.callbackscope.runInCallbackScope(function () {
assert(insideHook);
hook.disable();
resolve();
binding.callbackscope.runInPreExistingCbScope(function () {
assert(insideHook);
hook.disable();
resolve();
});
});
});
}