Skip to content

Commit

Permalink
test: add test covg for handle and escapehandle scopes
Browse files Browse the repository at this point in the history
PR-URL: #1263
Reviewed-By: Michael Dawson <midawson@redhat.com
  • Loading branch information
JckXia authored and mhdawson committed Jan 13, 2023
1 parent 0213134 commit 35d9d66
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/handlescope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ Value createScope(const CallbackInfo& info) {
return String::New(info.Env(), "scope");
}

Value createScopeFromExisting(const CallbackInfo& info) {
{
napi_handle_scope scope;
napi_open_handle_scope(info.Env(), &scope);
HandleScope scope_existing(info.Env(), scope);
String::New(scope_existing.Env(), "inner-existing-scope");
}
return String::New(info.Env(), "existing_scope");
}

Value escapeFromScope(const CallbackInfo& info) {
Value result;
{
Expand All @@ -22,6 +32,18 @@ Value escapeFromScope(const CallbackInfo& info) {
return result;
}

Value escapeFromExistingScope(const CallbackInfo& info) {
Value result;
{
napi_escapable_handle_scope scope;
napi_open_escapable_handle_scope(info.Env(), &scope);
EscapableHandleScope scope_existing(info.Env(), scope);
result = scope_existing.Escape(
String::New(scope_existing.Env(), "inner-existing-scope"));
}
return result;
}

#define LOOP_MAX 1000000
Value stressEscapeFromScope(const CallbackInfo& info) {
Value result;
Expand Down Expand Up @@ -52,7 +74,11 @@ Object InitHandleScope(Env env) {
Object exports = Object::New(env);

exports["createScope"] = Function::New(env, createScope);
exports["createScopeFromExisting"] =
Function::New(env, createScopeFromExisting);
exports["escapeFromScope"] = Function::New(env, escapeFromScope);
exports["escapeFromExistingScope"] =
Function::New(env, escapeFromExistingScope);
exports["stressEscapeFromScope"] = Function::New(env, stressEscapeFromScope);
exports["doubleEscapeFromScope"] = Function::New(env, doubleEscapeFromScope);

Expand Down
2 changes: 2 additions & 0 deletions test/handlescope.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module.exports = require('./common').runTest(test);

function test (binding) {
assert.strictEqual(binding.handlescope.createScope(), 'scope');
assert.strictEqual(binding.handlescope.createScopeFromExisting(), 'existing_scope');
assert.strictEqual(binding.handlescope.escapeFromScope(), 'inner-scope');
assert.strictEqual(binding.handlescope.escapeFromExistingScope(), 'inner-existing-scope');
assert.strictEqual(binding.handlescope.stressEscapeFromScope(), 'inner-scope999999');
assert.throws(() => binding.handlescope.doubleEscapeFromScope(),
Error,
Expand Down

0 comments on commit 35d9d66

Please sign in to comment.