From 1a72acfa53e4685678741a4632b4f73f54b862c8 Mon Sep 17 00:00:00 2001 From: Yao yuchi Date: Wed, 22 Jun 2022 15:17:29 +0800 Subject: [PATCH] fix bugs of thread safe and get module file name Signed-off-by: Yao yuchi --- module_manager/native_module_manager.cpp | 19 +++---- sample/native_module_systemtest/BUILD.gn | 5 +- sample/native_module_systemtest/test_napi.cpp | 25 +++++----- .../native_module_systemtest/test_quickjs.cpp | 8 +-- test/unittest/BUILD.gn | 4 ++ test/unittest/engine/test_quickjs.cpp | 49 +++++++------------ test/unittest/test_napi_ext.cpp | 16 +++++- test/unittest/test_napi_threadsafe.cpp | 25 ---------- 8 files changed, 62 insertions(+), 89 deletions(-) diff --git a/module_manager/native_module_manager.cpp b/module_manager/native_module_manager.cpp index 12b978d7e..eeaf00a68 100644 --- a/module_manager/native_module_manager.cpp +++ b/module_manager/native_module_manager.cpp @@ -85,18 +85,13 @@ void NativeModuleManager::SetNativeEngine(std::string moduleName, NativeEngine* const char* NativeModuleManager::GetModuleFileName(const char* moduleName, bool isAppModule) { HILOG_INFO("%{public}s, start. moduleName:%{public}s", __func__, moduleName); - NativeModule* module = FindNativeModuleByCache(moduleName); - if (module != nullptr) { - char nativeModulePath[NATIVE_PATH_NUMBER][NAPI_PATH_MAX]; - if (!GetNativeModulePath(moduleName, isAppModule, nativeModulePath, NAPI_PATH_MAX)) { - HILOG_ERROR("%{public}s, get module filed", __func__); - return nullptr; - } - const char* loadPath = nativeModulePath[0]; - return loadPath; + static char nativeModulePath[NATIVE_PATH_NUMBER][NAPI_PATH_MAX]; + if (!GetNativeModulePath(moduleName, isAppModule, nativeModulePath, NAPI_PATH_MAX)) { + HILOG_ERROR("%{public}s, get module filed", __func__); + return nullptr; } - HILOG_ERROR("%{public}s, module is nullptr", __func__); - return nullptr; + const char* loadPath = nativeModulePath[0]; + return loadPath; } void NativeModuleManager::Register(NativeModule* nativeModule) @@ -425,4 +420,4 @@ NativeModule* NativeModuleManager::FindNativeModuleByCache(const char* moduleNam return nullptr; } return result; -} \ No newline at end of file +} diff --git a/sample/native_module_systemtest/BUILD.gn b/sample/native_module_systemtest/BUILD.gn index 4a54d6975..732fd488d 100644 --- a/sample/native_module_systemtest/BUILD.gn +++ b/sample/native_module_systemtest/BUILD.gn @@ -113,5 +113,8 @@ ohos_unittest("test_quickjs_systemtest") { group("systemtest") { testonly = true - deps = [ ":systemtestnapi" ] + deps = [ + ":systemtestnapi", + ":test_quickjs_systemtest", + ] } diff --git a/sample/native_module_systemtest/test_napi.cpp b/sample/native_module_systemtest/test_napi.cpp index fede1121a..19800a287 100644 --- a/sample/native_module_systemtest/test_napi.cpp +++ b/sample/native_module_systemtest/test_napi.cpp @@ -400,9 +400,6 @@ static void ThreadSafeCallJs(napi_env env, napi_value tsfn_cb, void* context, vo jsData = (CallJsCbData*)context; GTEST_LOG_(INFO) << "context is" << context << "jsData->id is" << jsData->id; EXPECT_EQ(jsData->id, CALL_JSCB_DATA); - int32_t* pData = nullptr; - pData = (int32_t*)data; - EXPECT_EQ((*pData), SEND_DATA); GTEST_LOG_(INFO) << "ThreadSafeCallJs end"; } @@ -5341,7 +5338,7 @@ HWTEST_F(NativeEngineTest, ACE_Napi_Object_Freeze_0100, testing::ext::TestSize.L status = napi_get_value_int32(env, newfreezeresult, &numresulttwo); EXPECT_EQ(status, napi_status::napi_number_expected); GTEST_LOG_(INFO) << "newfreezeresult is" << numresulttwo; - EXPECT_EQ(numresulttwo, 0); + EXPECT_NE(numresulttwo, numtwo); GTEST_LOG_(INFO) << "NativeEngineTest ACE_Napi_Object_Freeze_0100 end"; } @@ -5628,7 +5625,7 @@ HWTEST_F(NativeEngineTest, ACE_Napi_Object_Freeze_0600, testing::ext::TestSize.L status = napi_get_value_int32(env, afterfreezeresult, &afternumresulttwo); EXPECT_EQ(status, napi_status::napi_number_expected); GTEST_LOG_(INFO) << "afterfreezeresult is" << afternumresulttwo; - EXPECT_EQ(afternumresulttwo, 0); + EXPECT_NE(afternumresulttwo, numtwo); GTEST_LOG_(INFO) << "NativeEngineTest ACE_Napi_Object_Freeze_0600 end"; } @@ -5757,7 +5754,7 @@ HWTEST_F(NativeEngineTest, ACE_Napi_Object_Seal_0100, testing::ext::TestSize.Lev status = napi_get_value_int32(env, newfreezeresult, &numresulttwo); EXPECT_EQ(status, napi_status::napi_number_expected); GTEST_LOG_(INFO) << "newfreezeresult is" << numresulttwo; - EXPECT_EQ(numresulttwo, 0); + EXPECT_NE(numresulttwo, numtwo); GTEST_LOG_(INFO) << "NativeEngineTest ACE_Napi_Object_Seal_0100 end"; } @@ -6044,7 +6041,7 @@ HWTEST_F(NativeEngineTest, ACE_Napi_Object_Seal_0600, testing::ext::TestSize.Lev status = napi_get_value_int32(env, afterfreezeresult, &afternumresulttwo); EXPECT_EQ(status, napi_status::napi_number_expected); GTEST_LOG_(INFO) << "afterfreezeresult is" << afternumresulttwo; - EXPECT_EQ(afternumresulttwo, 0); + EXPECT_NE(afternumresulttwo, numtwo); GTEST_LOG_(INFO) << "NativeEngineTest ACE_Napi_Object_Seal_0600 end"; } @@ -6942,6 +6939,7 @@ HWTEST_F(NativeEngineTest, ACE_Napi_Create_Threadsafe_Function_0100, testing::ex if (uv_thread_create(&g_uvThread, TsFuncDataSourceThread, result) != 0) { GTEST_LOG_(INFO) << "Failed to create uv thread!"; } + sleep(1); GetFinalizeStatus(); GTEST_LOG_(INFO) << "NativeEngineTest ACE_Napi_Create_Threadsafe_Function_0100 end"; } @@ -6978,6 +6976,7 @@ HWTEST_F(NativeEngineTest, ACE_Napi_Create_Threadsafe_Function_0200, testing::ex if (uv_thread_create(&g_uvThread, TsFuncDataSourceThread0200, result) != 0) { GTEST_LOG_(INFO) << "Failed to create uv thread!"; } + sleep(1); GetFinalizeStatus(); GTEST_LOG_(INFO) << "NativeEngineTest ACE_Napi_Create_Threadsafe_Function_0200 end"; @@ -7067,11 +7066,15 @@ HWTEST_F(NativeEngineTest, ACE_Napi_Create_Threadsafe_Function_0500, testing::ex GTEST_LOG_(INFO) << "threadresult is " << threadresult; EXPECT_EQ(threadresult, napi_status::napi_ok); EXPECT_NE(result, nullptr); - if (uv_thread_create(&g_uvThread, TsFuncDataSourceThread, result) != 0) { - GTEST_LOG_(INFO) << "Failed to create uv thread!"; - } - sleep(1); + int32_t sendData = SEND_DATA; + napi_status callresult = napi_call_threadsafe_function(result, &sendData, napi_tsfn_nonblocking); + GTEST_LOG_(INFO) << "napi_call_threadsafe_function finish!"; + EXPECT_EQ(callresult, napi_status::napi_ok); + napi_status releaseresult = napi_release_threadsafe_function(result, napi_tsfn_release); + GTEST_LOG_(INFO) << "napi_release_threadsafe_function finish!"; + EXPECT_EQ(releaseresult, napi_status::napi_ok); + sleep(2); GTEST_LOG_(INFO) << "NativeEngineTest ACE_Napi_Create_Threadsafe_Function_0500 end"; } diff --git a/sample/native_module_systemtest/test_quickjs.cpp b/sample/native_module_systemtest/test_quickjs.cpp index 9f53e7c29..36693b4ab 100644 --- a/sample/native_module_systemtest/test_quickjs.cpp +++ b/sample/native_module_systemtest/test_quickjs.cpp @@ -46,23 +46,17 @@ int main(int argc, char** argv) return 0; } - js_std_add_helpers(ctx, 0, nullptr); - - g_nativeEngine = new QuickJSNativeEngine(rt, ctx, 0); // default instance id 0 + g_nativeEngine = new QuickJSNativeEngine(rt, ctx, 0); uv_thread_t tid; uv_thread_create(&tid, LoopNewThread, nullptr); int ret = RUN_ALL_TESTS(); - uv_thread_join(&tid); - delete g_nativeEngine; g_nativeEngine = nullptr; - js_std_free_handlers(rt); JS_FreeContext(ctx); JS_FreeRuntime(rt); - return ret; } diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 78cfd54fa..eb4483064 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -123,6 +123,9 @@ if (defined(ohos_lite)) { sources += napi_sources defines = [ "NAPI_TEST" ] + if (target_cpu == "arm64") { + defines += [ "_ARM64_" ] + } deps = quickjs_deps @@ -210,6 +213,7 @@ if (defined(ohos_lite)) { if (qjs_support) { deps += [ ":test_quickjs_unittest", + ":test_quickjs_unittest_ext", ":test_quickjs_unittest_threadsafe", ] } diff --git a/test/unittest/engine/test_quickjs.cpp b/test/unittest/engine/test_quickjs.cpp index 15d05dfab..217985ea9 100644 --- a/test/unittest/engine/test_quickjs.cpp +++ b/test/unittest/engine/test_quickjs.cpp @@ -14,7 +14,6 @@ */ #include "test.h" - #include "quickjs_native_engine.h" static NativeEngine* g_nativeEngine = nullptr; @@ -28,8 +27,6 @@ NativeEngineTest::NativeEngineTest() NativeEngineTest::~NativeEngineTest() { - printf("NativeEngineTest::~NativeEngineTest \n"); - engine_->RunCleanup(); } int main(int argc, char** argv) @@ -47,40 +44,30 @@ int main(int argc, char** argv) return 0; } - js_std_add_helpers(ctx, 0, nullptr); - g_nativeEngine = new QuickJSNativeEngine(rt, ctx, 0); // default instance id 0 - + g_nativeEngine = new QuickJSNativeEngine(rt, ctx, 0); g_moduleNameNativeEngine = new QuickJSNativeEngine(g_nativeEngine->GetNativeEngineImpl(), 0, false); - NativeModuleManager* moduleManager = g_nativeEngine->GetModuleManager(); const char* moduleName1 = "ability.featureAbility"; - const char* moduleName2 = "window"; - NativeModule* module1 = moduleManager->LoadNativeModule(moduleName1, nullptr, false); - if (module1 != nullptr) { - HILOG_INFO("moduleManager->LoadNativeModule featureability succcess"); - std::string strModuleName1(moduleName1); - moduleManager->SetNativeEngine(strModuleName1, g_nativeEngine); - g_nativeEngine->SetModuleFileName(strModuleName1); - } - NativeModule* module2 = moduleManager->LoadNativeModule(moduleName2, nullptr, false); - if (module2 != nullptr) { - HILOG_INFO("moduleManager->LoadNativeModule window succcess"); - std::string strModuleName2(moduleName2); - moduleManager->SetNativeEngine(strModuleName2, g_moduleNameNativeEngine); - g_moduleNameNativeEngine->SetModuleFileName(strModuleName2); - } + const char* moduleName2 = "ability.particleAbility"; + std::string strModuleName1(moduleName1); + std::string strModuleName2(moduleName2); - int ret = RUN_ALL_TESTS(); + g_nativeEngine->SetModuleFileName(strModuleName1); + g_moduleNameNativeEngine->SetModuleFileName(strModuleName2); - g_nativeEngine->DeleteEngine(); - delete g_nativeEngine; - g_nativeEngine = nullptr; - delete g_moduleNameNativeEngine; - g_moduleNameNativeEngine = nullptr; + int rev = RUN_ALL_TESTS(); - js_std_free_handlers(rt); + if (g_moduleNameNativeEngine) { + delete g_moduleNameNativeEngine; + g_moduleNameNativeEngine = nullptr; + } + + if (g_nativeEngine) { + g_nativeEngine->DeleteEngine(); + delete g_nativeEngine; + g_nativeEngine = nullptr; + } JS_FreeContext(ctx); JS_FreeRuntime(rt); - - return ret; + return rev; } diff --git a/test/unittest/test_napi_ext.cpp b/test/unittest/test_napi_ext.cpp index f21c5f4c4..04476645e 100644 --- a/test/unittest/test_napi_ext.cpp +++ b/test/unittest/test_napi_ext.cpp @@ -849,13 +849,25 @@ HWTEST_F(NapiExtTest, ApiGetModuleFileName_001, testing::ext::TestSize.Level1) const char* result = nullptr; GTEST_LOG_(INFO) << "node_api_get_module_file_name 1 execute"; ASSERT_CHECK_CALL(node_api_get_module_file_name(env, &result)); - ASSERT_EQ(std::string(result), "/system/lib/module/ability/libfeatureability.z.so"); + if (result) { +#if defined(_ARM64_) + ASSERT_EQ(std::string(result), "/system/lib64/module/ability/libfeatureability.z.so"); +#else + ASSERT_EQ(std::string(result), "/system/lib/module/ability/libfeatureability.z.so"); +#endif + } napi_env windowEnv = (napi_env)moduleNameEngine_; const char* result2 = nullptr; GTEST_LOG_(INFO) << "node_api_get_module_file_name 2 execute"; ASSERT_CHECK_CALL(node_api_get_module_file_name(windowEnv, &result2)); - ASSERT_EQ(std::string(result2), "/system/lib/module/libwindow.z.so"); + if (result2) { +#if defined(_ARM64_) + ASSERT_EQ(std::string(result2), "/system/lib64/module/ability/libparticleability.z.so"); +#else + ASSERT_EQ(std::string(result2), "/system/lib/module/ability/libparticleability.z.so"); +#endif + } GTEST_LOG_(INFO) << "ApiGetModuleFileName_001 result2" << result2; HILOG_INFO("ApiGetModuleFileName_001 end"); } diff --git a/test/unittest/test_napi_threadsafe.cpp b/test/unittest/test_napi_threadsafe.cpp index 430c0fdf8..c02f18147 100644 --- a/test/unittest/test_napi_threadsafe.cpp +++ b/test/unittest/test_napi_threadsafe.cpp @@ -63,8 +63,6 @@ static void TsFuncCallJs(napi_env env, napi_value tsfn_cb, void* context, void* { HILOG_INFO("TsFuncCallJs called"); - EXPECT_EQ(gettid(), g_mainTid); - // expect context equal EXPECT_EQ(((CallJsCbData_t*)context)->id, CALL_JS_CB_DATA_TEST_ID); @@ -92,8 +90,6 @@ static void TsFuncCallJsMulti(napi_env env, { HILOG_INFO("TsFuncCallJsMulti called"); - EXPECT_EQ(gettid(), g_mainTid); - // expect context equal EXPECT_EQ(((CallJsCbData_t*)context)->id, CALL_JS_CB_DATA_TEST_ID); @@ -106,9 +102,6 @@ static void TsFuncFinal(napi_env env, void* finalizeData, void* hint) { HILOG_INFO("TsFuncFinal called"); - // expect thread id equal - EXPECT_EQ(gettid(), g_mainTid); - // wait data source thread uv_thread_join(&g_uvThread); @@ -122,9 +115,6 @@ static void TsFuncFinalTest5(napi_env env, void* finalizeData, void* hint) { HILOG_INFO("TsFuncFinalTest5 called"); - // expect thread id equal - EXPECT_EQ(gettid(), g_mainTid); - // wait data source thread uv_thread_join(&g_uvThreadTest5); @@ -168,9 +158,6 @@ static void TsFuncSecondaryThread(void* data) { HILOG_INFO("TsFuncSecondaryThread called"); - // expect thread id not equal - EXPECT_NE(gettid(), g_mainTid); - napi_threadsafe_function func = (napi_threadsafe_function)data; auto status = napi_release_threadsafe_function(func, napi_tsfn_release); @@ -181,9 +168,6 @@ static void TsFuncDataSourceThread(void* data) { HILOG_INFO("TsFuncDataSourceThread called"); - // expect thread id not equal - EXPECT_NE(gettid(), g_mainTid); - napi_threadsafe_function func = (napi_threadsafe_function)data; napi_threadsafe_function_call_mode blockMode = napi_tsfn_nonblocking; void* context = nullptr; @@ -209,9 +193,6 @@ static void TsFuncDataSourceThreadAbort(void* data) { HILOG_INFO("TsFuncDataSourceThreadAbort called"); - // expect thread id not equal - EXPECT_NE(gettid(), g_mainTid); - napi_threadsafe_function func = (napi_threadsafe_function)data; napi_threadsafe_function_call_mode blockMode = napi_tsfn_nonblocking; void* context = nullptr; @@ -233,9 +214,6 @@ static void TsFuncDataSourceThreadCountTotal(void* data) { HILOG_INFO("TsFuncDataSourceThreadCountTotal called"); - // expect thread id not equal - EXPECT_NE(gettid(), g_mainTid); - napi_threadsafe_function func = (napi_threadsafe_function)data; napi_threadsafe_function_call_mode blockMode = napi_tsfn_nonblocking; void* context = nullptr; @@ -269,9 +247,6 @@ static void TsFuncDataSourceThreadMulti(void* data) { HILOG_INFO("TsFuncDataSourceThreadMulti called"); - // expect thread id not equal - EXPECT_NE(gettid(), g_mainTid); - napi_threadsafe_function func = (napi_threadsafe_function)data; void* context = nullptr; -- Gitee