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 89501e4e99a98ff490eb0b75ce6b906059e3e94d..adb1db80ecd01a4fe1f7dcd6a2b071e1c52ef43a 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 @@ -150,6 +150,15 @@ int FileWatcher::CloseNotifyFdLocked() int FileWatcher::StopNotify(shared_ptr arg) { + while (true) { + lock_guard lock(readMutex_); + if (reading_) { + HILOGI("Watcher is reading, retry ReadMutex"); + } else { + break; + } + }; + unique_lock lock(watchMutex_); if (notifyFd_ < 0) { HILOGE("Failed to stop notify notifyFd_:%{public}d", notifyFd_); @@ -168,6 +177,7 @@ int FileWatcher::StopNotify(shared_ptr arg) lock_guard lock(readMutex_); if (!(closed_ && reading_)) { oldWd = inotify_rm_watch(notifyFd_, arg->wd); + HILOGE("rm watch failed, err: %{public}d", errno); } else { HILOGE("rm watch fail"); } 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 index 5d5a50ed38a1ae3e7e1bb809d25ebcedfb01d92a..d72883f60587d5e184db266224a95e4ba598598b 100644 --- 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 @@ -16,6 +16,7 @@ #include "watcher_entity.h" #include +#include namespace OHOS::FileManagement::ModuleFileIO::Test { @@ -105,4 +106,48 @@ HWTEST_F(WatcherEntityTest, WatcherEntityTest_CloseNotifyFd_003, testing::ext::T GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_CloseNotifyFd_003"; } +/** + * @tc.name: WatcherEntityTest_StopNotify_001 + * @tc.desc: Test function of WatcherEntityTest::StopNotify interface for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherEntityTest, WatcherEntityTest_StopNotify_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherEntityTest-begin WatcherEntityTest_StopNotify_001"; + + auto &watcher = FileWatcher::GetInstance(); + watcher.notifyFd_ = -1; + watcher.reading_ = false; + EXPECT_EQ(watcher.StopNotify(nullptr), 5); + + GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_StopNotify_001"; +} + +/** + * @tc.name: WatcherEntityTest_StopNotify_002 + * @tc.desc: Test function of WatcherEntityTest::StopNotify interface for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(WatcherEntityTest, WatcherEntityTest_StopNotify_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WatcherEntityTest-begin WatcherEntityTest_StopNotify_002"; + + auto &watcher = FileWatcher::GetInstance(); + watcher.notifyFd_ = -1; + watcher.reading_ = true; + std::thread recordThread([this, &watcher]() { + watcher.StopNotify(nullptr); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(600)); + watcher.reading_ = false; + recordThread.join(); + EXPECT_EQ(watcher.StopNotify(nullptr), 5); + + GTEST_LOG_(INFO) << "WatcherEntityTest-end WatcherEntityTest_StopNotify_002"; +} + } // namespace OHOS::FileManagement::ModuleFileIO::Test \ No newline at end of file