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: extend async addon test #14922

Closed
wants to merge 2 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
18 changes: 15 additions & 3 deletions test/addons/async-hello-world/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void DoAsync(uv_work_t* r) {
req->output = req->input * 2;
}

template <bool use_makecallback>
void AfterAsync(uv_work_t* r) {
async_req* req = reinterpret_cast<async_req*>(r->data);
v8::Isolate* isolate = req->isolate;
Expand All @@ -40,9 +41,18 @@ void AfterAsync(uv_work_t* r) {

v8::TryCatch try_catch(isolate);

v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global();
v8::Local<v8::Function> callback =
v8::Local<v8::Function>::New(isolate, req->callback);
callback->Call(isolate->GetCurrentContext()->Global(), 2, argv);

if (use_makecallback) {
v8::Local<v8::Value> ret =
node::MakeCallback(isolate, global, callback, 2, argv);
// This should be changed to an empty handle.
assert(!ret.IsEmpty());
} else {
callback->Call(global, 2, argv);
}

// cleanup
req->callback.Reset();
Expand All @@ -53,6 +63,7 @@ void AfterAsync(uv_work_t* r) {
}
}

template <bool use_makecallback>
void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();

Expand All @@ -69,11 +80,12 @@ void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
uv_queue_work(uv_default_loop(),
&req->req,
DoAsync,
(uv_after_work_cb)AfterAsync);
(uv_after_work_cb)AfterAsync<use_makecallback>);
}

void init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
NODE_SET_METHOD(module, "exports", Method);
NODE_SET_METHOD(exports, "runCall", Method<false>);
NODE_SET_METHOD(exports, "runMakeCallback", Method<true>);
}

NODE_MODULE(binding, init)
9 changes: 9 additions & 0 deletions test/addons/async-hello-world/test-makecallback-uncaught.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
const common = require('../../common');
const { runMakeCallback } = require(`./build/${common.buildType}/binding`);

process.on('uncaughtException', common.mustCall());

runMakeCallback(5, common.mustCall(() => {
throw new Error('foo');
}));
10 changes: 10 additions & 0 deletions test/addons/async-hello-world/test-makecallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const { runMakeCallback } = require(`./build/${common.buildType}/binding`);

runMakeCallback(5, common.mustCall(function(err, val) {
assert.strictEqual(err, null);
assert.strictEqual(val, 10);
process.nextTick(common.mustCall());
}));
4 changes: 2 additions & 2 deletions test/addons/async-hello-world/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const binding = require(`./build/${common.buildType}/binding`);
const { runCall } = require(`./build/${common.buildType}/binding`);

binding(5, common.mustCall(function(err, val) {
runCall(5, common.mustCall(function(err, val) {
assert.strictEqual(err, null);
assert.strictEqual(val, 10);
process.nextTick(common.mustCall());
Expand Down