diff options
author | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2018-11-17 12:34:54 -0800 |
---|---|---|
committer | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2018-12-04 13:58:17 -0800 |
commit | 938e11882b96e19b443477571455088baaa054d8 (patch) | |
tree | eb828a60957a2881995ba9a83f44a32a18fbff16 /test/node-api/test_uv_loop | |
parent | 83ee137c4565112177f22f2c735b266b22262220 (diff) | |
download | android-node-v8-938e11882b96e19b443477571455088baaa054d8.tar.gz android-node-v8-938e11882b96e19b443477571455088baaa054d8.tar.bz2 android-node-v8-938e11882b96e19b443477571455088baaa054d8.zip |
test: partition N-API tests
Partition test/addons-napi into test/js-native-api and test/node-api to
isolate the Node.js-agnostic portion of the N-API tests from the
Node.js-specific portion.
PR-URL: https://github.com/nodejs/node/pull/24557
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Diffstat (limited to 'test/node-api/test_uv_loop')
-rw-r--r-- | test/node-api/test_uv_loop/binding.gyp | 8 | ||||
-rw-r--r-- | test/node-api/test_uv_loop/test.js | 5 | ||||
-rw-r--r-- | test/node-api/test_uv_loop/test_uv_loop.cc | 87 |
3 files changed, 100 insertions, 0 deletions
diff --git a/test/node-api/test_uv_loop/binding.gyp b/test/node-api/test_uv_loop/binding.gyp new file mode 100644 index 0000000000..81fcfdc592 --- /dev/null +++ b/test/node-api/test_uv_loop/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "test_uv_loop", + "sources": [ "test_uv_loop.cc" ] + } + ] +} diff --git a/test/node-api/test_uv_loop/test.js b/test/node-api/test_uv_loop/test.js new file mode 100644 index 0000000000..4efc3c6fcd --- /dev/null +++ b/test/node-api/test_uv_loop/test.js @@ -0,0 +1,5 @@ +'use strict'; +const common = require('../../common'); +const { SetImmediate } = require(`./build/${common.buildType}/test_uv_loop`); + +SetImmediate(common.mustCall()); diff --git a/test/node-api/test_uv_loop/test_uv_loop.cc b/test/node-api/test_uv_loop/test_uv_loop.cc new file mode 100644 index 0000000000..c5eaac5273 --- /dev/null +++ b/test/node-api/test_uv_loop/test_uv_loop.cc @@ -0,0 +1,87 @@ +#include <node_api.h> +#include <uv.h> +#include <utility> +#include <memory> +#include <assert.h> +#include "../../js-native-api/common.h" + +template <typename T> +void* SetImmediate(napi_env env, T&& cb) { + T* ptr = new T(std::move(cb)); + uv_loop_t* loop = nullptr; + uv_check_t* check = new uv_check_t; + check->data = ptr; + NAPI_ASSERT(env, + napi_get_uv_event_loop(env, &loop) == napi_ok, + "can get event loop"); + uv_check_init(loop, check); + uv_check_start(check, [](uv_check_t* check) { + std::unique_ptr<T> ptr {static_cast<T*>(check->data)}; + T cb = std::move(*ptr); + uv_close(reinterpret_cast<uv_handle_t*>(check), [](uv_handle_t* handle) { + delete reinterpret_cast<uv_check_t*>(handle); + }); + + assert(cb() != nullptr); + }); + // Idle handle is needed only to stop the event loop from blocking in poll. + uv_idle_t* idle = new uv_idle_t; + uv_idle_init(loop, idle); + uv_idle_start(idle, [](uv_idle_t* idle) { + uv_close(reinterpret_cast<uv_handle_t*>(idle), [](uv_handle_t* handle) { + delete reinterpret_cast<uv_check_t*>(handle); + }); + }); + + return nullptr; +} + +static char dummy; + +napi_value SetImmediateBinding(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value argv[1]; + napi_value _this; + void* data; + NAPI_CALL(env, + napi_get_cb_info(env, info, &argc, argv, &_this, &data)); + NAPI_ASSERT(env, argc >= 1, "Not enough arguments, expected 1."); + + napi_valuetype t; + NAPI_CALL(env, napi_typeof(env, argv[0], &t)); + NAPI_ASSERT(env, t == napi_function, + "Wrong first argument, function expected."); + + napi_ref cbref; + NAPI_CALL(env, + napi_create_reference(env, argv[0], 1, &cbref)); + + SetImmediate(env, [=]() -> char* { + napi_value undefined; + napi_value callback; + napi_handle_scope scope; + NAPI_CALL(env, napi_open_handle_scope(env, &scope)); + NAPI_CALL(env, napi_get_undefined(env, &undefined)); + NAPI_CALL(env, napi_get_reference_value(env, cbref, &callback)); + NAPI_CALL(env, napi_delete_reference(env, cbref)); + NAPI_CALL(env, + napi_call_function(env, undefined, callback, 0, nullptr, nullptr)); + NAPI_CALL(env, napi_close_handle_scope(env, scope)); + return &dummy; + }); + + return nullptr; +} + +napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor properties[] = { + DECLARE_NAPI_PROPERTY("SetImmediate", SetImmediateBinding) + }; + + NAPI_CALL(env, napi_define_properties( + env, exports, sizeof(properties) / sizeof(*properties), properties)); + + return exports; +} + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |