Skip to content

Commit 9c7f27b

Browse files
addaleaxMylesBorins
authored andcommitted
test: extend async addon test
Test more current behaviour, based on discussions in #14697. PR-URL: #14922 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c861462 commit 9c7f27b

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

test/addons/async-hello-world/binding.cc

+15-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void DoAsync(uv_work_t* r) {
2828
req->output = req->input * 2;
2929
}
3030

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

4142
v8::TryCatch try_catch(isolate);
4243

44+
v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global();
4345
v8::Local<v8::Function> callback =
4446
v8::Local<v8::Function>::New(isolate, req->callback);
45-
callback->Call(isolate->GetCurrentContext()->Global(), 2, argv);
47+
48+
if (use_makecallback) {
49+
v8::Local<v8::Value> ret =
50+
node::MakeCallback(isolate, global, callback, 2, argv);
51+
// This should be changed to an empty handle.
52+
assert(!ret.IsEmpty());
53+
} else {
54+
callback->Call(global, 2, argv);
55+
}
4656

4757
// cleanup
4858
req->callback.Reset();
@@ -53,6 +63,7 @@ void AfterAsync(uv_work_t* r) {
5363
}
5464
}
5565

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

@@ -69,11 +80,12 @@ void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
6980
uv_queue_work(uv_default_loop(),
7081
&req->req,
7182
DoAsync,
72-
(uv_after_work_cb)AfterAsync);
83+
(uv_after_work_cb)AfterAsync<use_makecallback>);
7384
}
7485

7586
void init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
76-
NODE_SET_METHOD(module, "exports", Method);
87+
NODE_SET_METHOD(exports, "runCall", Method<false>);
88+
NODE_SET_METHOD(exports, "runMakeCallback", Method<true>);
7789
}
7890

7991
NODE_MODULE(binding, init)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const { runMakeCallback } = require(`./build/${common.buildType}/binding`);
4+
5+
process.on('uncaughtException', common.mustCall());
6+
7+
runMakeCallback(5, common.mustCall(() => {
8+
throw new Error('foo');
9+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
const { runMakeCallback } = require(`./build/${common.buildType}/binding`);
5+
6+
runMakeCallback(5, common.mustCall(function(err, val) {
7+
assert.strictEqual(err, null);
8+
assert.strictEqual(val, 10);
9+
process.nextTick(common.mustCall());
10+
}));

test/addons/async-hello-world/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22
const common = require('../../common');
33
const assert = require('assert');
4-
const binding = require(`./build/${common.buildType}/binding`);
4+
const { runCall } = require(`./build/${common.buildType}/binding`);
55

6-
binding(5, common.mustCall(function(err, val) {
6+
runCall(5, common.mustCall(function(err, val) {
77
assert.strictEqual(err, null);
88
assert.strictEqual(val, 10);
99
process.nextTick(common.mustCall());

0 commit comments

Comments
 (0)