From fbee8594580ce8542ca2bc6b4b01268fd546c412 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Mon, 18 Aug 2025 19:31:27 +0800 Subject: [PATCH 01/12] fix: Signed-off-by: weishaoxiong --- .../service/cloud/cloud_service_impl.cpp | 13 +--- .../service/cloud/sync_manager.cpp | 62 +++++++++++++++++++ .../service/cloud/sync_manager.h | 21 ++++++- 3 files changed, 85 insertions(+), 11 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 3179ab113..18f294fff 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -796,19 +796,11 @@ int32_t CloudServiceImpl::OnReady(const std::string &device) ZLOGW("current user is not logged in."); return E_NOT_LOGIN; } - std::vector users; - Account::GetInstance()->QueryForegroundUsers(users); - if (users.empty()) { - return SUCCESS; - } + if (!NetworkDelegate::GetInstance()->IsNetworkAvailable()) { return NETWORK_ERROR; } - for (auto user : users) { - DoKvCloudSync(user, "", MODE_ONLINE); - Execute(GenTask(0, user, CloudSyncScene::NETWORK_RECOVERY, - { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE, WORK_DO_CLOUD_SYNC, WORK_SUB })); - } + syncManager_.GetNetworkRecoveryManager().OnNetworkConnected(); return SUCCESS; } @@ -825,6 +817,7 @@ int32_t CloudServiceImpl::Offline(const std::string &device) } auto it = users.begin(); syncManager_.StopCloudSync(*it); + syncManager_.GetNetworkRecoveryManager().OnNetworkDisconnected(); return SUCCESS; } diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 39a046674..5c6c4b1a6 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -47,6 +47,7 @@ using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; using Defer = EventCenter::Defer; std::atomic SyncManager::genId_ = 0; constexpr int32_t SYSTEM_USER_ID = 0; +constexpr int32_t TIMEOUT_TIME = 20; // hours static constexpr const char *FT_GET_STORE = "GET_STORE"; static constexpr const char *FT_CALLBACK = "CALLBACK"; SyncManager::SyncInfo::SyncInfo( @@ -364,6 +365,9 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t times, bool retry, RefCount UpdateStartSyncInfo(cloudSyncInfos); auto code = IsValid(info, cloud); if (code != E_OK) { + if (code == E_NETWORK_ERROR) { + networkRecoveryManager_.RecordSyncApps(info.bundleName_); + } BatchUpdateFinishState(cloudSyncInfos, code); BatchReport(info.user_, traceIds, SyncStage::END, code, "!IsValid"); return; @@ -1084,4 +1088,62 @@ int32_t SyncManager::ConvertValidGeneralCode(int32_t code) { return (code >= E_OK && code < E_BUSY) ? code : E_ERROR; } + +void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() +{ + ZLOGI("network disconnected."); + currentEvent_ = std::make_unique(); + currentEvent_->disconnectTime = std::chrono::system_clock::now(); +} + +void SyncManager::NetworkRecoveryManager::OnNetworkConnected() +{ + ZLOGI("network connected."); + if (!currentEvent_) { + ZLOGE("network connected, but no sync event."); + return; + } + auto now = std::chrono::system_clock::now(); + auto duration = now - currentEvent_->disconnectTime; + auto hours = std::chrono::duration_cast(duration).count(); + bool timeout = (hours > TIMEOUT_TIME); + + std::vector users; + if (!Account::GetInstance()->QueryForegroundUsers(users) || users.empty()) { + ZLOGE("no foreground user, skip sync."); + return; + } + auto& syncManager = syncManager_; + for (auto user : users) { + CloudInfo cloud; + cloud.user = user; + if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), cloud, true)) { + continue; + } + ZLOGI("network connected, sync hours:%{public}ld", hours); + const auto& apps = timeout ? ExtractBundleNames(cloud.apps) : currentEvent_->syncApps; + for (const auto& app : apps) { + ZLOGI("sync app:%{public}s, user:%{public}d", app.c_str(), user); + SyncInfo info(user, app); + syncManager.DoCloudSync(std::move(info)); + } + } + currentEvent_.reset(); +} + +void SyncManager::NetworkRecoveryManager::RecordSyncApps(const std::string &bundleName) +{ + if (currentEvent_) { + ZLOGI("record sync bundleName:%{public}s", bundleName.c_str()); + currentEvent_->syncApps.insert(bundleName); + } +} +std::unordered_set SyncManager::NetworkRecoveryManager::ExtractBundleNames(const std::map apps) +{ + std::unordered_set bundleNames; + for (auto &app : apps) { + bundleNames.insert(app.first); + } + return bundleNames; +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index a24f0ae66..047da8b90 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -31,6 +31,7 @@ #include "store/general_store.h" #include "store/general_value.h" #include "utils/ref_count.h" +#include namespace OHOS::CloudData { class SyncManager { @@ -104,7 +105,24 @@ public: void OnScreenUnlocked(int32_t user); void CleanCompensateSync(int32_t userId); static std::string GetPath(const StoreMetaData &meta); - + class NetworkRecoveryManager { + public: + NetworkRecoveryManager(SyncManager &syncManager): syncManager_(syncManager) {} + void OnNetworkDisconnected(); + void OnNetworkConnected(); + void RecordSyncApps(const std::string &bundleName); + private: + std::unordered_set ExtractBundleNames(const std::map apps); + struct NetWorkEvent { + std::chrono::system_clock::time_point disconnectTime; + std::unordered_set syncApps; + }; + std::unique_ptr currentEvent_; + SyncManager &syncManager_; + }; + NetworkRecoveryManager& GetNetworkRecoveryManager() { + return networkRecoveryManager_; + } private: using Event = DistributedData::Event; using Task = ExecutorPool::Task; @@ -185,6 +203,7 @@ private: ConcurrentMap> lastSyncInfos_; std::set kvApps_; ConcurrentMap>> compensateSyncInfos_; + NetworkRecoveryManager networkRecoveryManager_{*this}; }; } // namespace OHOS::CloudData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_SYNC_MANAGER_H \ No newline at end of file -- Gitee From 3f4618c5ab448302e9c30de9b1d504194897e6b3 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Thu, 21 Aug 2025 14:32:28 +0800 Subject: [PATCH 02/12] fix: codecheck Signed-off-by: weishaoxiong --- .../service/cloud/sync_manager.cpp | 9 +++++---- .../service/cloud/sync_manager.h | 14 ++++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 5c6c4b1a6..7c147135f 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -1113,7 +1113,7 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() ZLOGE("no foreground user, skip sync."); return; } - auto& syncManager = syncManager_; + auto &syncManager = syncManager_; for (auto user : users) { CloudInfo cloud; cloud.user = user; @@ -1121,8 +1121,8 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() continue; } ZLOGI("network connected, sync hours:%{public}ld", hours); - const auto& apps = timeout ? ExtractBundleNames(cloud.apps) : currentEvent_->syncApps; - for (const auto& app : apps) { + const auto &apps = timeout ? ExtractBundleNames(cloud.apps) : currentEvent_->syncApps; + for (const auto &app : apps) { ZLOGI("sync app:%{public}s, user:%{public}d", app.c_str(), user); SyncInfo info(user, app); syncManager.DoCloudSync(std::move(info)); @@ -1138,7 +1138,8 @@ void SyncManager::NetworkRecoveryManager::RecordSyncApps(const std::string &bund currentEvent_->syncApps.insert(bundleName); } } -std::unordered_set SyncManager::NetworkRecoveryManager::ExtractBundleNames(const std::map apps) +std::unordered_set SyncManager::NetworkRecoveryManager::ExtractBundleNames( + const std::map apps) { std::unordered_set bundleNames; for (auto &app : apps) { diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index 047da8b90..0a56cfd2a 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -107,12 +107,16 @@ public: static std::string GetPath(const StoreMetaData &meta); class NetworkRecoveryManager { public: - NetworkRecoveryManager(SyncManager &syncManager): syncManager_(syncManager) {} + NetworkRecoveryManager(SyncManager &syncManager) : syncManager_(syncManager) + { + } void OnNetworkDisconnected(); void OnNetworkConnected(); void RecordSyncApps(const std::string &bundleName); + private: - std::unordered_set ExtractBundleNames(const std::map apps); + std::unordered_set ExtractBundleNames( + const std::map apps); struct NetWorkEvent { std::chrono::system_clock::time_point disconnectTime; std::unordered_set syncApps; @@ -120,9 +124,11 @@ public: std::unique_ptr currentEvent_; SyncManager &syncManager_; }; - NetworkRecoveryManager& GetNetworkRecoveryManager() { + NetworkRecoveryManager &GetNetworkRecoveryManager() + { return networkRecoveryManager_; } + private: using Event = DistributedData::Event; using Task = ExecutorPool::Task; @@ -203,7 +209,7 @@ private: ConcurrentMap> lastSyncInfos_; std::set kvApps_; ConcurrentMap>> compensateSyncInfos_; - NetworkRecoveryManager networkRecoveryManager_{*this}; + NetworkRecoveryManager networkRecoveryManager_{ *this }; }; } // namespace OHOS::CloudData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_SYNC_MANAGER_H \ No newline at end of file -- Gitee From bb97ed7363e880b6260686362a0521ca825fd724 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Thu, 21 Aug 2025 20:12:29 +0800 Subject: [PATCH 03/12] fix: Signed-off-by: weishaoxiong --- .../service/cloud/cloud_service_impl.cpp | 10 +++ .../service/cloud/sync_manager.cpp | 76 ++++++++++--------- .../service/cloud/sync_manager.h | 10 +-- 3 files changed, 57 insertions(+), 39 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 18f294fff..903657cc7 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -796,10 +796,20 @@ int32_t CloudServiceImpl::OnReady(const std::string &device) ZLOGW("current user is not logged in."); return E_NOT_LOGIN; } + std::vector users; + Account::GetInstance()->QueryForegroundUsers(users); + if (users.empty()) { + return SUCCESS; + } if (!NetworkDelegate::GetInstance()->IsNetworkAvailable()) { return NETWORK_ERROR; } + + for (auto user : users) { + Execute(GenTask(0, user, CloudSyncScene::NETWORK_RECOVERY, + { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE, WORK_SUB })); + } syncManager_.GetNetworkRecoveryManager().OnNetworkConnected(); return SUCCESS; } diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 7c147135f..c71d80343 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -365,8 +365,8 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t times, bool retry, RefCount UpdateStartSyncInfo(cloudSyncInfos); auto code = IsValid(info, cloud); if (code != E_OK) { - if (code == E_NETWORK_ERROR) { - networkRecoveryManager_.RecordSyncApps(info.bundleName_); + if (code == E_NETWORK_ERROR && retry) { + networkRecoveryManager_.RecordSyncApps(info.user_, info.bundleName_); } BatchUpdateFinishState(cloudSyncInfos, code); BatchReport(info.user_, traceIds, SyncStage::END, code, "!IsValid"); @@ -1098,53 +1098,61 @@ void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() void SyncManager::NetworkRecoveryManager::OnNetworkConnected() { - ZLOGI("network connected."); + ZLOGI("network connected start."); if (!currentEvent_) { - ZLOGE("network connected, but no sync event."); + ZLOGE("network connected, but currentEvent_ is not initialized."); return; } auto now = std::chrono::system_clock::now(); auto duration = now - currentEvent_->disconnectTime; auto hours = std::chrono::duration_cast(duration).count(); bool timeout = (hours > TIMEOUT_TIME); - - std::vector users; - if (!Account::GetInstance()->QueryForegroundUsers(users) || users.empty()) { - ZLOGE("no foreground user, skip sync."); - return; - } - auto &syncManager = syncManager_; - for (auto user : users) { - CloudInfo cloud; - cloud.user = user; - if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), cloud, true)) { - continue; - } - ZLOGI("network connected, sync hours:%{public}ld", hours); - const auto &apps = timeout ? ExtractBundleNames(cloud.apps) : currentEvent_->syncApps; - for (const auto &app : apps) { - ZLOGI("sync app:%{public}s, user:%{public}d", app.c_str(), user); - SyncInfo info(user, app); - syncManager.DoCloudSync(std::move(info)); - } - } + CompensateSync(timeout); currentEvent_.reset(); + ZLOGI("network connected end, sync hours:%{public}ld", hours); } -void SyncManager::NetworkRecoveryManager::RecordSyncApps(const std::string &bundleName) +void SyncManager::NetworkRecoveryManager::RecordSyncApps(const int32_t user, const std::string &bundleName) { if (currentEvent_) { - ZLOGI("record sync bundleName:%{public}s", bundleName.c_str()); - currentEvent_->syncApps.insert(bundleName); + ZLOGI("record sync user:%{public}d, bundleName:%{public}s", user, bundleName.c_str()); + std::lock_guard lock(syncAppsMutex_); + currentEvent_->syncApps[user].insert(bundleName); } } -std::unordered_set SyncManager::NetworkRecoveryManager::ExtractBundleNames( - const std::map apps) +void SyncManager::NetworkRecoveryManager::CompensateSync(bool timeout) { - std::unordered_set bundleNames; - for (auto &app : apps) { - bundleNames.insert(app.first); + std::vector users; + if (!Account::GetInstance()->QueryForegroundUsers(users) || users.empty()) { + ZLOGE("no foreground user, skip sync."); + return; + } + if (timeout) { + for (auto user : users) { + CloudInfo cloud; + cloud.user = user; + if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), cloud, true)) { + ZLOGE("load cloud info fail, user:%{public}d", user); + continue; + } + for (const auto &app : cloud.apps) { + ZLOGI("sync start bundleName:%{public}s, user:%{public}d", app.second.bundleName.c_str(), user); + syncManager_.DoCloudSync(SyncInfo(user, app.second.bundleName)); + } + } + } else { + std::lock_guard lock(syncAppsMutex_); + for (int32_t user : users) { + auto item = currentEvent_->syncApps.find(user); + if (item == currentEvent_->syncApps.end()) { + continue; + } + const auto &apps = item->second; + for (const auto &app : apps) { + ZLOGI("sync start bundleName:%{public}s, user:%{public}d", app.c_str(), user); + syncManager_.DoCloudSync(SyncInfo(user, app)); + } + } } - return bundleNames; } } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index 0a56cfd2a..fff9f0bf9 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -107,20 +107,20 @@ public: static std::string GetPath(const StoreMetaData &meta); class NetworkRecoveryManager { public: - NetworkRecoveryManager(SyncManager &syncManager) : syncManager_(syncManager) + explicit NetworkRecoveryManager(SyncManager &syncManager) : syncManager_(syncManager) { } void OnNetworkDisconnected(); void OnNetworkConnected(); - void RecordSyncApps(const std::string &bundleName); + void RecordSyncApps(const int32_t user, const std::string &bundleName); private: - std::unordered_set ExtractBundleNames( - const std::map apps); + void CompensateSync(bool timeout); struct NetWorkEvent { std::chrono::system_clock::time_point disconnectTime; - std::unordered_set syncApps; + std::map> syncApps; }; + std::mutex syncAppsMutex_; std::unique_ptr currentEvent_; SyncManager &syncManager_; }; -- Gitee From 9dd4498a7abff33bc95803bfa2d542aa403e633d Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Thu, 21 Aug 2025 20:15:04 +0800 Subject: [PATCH 04/12] fix: Signed-off-by: weishaoxiong --- .../distributeddataservice/service/cloud/cloud_service_impl.cpp | 2 -- services/distributeddataservice/service/cloud/sync_manager.cpp | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 903657cc7..4542e32be 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -801,11 +801,9 @@ int32_t CloudServiceImpl::OnReady(const std::string &device) if (users.empty()) { return SUCCESS; } - if (!NetworkDelegate::GetInstance()->IsNetworkAvailable()) { return NETWORK_ERROR; } - for (auto user : users) { Execute(GenTask(0, user, CloudSyncScene::NETWORK_RECOVERY, { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE, WORK_SUB })); diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index c71d80343..5054bb284 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -1120,6 +1120,7 @@ void SyncManager::NetworkRecoveryManager::RecordSyncApps(const int32_t user, con currentEvent_->syncApps[user].insert(bundleName); } } + void SyncManager::NetworkRecoveryManager::CompensateSync(bool timeout) { std::vector users; -- Gitee From 5cd2e768d904faf09192e733d6b64cc1dcfd4841 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Fri, 22 Aug 2025 15:30:53 +0800 Subject: [PATCH 05/12] fix: Signed-off-by: weishaoxiong --- .../service/cloud/sync_manager.cpp | 70 +++++++++++-------- .../service/cloud/sync_manager.h | 5 +- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 5054bb284..003dad5fe 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -365,7 +365,7 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t times, bool retry, RefCount UpdateStartSyncInfo(cloudSyncInfos); auto code = IsValid(info, cloud); if (code != E_OK) { - if (code == E_NETWORK_ERROR && retry) { + if (code == E_NETWORK_ERROR) { networkRecoveryManager_.RecordSyncApps(info.user_, info.bundleName_); } BatchUpdateFinishState(cloudSyncInfos, code); @@ -1107,9 +1107,20 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() auto duration = now - currentEvent_->disconnectTime; auto hours = std::chrono::duration_cast(duration).count(); bool timeout = (hours > TIMEOUT_TIME); - CompensateSync(timeout); + std::vector users; + if (!Account::GetInstance()->QueryForegroundUsers(users) || users.empty()) { + ZLOGE("no foreground user, skip sync."); + return; + } + for (auto user : users) { + auto syncApps = GetAppList(user, timeout); + for (const auto &bundleName : syncApps) { + ZLOGI("sync start bundleName:%{public}s, user:%{public}d", bundleName.c_str(), user); + syncManager_.DoCloudSync(SyncInfo(user, bundleName, "", {}, MODE_ONLINE)); + } + } currentEvent_.reset(); - ZLOGI("network connected end, sync hours:%{public}ld", hours); + ZLOGI("network connected end, network disconnect duration :%{public}ld hours", hours); } void SyncManager::NetworkRecoveryManager::RecordSyncApps(const int32_t user, const std::string &bundleName) @@ -1117,43 +1128,42 @@ void SyncManager::NetworkRecoveryManager::RecordSyncApps(const int32_t user, con if (currentEvent_) { ZLOGI("record sync user:%{public}d, bundleName:%{public}s", user, bundleName.c_str()); std::lock_guard lock(syncAppsMutex_); - currentEvent_->syncApps[user].insert(bundleName); + auto &syncApps = currentEvent_->syncApps[user]; + if (std::find(syncApps.begin(), syncApps.end(), bundleName) == syncApps.end()) { + syncApps.push_back(bundleName); + } } } -void SyncManager::NetworkRecoveryManager::CompensateSync(bool timeout) +std::vector SyncManager::NetworkRecoveryManager::GetAppList(const int32_t user, bool timeout) { - std::vector users; - if (!Account::GetInstance()->QueryForegroundUsers(users) || users.empty()) { - ZLOGE("no foreground user, skip sync."); - return; - } + std::vector appList; if (timeout) { - for (auto user : users) { - CloudInfo cloud; - cloud.user = user; - if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), cloud, true)) { - ZLOGE("load cloud info fail, user:%{public}d", user); - continue; - } - for (const auto &app : cloud.apps) { - ZLOGI("sync start bundleName:%{public}s, user:%{public}d", app.second.bundleName.c_str(), user); - syncManager_.DoCloudSync(SyncInfo(user, app.second.bundleName)); + CloudInfo cloud; + cloud.user = user; + if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), cloud, true)) { + ZLOGE("load cloud info fail, user:%{public}d", user); + return appList; + } + auto stores = CheckerManager::GetInstance().GetDynamicStores(); + auto staticStores = CheckerManager::GetInstance().GetStaticStores(); + stores.insert(stores.end(), staticStores.begin(), staticStores.end()); + for (const auto &store : stores) { + appList.push_back(store.bundleName); + } + for (const auto &app : cloud.apps) { + if (std::find(appList.begin(), appList.end(), app.second.bundleName) == appList.end()) { + appList.push_back(app.second.bundleName); } } } else { std::lock_guard lock(syncAppsMutex_); - for (int32_t user : users) { - auto item = currentEvent_->syncApps.find(user); - if (item == currentEvent_->syncApps.end()) { - continue; - } - const auto &apps = item->second; - for (const auto &app : apps) { - ZLOGI("sync start bundleName:%{public}s, user:%{public}d", app.c_str(), user); - syncManager_.DoCloudSync(SyncInfo(user, app)); - } + auto item = currentEvent_->syncApps.find(user); + if (item == currentEvent_->syncApps.end()) { + return appList; } + appList = item->second; } + return appList; } } // namespace OHOS::CloudData \ No newline at end of file diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index fff9f0bf9..ef2cc1b6e 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -31,7 +31,6 @@ #include "store/general_store.h" #include "store/general_value.h" #include "utils/ref_count.h" -#include namespace OHOS::CloudData { class SyncManager { @@ -115,10 +114,10 @@ public: void RecordSyncApps(const int32_t user, const std::string &bundleName); private: - void CompensateSync(bool timeout); + std::vector GetAppList(const int32_t user, bool timeout); struct NetWorkEvent { std::chrono::system_clock::time_point disconnectTime; - std::map> syncApps; + std::map> syncApps; }; std::mutex syncAppsMutex_; std::unique_ptr currentEvent_; -- Gitee From c7f72b0e4086239c29b311e609d4c4a30d5599fd Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Fri, 22 Aug 2025 17:54:46 +0800 Subject: [PATCH 06/12] fix: Signed-off-by: weishaoxiong --- .../service/cloud/sync_manager.cpp | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 003dad5fe..b483ac323 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -16,6 +16,7 @@ #include "sync_manager.h" #include +#include #include "account/account_delegate.h" #include "bootstrap.h" @@ -1137,32 +1138,38 @@ void SyncManager::NetworkRecoveryManager::RecordSyncApps(const int32_t user, con std::vector SyncManager::NetworkRecoveryManager::GetAppList(const int32_t user, bool timeout) { - std::vector appList; - if (timeout) { - CloudInfo cloud; - cloud.user = user; - if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), cloud, true)) { - ZLOGE("load cloud info fail, user:%{public}d", user); - return appList; - } - auto stores = CheckerManager::GetInstance().GetDynamicStores(); - auto staticStores = CheckerManager::GetInstance().GetStaticStores(); - stores.insert(stores.end(), staticStores.begin(), staticStores.end()); - for (const auto &store : stores) { - appList.push_back(store.bundleName); - } - for (const auto &app : cloud.apps) { - if (std::find(appList.begin(), appList.end(), app.second.bundleName) == appList.end()) { - appList.push_back(app.second.bundleName); - } - } - } else { + if (!timeout) { std::lock_guard lock(syncAppsMutex_); - auto item = currentEvent_->syncApps.find(user); - if (item == currentEvent_->syncApps.end()) { - return appList; + if (auto it = currentEvent_->syncApps.find(user); it != currentEvent_->syncApps.end()) { + return it->second; + } + return {}; + } + + CloudInfo cloud; + cloud.user = user; + if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), cloud, true)) { + ZLOGE("load cloud info fail, user:%{public}d", user); + return {}; + } + const size_t totalCount = cloud.apps.size(); + std::vector appList; + appList.reserve(totalCount); + std::unordered_set uniqueSet; + uniqueSet.reserve(totalCount); + auto addApp = [&](const std::string &bundleName) { + if (uniqueSet.insert(bundleName).second) { + appList.push_back(bundleName); } - appList = item->second; + }; + auto stores = CheckerManager::GetInstance().GetDynamicStores(); + auto staticStores = CheckerManager::GetInstance().GetStaticStores(); + stores.insert(stores.end(), staticStores.begin(), staticStores.end()); + for (const auto &store : stores) { + addApp(store.bundleName); + } + for (const auto &[_, app] : cloud.apps) { + addApp(app.bundleName); } return appList; } -- Gitee From 1b1f17d747a88ee032fe9ffee30cbf73f022d0d9 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Sat, 23 Aug 2025 10:51:19 +0800 Subject: [PATCH 07/12] fix: Signed-off-by: weishaoxiong --- .../service/test/BUILD.gn | 1 + .../service/test/cloud_service_impl_test.cpp | 174 ++++++++++++++++++ 2 files changed, 175 insertions(+) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 1f6ecbcfd..5ef8ef536 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -187,6 +187,7 @@ ohos_unittest("CloudServiceImplTest") { "${data_service_path}/service/cloud/sync_strategies/network_sync_strategy.cpp", "${data_service_path}/service/test/mock/checker_mock.cpp", "cloud_service_impl_test.cpp", + "mock/account_delegate_mock.cpp", ] configs = [ ":module_private_config" ] diff --git a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp index dbe572a55..d2d31f17e 100644 --- a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp @@ -40,6 +40,7 @@ #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" +#include "mock/account_delegate_mock.h" #include "mock/db_store_mock.h" #include "mock/general_store_mock.h" #include "mock/meta_data_manager_mock.h" @@ -68,6 +69,8 @@ static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; static constexpr const char *TEST_CLOUD_STORE = "test_cloud_store"; static constexpr const char *TEST_CLOUD_DATABASE_ALIAS_1 = "test_cloud_database_alias_1"; +constexpr const int32_t DISCONNECT_TIME = 21; +constexpr const int32_t MOCK_USER = 200; class CloudServiceImplTest : public testing::Test { public: static void SetUpTestCase(void); @@ -78,6 +81,7 @@ public: static std::shared_ptr cloudServiceImpl_; static inline std::shared_ptr metaDataManagerMock = nullptr; static NetworkDelegateMock delegate_; + static inline AccountDelegateMock *accountDelegateMock = nullptr; }; std::shared_ptr CloudServiceImplTest::cloudServiceImpl_ = std::make_shared(); @@ -778,5 +782,175 @@ HWTEST_F(ComponentConfigTest, ComponentConfig, TestSize.Level0) EXPECT_EQ(node["constructor"], componentConfig.constructor); EXPECT_EQ(node["destructor"], componentConfig.destructor); } + +/** + * @tc.name: NetworkRecoveryTest001 + * @tc.desc: test the compensatory sync strategy for network disconnection times of less than 20 hours + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) +{ + ASSERT_NE(cloudServiceImpl_, nullptr); + accountDelegateMock = new (std::nothrow) AccountDelegateMock(); + if (accountDelegateMock != nullptr) { + AccountDelegate::instance_ = nullptr; + AccountDelegate::RegisterAccountInstance(accountDelegateMock); + } + + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { + users = { MOCK_USER }; + return true; + })); + // 2 means that the QueryForegroundUsers interface will be called twice + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .Times(2) + .WillRepeatedly(Invoke([&](std::vector &users) -> bool { + users = { MOCK_USER }; + return true; + })); + std::string bundleName = "com.ohos.test"; + CloudInfo::AppInfo appInfo; + appInfo.bundleName = bundleName; + std::map apps; + apps.emplace(bundleName, appInfo); + CloudInfo cloudInfo; + cloudInfo.apps = apps; + cloudInfo.user = MOCK_USER; + MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); + auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_NE(recoveryManager.currentEvent_, nullptr); + recoveryManager.RecordSyncApps(MOCK_USER, bundleName); + recoveryManager.RecordSyncApps(MOCK_USER, bundleName); + EXPECT_EQ(recoveryManager.currentEvent_->syncApps.size(), 1); + cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); +} + +/** + * @tc.name: NetworkRecoveryTest002 + * @tc.desc: test the compensatory sync strategy for network disconnection times of more than 20 hours + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest002, TestSize.Level0) +{ + ASSERT_NE(cloudServiceImpl_, nullptr); + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { + users = { MOCK_USER }; + return true; + })); + // 2 means that the QueryForegroundUsers interface will be called twice + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .Times(2) + .WillRepeatedly(Invoke([&](std::vector &users) -> bool { + users = { MOCK_USER }; + return true; + })); + auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_NE(recoveryManager.currentEvent_, nullptr); + recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); + cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); +} + +/** + * @tc.name: NetworkRecoveryTest003 + * @tc.desc: The test only calls the network connection interface but not disconnect + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest003, TestSize.Level0) +{ + ASSERT_NE(cloudServiceImpl_, nullptr); + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { + users = { MOCK_USER }; + return true; + })); + auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + recoveryManager.RecordSyncApps(MOCK_USER, ""); + cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); +} + +/** + * @tc.name: NetworkRecoveryTest004 + * @tc.desc: The QueryForegroundUsers interface call fails when the network is restored + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest004, TestSize.Level0) +{ + ASSERT_NE(cloudServiceImpl_, nullptr); + auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + recoveryManager.OnNetworkDisconnected(); + auto returnWithUserList = + [](const std::vector &users) { + return Invoke([=](std::vector &outUsers) -> bool { + outUsers = users; + return true; + }); + }; + + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .WillOnce(returnWithUserList({ MOCK_USER })) + .WillOnce(Return(false)); + cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); + + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .WillOnce(returnWithUserList({ MOCK_USER })) + .WillOnce(returnWithUserList({})); + cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_NE(recoveryManager.currentEvent_, nullptr); +} + +/** + * @tc.name: NetworkRecoveryTest005 + * @tc.desc: The test network connection interface call fails when the load cloudInfo failed + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest005, TestSize.Level0) +{ + ASSERT_NE(cloudServiceImpl_, nullptr); + EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { + users = { MOCK_USER }; + return true; + })); + // 2 means that the QueryForegroundUsers interface will be called twice + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) + .Times(2) + .WillRepeatedly(Invoke([&](std::vector &users) -> bool { + users = { MOCK_USER }; + return true; + })); + CloudInfo cloudInfo; + cloudInfo.user = MOCK_USER; + MetaDataManager::GetInstance().DelMeta(cloudInfo.GetKey(), true); + auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_NE(recoveryManager.currentEvent_, nullptr); + recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); + cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); + if (accountDelegateMock != nullptr) { + delete accountDelegateMock; + accountDelegateMock = nullptr; + } +} } // namespace DistributedDataTest } // namespace OHOS::Test \ No newline at end of file -- Gitee From 9648ce9da19d2a8f31c28cf0977adc507a30dc22 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Mon, 25 Aug 2025 11:46:44 +0800 Subject: [PATCH 08/12] fix: Signed-off-by: weishaoxiong --- .../service/cloud/sync_manager.cpp | 32 +++++++++---------- .../service/cloud/sync_manager.h | 4 +-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index b483ac323..401b3e6bf 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -1093,6 +1093,7 @@ int32_t SyncManager::ConvertValidGeneralCode(int32_t code) void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() { ZLOGI("network disconnected."); + std::lock_guard lock(eventMutex_); currentEvent_ = std::make_unique(); currentEvent_->disconnectTime = std::chrono::system_clock::now(); } @@ -1100,12 +1101,17 @@ void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() void SyncManager::NetworkRecoveryManager::OnNetworkConnected() { ZLOGI("network connected start."); - if (!currentEvent_) { - ZLOGE("network connected, but currentEvent_ is not initialized."); - return; + std::unique_ptr event; + { + std::lock_guard lock(eventMutex_); + if (!currentEvent_) { + ZLOGE("network connected, but currentEvent_ is not initialized."); + return; + } + event = std::move(currentEvent_); } auto now = std::chrono::system_clock::now(); - auto duration = now - currentEvent_->disconnectTime; + auto duration = now - event->disconnectTime; auto hours = std::chrono::duration_cast(duration).count(); bool timeout = (hours > TIMEOUT_TIME); std::vector users; @@ -1114,21 +1120,23 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() return; } for (auto user : users) { - auto syncApps = GetAppList(user, timeout); + std::vector syncApps(event->syncApps[user]); + if (timeout) { + syncApps = GetAppList(user); + } for (const auto &bundleName : syncApps) { ZLOGI("sync start bundleName:%{public}s, user:%{public}d", bundleName.c_str(), user); syncManager_.DoCloudSync(SyncInfo(user, bundleName, "", {}, MODE_ONLINE)); } } - currentEvent_.reset(); ZLOGI("network connected end, network disconnect duration :%{public}ld hours", hours); } void SyncManager::NetworkRecoveryManager::RecordSyncApps(const int32_t user, const std::string &bundleName) { + std::lock_guard lock(eventMutex_); if (currentEvent_) { ZLOGI("record sync user:%{public}d, bundleName:%{public}s", user, bundleName.c_str()); - std::lock_guard lock(syncAppsMutex_); auto &syncApps = currentEvent_->syncApps[user]; if (std::find(syncApps.begin(), syncApps.end(), bundleName) == syncApps.end()) { syncApps.push_back(bundleName); @@ -1136,16 +1144,8 @@ void SyncManager::NetworkRecoveryManager::RecordSyncApps(const int32_t user, con } } -std::vector SyncManager::NetworkRecoveryManager::GetAppList(const int32_t user, bool timeout) +std::vector SyncManager::NetworkRecoveryManager::GetAppList(const int32_t user) { - if (!timeout) { - std::lock_guard lock(syncAppsMutex_); - if (auto it = currentEvent_->syncApps.find(user); it != currentEvent_->syncApps.end()) { - return it->second; - } - return {}; - } - CloudInfo cloud; cloud.user = user; if (!MetaDataManager::GetInstance().LoadMeta(cloud.GetKey(), cloud, true)) { diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index ef2cc1b6e..89e36115b 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -114,12 +114,12 @@ public: void RecordSyncApps(const int32_t user, const std::string &bundleName); private: - std::vector GetAppList(const int32_t user, bool timeout); + std::vector GetAppList(const int32_t user); struct NetWorkEvent { std::chrono::system_clock::time_point disconnectTime; std::map> syncApps; }; - std::mutex syncAppsMutex_; + std::mutex eventMutex_; std::unique_ptr currentEvent_; SyncManager &syncManager_; }; -- Gitee From da3d11a54c61fd509e0a49281ac0a36d2e34ded8 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Tue, 26 Aug 2025 10:04:54 +0800 Subject: [PATCH 09/12] fix: Signed-off-by: weishaoxiong --- .../service/test/cloud_service_impl_test.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp index 8f8c9f113..e66f7ad97 100644 --- a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp @@ -1076,6 +1076,7 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest004, TestSize.Level0) ASSERT_NE(cloudServiceImpl_, nullptr); auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); recoveryManager.OnNetworkDisconnected(); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); auto returnWithUserList = [](const std::vector &users) { return Invoke([=](std::vector &outUsers) -> bool { @@ -1089,14 +1090,16 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest004, TestSize.Level0) .WillOnce(returnWithUserList({ MOCK_USER })) .WillOnce(Return(false)); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - ASSERT_NE(recoveryManager.currentEvent_, nullptr); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); + recoveryManager.OnNetworkDisconnected(); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) .WillOnce(returnWithUserList({ MOCK_USER })) .WillOnce(returnWithUserList({})); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_NE(recoveryManager.currentEvent_, nullptr); + EXPECT_EQ(recoveryManager.currentEvent_, nullptr); } /** -- Gitee From 572102a612325b6cd470e0e289742f137af0f1bc Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Wed, 27 Aug 2025 15:17:04 +0800 Subject: [PATCH 10/12] fix: Signed-off-by: weishaoxiong --- .../service/cloud/cloud_service_impl.cpp | 4 +-- .../service/cloud/sync_manager.cpp | 25 +++++++++++++------ .../service/cloud/sync_manager.h | 6 ++--- .../service/test/cloud_service_impl_test.cpp | 10 ++++---- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 4542e32be..a1f141257 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -808,7 +808,7 @@ int32_t CloudServiceImpl::OnReady(const std::string &device) Execute(GenTask(0, user, CloudSyncScene::NETWORK_RECOVERY, { WORK_CLOUD_INFO_UPDATE, WORK_SCHEMA_UPDATE, WORK_SUB })); } - syncManager_.GetNetworkRecoveryManager().OnNetworkConnected(); + syncManager_.OnNetworkConnected(); return SUCCESS; } @@ -825,7 +825,7 @@ int32_t CloudServiceImpl::Offline(const std::string &device) } auto it = users.begin(); syncManager_.StopCloudSync(*it); - syncManager_.GetNetworkRecoveryManager().OnNetworkDisconnected(); + syncManager_.OnNetworkDisconnected(); return SUCCESS; } diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 1354cb407..39f53e224 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -1087,6 +1087,16 @@ int32_t SyncManager::ConvertValidGeneralCode(int32_t code) return (code >= E_OK && code < E_BUSY) ? code : E_ERROR; } +void SyncManager::OnNetworkDisconnected() +{ + networkRecoveryManager_.OnNetworkDisconnected(); +} + +void SyncManager::OnNetworkConnected() +{ + networkRecoveryManager_.OnNetworkConnected(); +} + void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() { ZLOGI("network disconnected."); @@ -1097,7 +1107,6 @@ void SyncManager::NetworkRecoveryManager::OnNetworkDisconnected() void SyncManager::NetworkRecoveryManager::OnNetworkConnected() { - ZLOGI("network connected start."); std::unique_ptr event; { std::lock_guard lock(eventMutex_); @@ -1126,17 +1135,17 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() syncManager_.DoCloudSync(SyncInfo(user, bundleName, "", {}, MODE_ONLINE)); } } - ZLOGI("network connected end, network disconnect duration :%{public}ld hours", hours); + ZLOGI("network connected success, network disconnect duration :%{public}ld hours", hours); } void SyncManager::NetworkRecoveryManager::RecordSyncApps(const int32_t user, const std::string &bundleName) { std::lock_guard lock(eventMutex_); if (currentEvent_) { - ZLOGI("record sync user:%{public}d, bundleName:%{public}s", user, bundleName.c_str()); auto &syncApps = currentEvent_->syncApps[user]; if (std::find(syncApps.begin(), syncApps.end(), bundleName) == syncApps.end()) { syncApps.push_back(bundleName); + ZLOGI("record sync user:%{public}d, bundleName:%{public}s", user, bundleName.c_str()); } } } @@ -1154,19 +1163,19 @@ std::vector SyncManager::NetworkRecoveryManager::GetAppList(const i appList.reserve(totalCount); std::unordered_set uniqueSet; uniqueSet.reserve(totalCount); - auto addApp = [&](const std::string &bundleName) { - if (uniqueSet.insert(bundleName).second) { - appList.push_back(bundleName); + auto addApp = [&](std::string bundleName) { + if (uniqueSet.insert(std::move(bundleName)).second) { + appList.push_back(std::move(bundleName)); } }; auto stores = CheckerManager::GetInstance().GetDynamicStores(); auto staticStores = CheckerManager::GetInstance().GetStaticStores(); stores.insert(stores.end(), staticStores.begin(), staticStores.end()); for (const auto &store : stores) { - addApp(store.bundleName); + addApp(std::move(store.bundleName)); } for (const auto &[_, app] : cloud.apps) { - addApp(app.bundleName); + addApp(std::move(app.bundleName)); } return appList; } diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index 89e36115b..ad5efa767 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -123,11 +123,9 @@ public: std::unique_ptr currentEvent_; SyncManager &syncManager_; }; - NetworkRecoveryManager &GetNetworkRecoveryManager() - { - return networkRecoveryManager_; - } + void OnNetworkDisconnected(); + void OnNetworkConnected(); private: using Event = DistributedData::Event; using Task = ExecutorPool::Task; diff --git a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp index e66f7ad97..bc45b8553 100644 --- a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp @@ -1003,7 +1003,7 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) cloudInfo.apps = apps; cloudInfo.user = MOCK_USER; MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); - auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_NE(recoveryManager.currentEvent_, nullptr); recoveryManager.RecordSyncApps(MOCK_USER, bundleName); @@ -1035,7 +1035,7 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest002, TestSize.Level0) users = { MOCK_USER }; return true; })); - auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_NE(recoveryManager.currentEvent_, nullptr); recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); @@ -1058,7 +1058,7 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest003, TestSize.Level0) users = { MOCK_USER }; return true; })); - auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; recoveryManager.RecordSyncApps(MOCK_USER, ""); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_EQ(recoveryManager.currentEvent_, nullptr); @@ -1074,7 +1074,7 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest003, TestSize.Level0) HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest004, TestSize.Level0) { ASSERT_NE(cloudServiceImpl_, nullptr); - auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; recoveryManager.OnNetworkDisconnected(); ASSERT_NE(recoveryManager.currentEvent_, nullptr); auto returnWithUserList = @@ -1127,7 +1127,7 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest005, TestSize.Level0) CloudInfo cloudInfo; cloudInfo.user = MOCK_USER; MetaDataManager::GetInstance().DelMeta(cloudInfo.GetKey(), true); - auto &recoveryManager = cloudServiceImpl_->syncManager_.GetNetworkRecoveryManager(); + auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_NE(recoveryManager.currentEvent_, nullptr); recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); -- Gitee From ff82b1cce37fbd91625257be8065ed507fd2ed75 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Thu, 28 Aug 2025 10:40:53 +0800 Subject: [PATCH 11/12] fix: Signed-off-by: weishaoxiong --- .../service/cloud/sync_manager.cpp | 13 ++++++++----- .../service/cloud/sync_manager.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 39f53e224..ee8fb3b06 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -48,7 +48,7 @@ using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; using Defer = EventCenter::Defer; std::atomic SyncManager::genId_ = 0; constexpr int32_t SYSTEM_USER_ID = 0; -constexpr int32_t TIMEOUT_TIME = 20; // hours +constexpr int32_t NETWORK_DISCONNECT_TIMEOUT_HOURS = 20; static constexpr const char *FT_GET_STORE = "GET_STORE"; static constexpr const char *FT_CALLBACK = "CALLBACK"; SyncManager::SyncInfo::SyncInfo( @@ -515,6 +515,9 @@ bool SyncManager::HandleRetryFinished(const SyncInfo &info, int32_t user, int32_ if (code == E_OK || code == E_SYNC_TASK_MERGED) { return true; } + if (code == E_NETWORK_ERROR) { + networkRecoveryManager_.RecordSyncApps(user, info.bundleName_); + } info.SetError(code); RadarReporter::Report({ info.bundleName_.c_str(), CLOUD_SYNC, FINISH_SYNC, info.syncId_, info.triggerMode_, dbCode }, @@ -1110,7 +1113,7 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() std::unique_ptr event; { std::lock_guard lock(eventMutex_); - if (!currentEvent_) { + if (currentEvent_ == nullptr) { ZLOGE("network connected, but currentEvent_ is not initialized."); return; } @@ -1119,7 +1122,7 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() auto now = std::chrono::system_clock::now(); auto duration = now - event->disconnectTime; auto hours = std::chrono::duration_cast(duration).count(); - bool timeout = (hours > TIMEOUT_TIME); + bool timeout = (hours > NETWORK_DISCONNECT_TIMEOUT_HOURS); std::vector users; if (!Account::GetInstance()->QueryForegroundUsers(users) || users.empty()) { ZLOGE("no foreground user, skip sync."); @@ -1141,7 +1144,7 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() void SyncManager::NetworkRecoveryManager::RecordSyncApps(const int32_t user, const std::string &bundleName) { std::lock_guard lock(eventMutex_); - if (currentEvent_) { + if (currentEvent_ != nullptr) { auto &syncApps = currentEvent_->syncApps[user]; if (std::find(syncApps.begin(), syncApps.end(), bundleName) == syncApps.end()) { syncApps.push_back(bundleName); @@ -1164,7 +1167,7 @@ std::vector SyncManager::NetworkRecoveryManager::GetAppList(const i std::unordered_set uniqueSet; uniqueSet.reserve(totalCount); auto addApp = [&](std::string bundleName) { - if (uniqueSet.insert(std::move(bundleName)).second) { + if (uniqueSet.insert(bundleName).second) { appList.push_back(std::move(bundleName)); } }; diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index ad5efa767..443e75022 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -170,7 +170,7 @@ private: const std::string &message = ""); static void ReportSyncEvent(const DistributedData::SyncEvent &evt, DistributedDataDfx::BizState bizState, int32_t code); - static bool HandleRetryFinished(const SyncInfo &info, int32_t user, int32_t code, int32_t dbCode, + bool HandleRetryFinished(const SyncInfo &info, int32_t user, int32_t code, int32_t dbCode, const std::string &prepareTraceId); Task GetSyncTask(int32_t times, bool retry, RefCount ref, SyncInfo &&syncInfo); void UpdateSchema(const SyncInfo &syncInfo); -- Gitee From c43fa98d3571a173fe61e155a4f010fc84fd6a32 Mon Sep 17 00:00:00 2001 From: weishaoxiong Date: Fri, 29 Aug 2025 10:48:17 +0800 Subject: [PATCH 12/12] fix: Signed-off-by: weishaoxiong --- .../service/cloud/sync_manager.cpp | 5 +- .../service/cloud/sync_manager.h | 8 +- .../service/test/cloud_service_impl_test.cpp | 113 ++++++++++-------- 3 files changed, 69 insertions(+), 57 deletions(-) diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index ee8fb3b06..ea2bc3db7 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -1129,10 +1129,7 @@ void SyncManager::NetworkRecoveryManager::OnNetworkConnected() return; } for (auto user : users) { - std::vector syncApps(event->syncApps[user]); - if (timeout) { - syncApps = GetAppList(user); - } + const auto &syncApps = timeout ? GetAppList(user) : event->syncApps[user]; for (const auto &bundleName : syncApps) { ZLOGI("sync start bundleName:%{public}s, user:%{public}d", bundleName.c_str(), user); syncManager_.DoCloudSync(SyncInfo(user, bundleName, "", {}, MODE_ONLINE)); diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index 443e75022..1e152ca10 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -104,6 +104,10 @@ public: void OnScreenUnlocked(int32_t user); void CleanCompensateSync(int32_t userId); static std::string GetPath(const StoreMetaData &meta); + void OnNetworkDisconnected(); + void OnNetworkConnected(); + +private: class NetworkRecoveryManager { public: explicit NetworkRecoveryManager(SyncManager &syncManager) : syncManager_(syncManager) @@ -123,10 +127,6 @@ public: std::unique_ptr currentEvent_; SyncManager &syncManager_; }; - - void OnNetworkDisconnected(); - void OnNetworkConnected(); -private: using Event = DistributedData::Event; using Task = ExecutorPool::Task; using TaskId = ExecutorPool::TaskId; diff --git a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp index bc45b8553..6238c5271 100644 --- a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp @@ -83,6 +83,8 @@ public: static void CheckDelMeta(StoreMetaMapping &metaMapping, StoreMetaData &meta, StoreMetaData &meta1); static std::shared_ptr cloudServiceImpl_; static NetworkDelegateMock delegate_; + static auto ReturnWithUserList(const std::vector& users); + protected: static std::shared_ptr dbStoreMock_; static StoreMetaData metaData_; @@ -131,12 +133,22 @@ void CloudServiceImplTest::InitMetaData() metaData_.dataDir = "/test_cloud_service_impl_store"; } +auto CloudServiceImplTest::ReturnWithUserList(const std::vector &users) +{ + return Invoke([=](std::vector &outUsers) -> bool { + outUsers = users; + return true; + }); +} + void CloudServiceImplTest::SetUpTestCase(void) { size_t max = 12; size_t min = 5; auto executor = std::make_shared(max, min); DeviceManagerAdapter::GetInstance().Init(executor); + cloudServiceImpl_->OnBind( + { "CloudServiceImplTest", static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor) }); Bootstrap::GetInstance().LoadCheckers(); CryptoManager::GetInstance().GenerateRootKey(); MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, ""); @@ -977,38 +989,49 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) { ASSERT_NE(cloudServiceImpl_, nullptr); accountDelegateMock = new (std::nothrow) AccountDelegateMock(); - if (accountDelegateMock != nullptr) { - AccountDelegate::instance_ = nullptr; - AccountDelegate::RegisterAccountInstance(accountDelegateMock); - } + ASSERT_NE(accountDelegateMock, nullptr); + AccountDelegate::instance_ = nullptr; + AccountDelegate::RegisterAccountInstance(accountDelegateMock); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { - users = { MOCK_USER }; - return true; - })); + EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); // 2 means that the QueryForegroundUsers interface will be called twice EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) .Times(2) - .WillRepeatedly(Invoke([&](std::vector &users) -> bool { - users = { MOCK_USER }; - return true; - })); - std::string bundleName = "com.ohos.test"; + .WillRepeatedly(ReturnWithUserList({ MOCK_USER })); + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(MOCK_USER)); + delegate_.isNetworkAvailable_ = false; CloudInfo::AppInfo appInfo; - appInfo.bundleName = bundleName; + appInfo.bundleName = TEST_CLOUD_BUNDLE; + appInfo.cloudSwitch = true; std::map apps; - apps.emplace(bundleName, appInfo); + apps.emplace(TEST_CLOUD_BUNDLE, appInfo); CloudInfo cloudInfo; cloudInfo.apps = apps; cloudInfo.user = MOCK_USER; + cloudInfo.enableCloud = true; MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_NE(recoveryManager.currentEvent_, nullptr); - recoveryManager.RecordSyncApps(MOCK_USER, bundleName); - recoveryManager.RecordSyncApps(MOCK_USER, bundleName); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); + + SchemaMeta schemaMeta; + schemaMeta.bundleName = TEST_CLOUD_BUNDLE; + SchemaMeta::Database database; + database.name = TEST_CLOUD_STORE; + schemaMeta.databases.emplace_back(database); + MetaDataManager::GetInstance().SaveMeta(CloudInfo::GetSchemaKey(cloudInfo.user, TEST_CLOUD_BUNDLE), schemaMeta, + true); + CloudData::CloudService::Option option; + option.syncMode = DistributedData::GeneralStore::CLOUD_BEGIN; + auto async = [](const DistributedRdb::Details &details) {}; + cloudServiceImpl_->CloudSync(TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, option, async); + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(MOCK_USER)); + cloudServiceImpl_->CloudSync(TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, option, async); + sleep(1); EXPECT_EQ(recoveryManager.currentEvent_->syncApps.size(), 1); + delegate_.isNetworkAvailable_ = true; cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_EQ(recoveryManager.currentEvent_, nullptr); } @@ -1023,6 +1046,7 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest001, TestSize.Level0) HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest002, TestSize.Level0) { ASSERT_NE(cloudServiceImpl_, nullptr); + EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { users = { MOCK_USER }; @@ -1031,13 +1055,10 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest002, TestSize.Level0) // 2 means that the QueryForegroundUsers interface will be called twice EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) .Times(2) - .WillRepeatedly(Invoke([&](std::vector &users) -> bool { - users = { MOCK_USER }; - return true; - })); + .WillRepeatedly(ReturnWithUserList({ MOCK_USER })); auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_NE(recoveryManager.currentEvent_, nullptr); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_EQ(recoveryManager.currentEvent_, nullptr); @@ -1053,13 +1074,16 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest002, TestSize.Level0) HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest003, TestSize.Level0) { ASSERT_NE(cloudServiceImpl_, nullptr); + EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); - EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { - users = { MOCK_USER }; - return true; - })); + EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; - recoveryManager.RecordSyncApps(MOCK_USER, ""); + CloudData::CloudService::Option option; + option.syncMode = DistributedData::GeneralStore::CLOUD_BEGIN; + auto async = [](const DistributedRdb::Details &details) {}; + EXPECT_CALL(*accountDelegateMock, GetUserByToken(_)).WillOnce(Return(MOCK_USER)); + cloudServiceImpl_->CloudSync(TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, option, async); + sleep(1); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_EQ(recoveryManager.currentEvent_, nullptr); } @@ -1074,30 +1098,26 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest003, TestSize.Level0) HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest004, TestSize.Level0) { ASSERT_NE(cloudServiceImpl_, nullptr); + EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); + cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; - recoveryManager.OnNetworkDisconnected(); ASSERT_NE(recoveryManager.currentEvent_, nullptr); - auto returnWithUserList = - [](const std::vector &users) { - return Invoke([=](std::vector &outUsers) -> bool { - outUsers = users; - return true; - }); - }; EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) - .WillOnce(returnWithUserList({ MOCK_USER })) + .WillOnce(ReturnWithUserList({ MOCK_USER })) .WillOnce(Return(false)); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_EQ(recoveryManager.currentEvent_, nullptr); - recoveryManager.OnNetworkDisconnected(); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); + cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); ASSERT_NE(recoveryManager.currentEvent_, nullptr); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) - .WillOnce(returnWithUserList({ MOCK_USER })) - .WillOnce(returnWithUserList({})); + .WillOnce(ReturnWithUserList({ MOCK_USER })) + .WillOnce(ReturnWithUserList({})); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_EQ(recoveryManager.currentEvent_, nullptr); } @@ -1112,24 +1132,19 @@ HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest004, TestSize.Level0) HWTEST_F(CloudServiceImplTest, NetworkRecoveryTest005, TestSize.Level0) { ASSERT_NE(cloudServiceImpl_, nullptr); + EXPECT_CALL(*accountDelegateMock, IsVerified(_)).WillRepeatedly(Return(true)); EXPECT_CALL(*accountDelegateMock, IsLoginAccount()).WillOnce(Return(true)); - EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(Invoke([&](std::vector &users) -> bool { - users = { MOCK_USER }; - return true; - })); + EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).WillOnce(ReturnWithUserList({ MOCK_USER })); // 2 means that the QueryForegroundUsers interface will be called twice EXPECT_CALL(*accountDelegateMock, QueryForegroundUsers(_)) .Times(2) - .WillRepeatedly(Invoke([&](std::vector &users) -> bool { - users = { MOCK_USER }; - return true; - })); + .WillRepeatedly(ReturnWithUserList({ MOCK_USER })); CloudInfo cloudInfo; cloudInfo.user = MOCK_USER; MetaDataManager::GetInstance().DelMeta(cloudInfo.GetKey(), true); auto &recoveryManager = cloudServiceImpl_->syncManager_.networkRecoveryManager_; cloudServiceImpl_->Offline(DeviceManagerAdapter::CLOUD_DEVICE_UUID); - EXPECT_NE(recoveryManager.currentEvent_, nullptr); + ASSERT_NE(recoveryManager.currentEvent_, nullptr); recoveryManager.currentEvent_->disconnectTime -= std::chrono::hours(DISCONNECT_TIME); cloudServiceImpl_->OnReady(DeviceManagerAdapter::CLOUD_DEVICE_UUID); EXPECT_EQ(recoveryManager.currentEvent_, nullptr); -- Gitee