diff --git a/src/rcl_subscription_bindings.cpp b/src/rcl_subscription_bindings.cpp index b880245f..78bcd333 100644 --- a/src/rcl_subscription_bindings.cpp +++ b/src/rcl_subscription_bindings.cpp @@ -38,9 +38,9 @@ Napi::Value RclTake(const Napi::CallbackInfo& info) { rcl_ret_t ret = rcl_take(subscription, msg_taken, nullptr, nullptr); if (ret != RCL_RET_OK && ret != RCL_RET_SUBSCRIPTION_TAKE_FAILED) { + std::string error_string = rcl_get_error_string().str; rcl_reset_error(); - Napi::Error::New(env, rcl_get_error_string().str) - .ThrowAsJavaScriptException(); + Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); return Napi::Boolean::New(env, false); } @@ -99,7 +99,7 @@ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { for (int i = 0; i < argc; i++) { std::string arg = jsArgv.Get(i).As().Utf8Value(); int len = arg.length() + 1; - argv[i] = reinterpret_cast(malloc(len * sizeof(char*))); + argv[i] = reinterpret_cast(malloc(len * sizeof(char))); snprintf(argv[i], len, "%s", arg.c_str()); } } @@ -109,9 +109,9 @@ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { expression.c_str(), argc, (const char**)argv, &subscription_ops); if (ret != RCL_RET_OK) { + std::string error_string = rcl_get_error_string().str; rcl_reset_error(); - Napi::Error::New(env, rcl_get_error_string().str) - .ThrowAsJavaScriptException(); + Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); } if (argc) { @@ -120,6 +120,11 @@ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { } free(argv); } + + if (ret != RCL_RET_OK) { + free(subscription); + return env.Undefined(); + } } } @@ -127,11 +132,15 @@ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { GetMessageTypeSupport(package_name, message_sub_folder, message_name); if (ts) { - THROW_ERROR_IF_NOT_EQUAL( - RCL_RET_OK, - rcl_subscription_init(subscription, node, ts, topic.c_str(), - &subscription_ops), - rcl_get_error_string().str); + rcl_ret_t ret = rcl_subscription_init(subscription, node, ts, topic.c_str(), + &subscription_ops); + if (ret != RCL_RET_OK) { + std::string error_msg = rcl_get_error_string().str; + rcl_reset_error(); + Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); + free(subscription); + return env.Undefined(); + } auto js_obj = RclHandle::NewInstance( env, subscription, node_handle, [node, env](void* ptr) { @@ -139,14 +148,18 @@ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { reinterpret_cast(ptr); rcl_ret_t ret = rcl_subscription_fini(subscription, node); free(ptr); - THROW_ERROR_IF_NOT_EQUAL_NO_RETURN(RCL_RET_OK, ret, - rcl_get_error_string().str); + if (ret != RCL_RET_OK) { + std::string error_msg = rcl_get_error_string().str; + rcl_reset_error(); + Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); + } }); return js_obj; } else { Napi::Error::New(env, GetErrorMessageAndClear()) .ThrowAsJavaScriptException(); + free(subscription); return env.Undefined(); } } @@ -235,7 +248,7 @@ Napi::Value SetContentFilter(const Napi::CallbackInfo& info) { for (int i = 0; i < argc; i++) { std::string arg = jsArgv.Get(i).As().Utf8Value(); int len = arg.length() + 1; - argv[i] = reinterpret_cast(malloc(len * sizeof(char*))); + argv[i] = reinterpret_cast(malloc(len * sizeof(char))); snprintf(argv[i], len, "%s", arg.c_str()); } } @@ -245,15 +258,23 @@ Napi::Value SetContentFilter(const Napi::CallbackInfo& info) { rcl_subscription_content_filter_options_t options = rcl_get_zero_initialized_subscription_content_filter_options(); - THROW_ERROR_IF_NOT_EQUAL( - RCL_RET_OK, - rcl_subscription_content_filter_options_set( - subscription, expression.c_str(), argc, (const char**)argv, &options), - rcl_get_error_string().str); + rcl_ret_t ret = rcl_subscription_content_filter_options_set( + subscription, expression.c_str(), argc, (const char**)argv, &options); - THROW_ERROR_IF_NOT_EQUAL( - RCL_RET_OK, rcl_subscription_set_content_filter(subscription, &options), - rcl_get_error_string().str); + if (ret != RCL_RET_OK) { + if (argc) { + for (int i = 0; i < argc; i++) { + free(argv[i]); + } + free(argv); + } + std::string error_string = rcl_get_error_string().str; + rcl_reset_error(); + Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); + return env.Undefined(); + } + + ret = rcl_subscription_set_content_filter(subscription, &options); if (argc) { for (int i = 0; i < argc; i++) { @@ -262,6 +283,23 @@ Napi::Value SetContentFilter(const Napi::CallbackInfo& info) { free(argv); } + rcl_ret_t fini_ret = + rcl_subscription_content_filter_options_fini(subscription, &options); + + if (ret != RCL_RET_OK) { + std::string error_string = rcl_get_error_string().str; + rcl_reset_error(); + Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); + return env.Undefined(); + } + + if (fini_ret != RCL_RET_OK) { + std::string error_string = rcl_get_error_string().str; + rcl_reset_error(); + Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); + return env.Undefined(); + } + return Napi::Boolean::New(env, true); } @@ -277,15 +315,33 @@ Napi::Value ClearContentFilter(const Napi::CallbackInfo& info) { rcl_subscription_content_filter_options_t options = rcl_get_zero_initialized_subscription_content_filter_options(); - THROW_ERROR_IF_NOT_EQUAL( - RCL_RET_OK, - rcl_subscription_content_filter_options_init( - subscription, "", 0, (const char**)nullptr, &options), - rcl_get_error_string().str); + rcl_ret_t ret = rcl_subscription_content_filter_options_init( + subscription, "", 0, (const char**)nullptr, &options); + + if (ret != RCL_RET_OK) { + std::string error_string = rcl_get_error_string().str; + rcl_reset_error(); + Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); + return env.Undefined(); + } + + ret = rcl_subscription_set_content_filter(subscription, &options); + rcl_ret_t fini_ret = + rcl_subscription_content_filter_options_fini(subscription, &options); + + if (ret != RCL_RET_OK) { + std::string error_string = rcl_get_error_string().str; + rcl_reset_error(); + Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); + return env.Undefined(); + } - THROW_ERROR_IF_NOT_EQUAL( - RCL_RET_OK, rcl_subscription_set_content_filter(subscription, &options), - rcl_get_error_string().str); + if (fini_ret != RCL_RET_OK) { + std::string error_string = rcl_get_error_string().str; + rcl_reset_error(); + Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); + return env.Undefined(); + } return Napi::Boolean::New(env, true); } @@ -303,9 +359,9 @@ Napi::Value GetContentFilter(const Napi::CallbackInfo& info) { rcl_ret_t ret = rcl_subscription_get_content_filter(subscription, &options); if (ret != RCL_RET_OK) { - Napi::Error::New(env, rcl_get_error_string().str) - .ThrowAsJavaScriptException(); + std::string error_msg = rcl_get_error_string().str; rcl_reset_error(); + Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); return env.Undefined(); } @@ -331,9 +387,9 @@ Napi::Value GetContentFilter(const Napi::CallbackInfo& info) { rcl_ret_t fini_ret = rcl_subscription_content_filter_options_fini(subscription, &options); if (fini_ret != RCL_RET_OK) { - Napi::Error::New(env, rcl_get_error_string().str) - .ThrowAsJavaScriptException(); + std::string error_msg = rcl_get_error_string().str; rcl_reset_error(); + Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); return env.Undefined(); } @@ -347,9 +403,12 @@ Napi::Value GetPublisherCount(const Napi::CallbackInfo& info) { RclHandle::Unwrap(info[0].As())->ptr()); size_t count = 0; - THROW_ERROR_IF_NOT_EQUAL( - rcl_subscription_get_publisher_count(subscription, &count), RCL_RET_OK, - rcl_get_error_string().str); + rcl_ret_t ret = rcl_subscription_get_publisher_count(subscription, &count); + if (ret != RCL_RET_OK) { + std::string error_msg = rcl_get_error_string().str; + rcl_reset_error(); + Napi::Error::New(env, error_msg).ThrowAsJavaScriptException(); + } return Napi::Number::New(env, count); } diff --git a/test/test-clock-event.js b/test/test-clock-event.js index 6e72a091..10276c00 100644 --- a/test/test-clock-event.js +++ b/test/test-clock-event.js @@ -42,7 +42,7 @@ describe('ClockEvent', function () { await event.waitUntilSteady(clock, until.nanoseconds); const end = Date.now(); - assert(end - start >= 1000); + assert(end - start >= 950); }); it('should wait until system time', async function () { @@ -55,7 +55,7 @@ describe('ClockEvent', function () { await event.waitUntilSystem(clock, until.nanoseconds); const end = Date.now(); - assert(end - start >= 1000); + assert(end - start >= 950); }); it('should wait until ros time', async function () { @@ -72,7 +72,7 @@ describe('ClockEvent', function () { await event.waitUntilRos(clock, until.nanoseconds); const end = Date.now(); - assert(end - start >= 1000); + assert(end - start >= 950); }); it('should set and clear event', function () { diff --git a/test/test-type-description-service.js b/test/test-type-description-service.js index 45af596b..c0e9f731 100644 --- a/test/test-type-description-service.js +++ b/test/test-type-description-service.js @@ -85,51 +85,55 @@ describe('type description service test suite', function () { }); it('Test type description service configured by parameter', function (done) { - exec( - 'ros2 param list /test_type_description_service', - (error, stdout, stderr) => { - if (error || stderr) { - done( - new Error( - `Test type description service configured by parameter failed. Error: ${error}, Stderr: ${stderr}` - ) - ); - return; - } - if (stdout.includes('start_type_description_service')) { - done(); - } else { - done( - new Error("'start_type_description_service' not found in stdout.") - ); + setTimeout(() => { + exec( + 'ros2 param list /test_type_description_service', + (error, stdout, stderr) => { + if (error || stderr) { + done( + new Error( + `Test type description service configured by parameter failed. Error: ${error}, Stderr: ${stderr}` + ) + ); + return; + } + if (stdout.includes('start_type_description_service')) { + done(); + } else { + done( + new Error("'start_type_description_service' not found in stdout.") + ); + } } - } - ); + ); + }, 1000); }); it('Test start_type_description_service parameter value', function (done) { - exec( - 'ros2 param get /test_type_description_service start_type_description_service', - (error, stdout, stderr) => { - if (error || stderr) { - done( - new Error( - `Test type description service configured by parameter failed. Error: ${error}, Stderr: ${stderr}` - ) - ); - return; - } - if (stdout.includes('Boolean value is: True')) { - done(); - } else { - console.log(stdout); - done( - new Error( - "'start_type_description_service param value' not found in stdout." - ) - ); + setTimeout(() => { + exec( + 'ros2 param get /test_type_description_service start_type_description_service', + (error, stdout, stderr) => { + if (error || stderr) { + done( + new Error( + `Test type description service configured by parameter failed. Error: ${error}, Stderr: ${stderr}` + ) + ); + return; + } + if (stdout.includes('Boolean value is: True')) { + done(); + } else { + console.log(stdout); + done( + new Error( + "'start_type_description_service param value' not found in stdout." + ) + ); + } } - } - ); + ); + }, 1000); }); });