Skip to content

Commit

Permalink
test: extend async addon test
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
addaleax authored and MylesBorins committed Sep 20, 2017
1 parent b10bc31 commit ad1d745
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
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

0 comments on commit ad1d745

Please sign in to comment.