Skip to content

Commit

Permalink
test: add regression test for 5691
Browse files Browse the repository at this point in the history
With `CallbackScope`, this has become possible to do properly.

Fixes: #5691
PR-URL: #14697
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and jasnell committed Sep 20, 2017
1 parent 774e42b commit 50b6203
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/addons/callback-scope/binding.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "node.h"
#include "v8.h"
#include "uv.h"

#include <assert.h>
#include <vector>
Expand Down Expand Up @@ -30,8 +31,39 @@ void RunInCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(ret.ToLocalChecked());
}

static v8::Persistent<v8::Promise::Resolver> persistent;

static void Callback(uv_work_t* req, int ignored) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
node::CallbackScope callback_scope(isolate, v8::Object::New(isolate), {0, 0});

v8::Local<v8::Promise::Resolver> local =
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
local->Resolve(v8::Undefined(isolate));
delete req;
}

static void TestResolveAsync(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();

if (persistent.IsEmpty()) {
persistent.Reset(isolate, v8::Promise::Resolver::New(isolate));

uv_work_t* req = new uv_work_t;

uv_queue_work(uv_default_loop(), req, [](uv_work_t*) {}, Callback);
}

v8::Local<v8::Promise::Resolver> local =
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);

args.GetReturnValue().Set(local->GetPromise());
}

void Initialize(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "runInCallbackScope", RunInCallbackScope);
NODE_SET_METHOD(exports, "testResolveAsync", TestResolveAsync);
}

} // namespace
Expand Down
13 changes: 13 additions & 0 deletions test/addons/callback-scope/test-resolve-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const common = require('../../common');
const assert = require('assert');
const { testResolveAsync } = require(`./build/${common.buildType}/binding`);

let called = false;
testResolveAsync().then(common.mustCall(() => {
called = true;
}));

setTimeout(common.mustCall(() => { assert(called); }),
common.platformTimeout(20));

0 comments on commit 50b6203

Please sign in to comment.