From 19e08f404e9dfb15dfd61f68ca62e6cbcae6ef36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=8F=E5=AD=90=E6=81=92?= Date: Mon, 7 Jul 2025 19:43:15 +0800 Subject: [PATCH 1/2] watcher bug fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 魏子恒 --- interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp | 2 +- interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp index 54d9851bf..89501e4e9 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp @@ -117,7 +117,7 @@ int FileWatcher::NotifyToWatchNewEvents(const string &fileName, const int &wd, c int FileWatcher::CloseNotifyFd() { int closeRet = ERRNO_NOERR; - if (watcherInfoSet_.size() == 0) { + if (watcherInfoSet_.size() == 0 && run_) { run_ = false; closeRet = close(notifyFd_); if (closeRet != 0) { diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index d2dc25d99..c64f1c75c 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -15,6 +15,7 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_ENTITY_H #define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_ENTITY_H +#include #include #include #include @@ -71,7 +72,7 @@ private: private: static std::mutex watchMutex_; std::mutex readMutex_; - bool run_ = false; + std::atomic run_ = false; bool reading_ = false; bool closed_ = false; int32_t notifyFd_ = -1; -- Gitee From b961c150bfeeea127671e5788c3c1dd85bed749f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Mon, 7 Jul 2025 17:35:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwatcher=E5=B7=B2=E7=9F=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interfaces/test/unittest/BUILD.gn | 1 + interfaces/test/unittest/js/BUILD.gn | 73 ++++++++++++------- .../class_watcher/watcher_entity_test.cpp | 67 +++++++++++++++++ 3 files changed, 116 insertions(+), 25 deletions(-) create mode 100644 interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 5aeda1645..20af46a08 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -22,6 +22,7 @@ group("file_api_unittest") { "js:ani_file_fs_test", "js:ani_file_hash_test", "js:ani_file_securitylabel_test", + "js:napi_file_fs_test", "remote_uri:remote_uri_test", "task_signal:task_signal_test", ] diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 31b84da70..e1b5e51c6 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -33,9 +33,9 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/properties/access_core_mock_test.cpp", "mod_fs/properties/copy_file_core_mock_test.cpp", "mod_fs/properties/mkdir_core_mock_test.cpp", - "mod_fs/properties/move_core_mock_test.cpp", "mod_fs/properties/mock/system_mock.cpp", "mod_fs/properties/mock/uv_fs_mock.cpp", + "mod_fs/properties/move_core_mock_test.cpp", "mod_fs/properties/open_core_mock_test.cpp", "mod_fs/properties/read_core_mock_test.cpp", "mod_fs/properties/stat_core_mock_test.cpp", @@ -45,12 +45,12 @@ ohos_unittest("ani_file_fs_mock_test") { ] deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_fs", "${file_api_path}/interfaces/kits/native:remote_uri_native", "${file_api_path}/interfaces/kits/native:task_signal_native", "${file_api_path}/interfaces/kits/rust:rust_file", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", - "${file_api_path}/interfaces/kits/js:ani_file_fs", ] external_deps = [ @@ -64,9 +64,7 @@ ohos_unittest("ani_file_fs_mock_test") { "libuv:uv", ] - defines = [ - "private=public", - ] + defines = [ "private=public" ] use_exceptions = true } @@ -108,12 +106,12 @@ ohos_unittest("ani_file_fs_test") { ] deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_fs", "${file_api_path}/interfaces/kits/native:remote_uri_native", "${file_api_path}/interfaces/kits/native:task_signal_native", "${file_api_path}/interfaces/kits/rust:rust_file", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", - "${file_api_path}/interfaces/kits/js:ani_file_fs", ] external_deps = [ @@ -126,9 +124,7 @@ ohos_unittest("ani_file_fs_test") { "libuv:uv", ] - defines = [ - "private=public", - ] + defines = [ "private=public" ] use_exceptions = true } @@ -138,9 +134,7 @@ ohos_unittest("ani_file_hash_test") { resource_config_file = "../resource/ohos_test.xml" - sources = [ - "mod_hash/hash_core_test.cpp", - ] + sources = [ "mod_hash/hash_core_test.cpp" ] include_dirs = [ "mock/libuv", @@ -148,17 +142,17 @@ ohos_unittest("ani_file_hash_test") { ] deps = [ - "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", - "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${file_api_path}/interfaces/kits/js:ani_file_hash", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] external_deps = [ "c_utils:utils", - "hilog:libhilog", - "libuv:uv", "googletest:gmock_main", "googletest:gtest_main", + "hilog:libhilog", + "libuv:uv", ] use_exceptions = true @@ -169,26 +163,55 @@ ohos_unittest("ani_file_securitylabel_test") { resource_config_file = "../resource/ohos_test.xml" - sources = [ - "mod_securitylabel/securitylabel_core_test.cpp", + sources = [ "mod_securitylabel/securitylabel_core_test.cpp" ] + + include_dirs = [ "${file_api_path}/interfaces/kits/js/src/mod_securitylabel" ] + + deps = [ + "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] - include_dirs = [ - "${file_api_path}/interfaces/kits/js/src/mod_securitylabel", + external_deps = [ + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + ] + + use_exceptions = true +} + +ohos_unittest("napi_file_fs_test") { + branch_protector_ret = "pac_ret" + testonly = true + + module_out_path = "file_api/file_api" + + include_dirs = + [ "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher" ] + + sources = [ + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp", + "mod_fs/class_watcher/watcher_entity_test.cpp", ] deps = [ + "${file_api_path}/interfaces/kits/js:fs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", - "${utils_path}/filemgmt_libfs:filemgmt_libfs", - "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", + "${utils_path}/filemgmt_libn:filemgmt_libn", ] external_deps = [ "c_utils:utils", - "hilog:libhilog", - "googletest:gmock_main", "googletest:gtest_main", + "hilog:libhilog", + "libuv:uv", + "napi:ace_napi", ] + defines = [ "private=public" ] + use_exceptions = true -} \ No newline at end of file +} diff --git a/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp b/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp new file mode 100644 index 000000000..37613fb63 --- /dev/null +++ b/interfaces/test/unittest/js/mod_fs/class_watcher/watcher_entity_test.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "watcher_entity.h" + +#include + +namespace OHOS::FileManagement::ModuleFileIO::Test { + +class WatcherEntityTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void WatcherEntityTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void WatcherEntityTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void WatcherEntityTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void WatcherEntityTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: WatcherEntityTest_CloseNotifyFd_001 + * @tc.desc: Test function of WatcherEntityTest::CloseNotifyFd interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherEntityTest, WatcherEntityTest_CloseNotifyFd_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherEntityTest-begin WatcherEntityTest_CloseNotifyFd_001"; + + auto ret = FileWatcher::GetInstance().CloseNotifyFd(); + EXPECT_NE(ret, 0); + + GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_CloseNotifyFd_001"; +} + +} // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file -- Gitee