diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 64ce9ff50e58b2740ec51683a83809a58d93b5d6..1f584184075ad4d204080f8b7be73374fb9e8b44 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -123,6 +123,15 @@ CloudServiceImpl::CloudServiceImpl() }, true); } +CloudServiceImpl::~CloudServiceImpl() +{ + executor_ = nullptr; + EventCenter::GetInstance().Unsubscribe(CloudEvent::GET_SCHEMA); + EventCenter::GetInstance().Unsubscribe(CloudEvent::CLOUD_SHARE); + EventCenter::GetInstance().Unsubscribe(CloudEvent::UPGRADE_SCHEMA); + MetaDataManager::GetInstance().Unsubscribe(Subscription::GetPrefix({ "" })); +} + int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) { XCollie xcollie( diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 4410519c14c0d09b7e8a50426a3e149b40db4be2..b78eea08f047cd52da12508b5641314b0f703ef5 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -41,7 +41,7 @@ public: using StoreMetaData = DistributedData::StoreMetaData; using StoreInfo = DistributedData::StoreInfo; CloudServiceImpl(); - ~CloudServiceImpl() = default; + ~CloudServiceImpl(); int32_t EnableCloud(const std::string &id, const std::map &switches) override; int32_t DisableCloud(const std::string &id) override; int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index 773fca44bac834540d494dc7bf73daf23538d448..16341a989a0c8f467d28ad561f352600a5fb894e 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -225,6 +225,9 @@ SyncManager::SyncManager() SyncManager::~SyncManager() { + EventCenter::GetInstance().Unsubscribe(CloudEvent::LOCK_CLOUD_CONTAINER); + EventCenter::GetInstance().Unsubscribe(CloudEvent::UNLOCK_CLOUD_CONTAINER); + EventCenter::GetInstance().Unsubscribe(CloudEvent::LOCAL_CHANGE); if (executor_ != nullptr) { actives_.ForEachCopies([this](auto &syncId, auto &taskId) { executor_->Remove(taskId); diff --git a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp index dbe572a55aab8c2a40429bbf4b223fd8c511d082..10c8becc51f858433512ddfae92ad8c3db9bfd48 100644 --- a/services/distributeddataservice/service/test/cloud_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/cloud_service_impl_test.cpp @@ -104,6 +104,38 @@ void CloudServiceImplTest::SetUp() { } void CloudServiceImplTest::TearDown() { } +/** + * @tc.name: CloudServiceImpl_Deconstruct_Test + * @tc.desc: Test CloudServiceImpl Deconstruct functions. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(CloudServiceImplTest, CloudServiceImpl_Deconstruct_Test, TestSize.Level0) +{ + ZLOGI("CloudServiceImplTest CloudServiceImpl_Deconstruct_Test start"); + std::shared_ptr dbStoreMock = std::make_shared(); + MetaDataManager::GetInstance().Initialize(dbStoreMock, nullptr, ""); + // LOCAL_CHANGE event subscribed when construct CloudServiceImpl. + auto cloudServiceImpl = std::make_shared(); + ASSERT_NE(cloudServiceImpl, nullptr); + auto [found, observers] = EventCenter::GetInstance().observers_.Find(CloudEvent::LOCAL_CHANGE); + EXPECT_EQ(found, true); + EXPECT_NE(observers.size(), 0); + auto [isExist, metaObserver] = MetaDataManager::GetInstance().metaObservers_.Find(Subscription::GetPrefix({ "" })); + EXPECT_EQ(isExist, true); + EXPECT_NE(metaObserver, nullptr); + + cloudServiceImpl = nullptr; + std::tie(found, observers) = EventCenter::GetInstance().observers_.Find(CloudEvent::LOCAL_CHANGE); + EXPECT_EQ(found, false); + EXPECT_EQ(observers.size(), 0); + std::tie(isExist, metaObserver) = + MetaDataManager::GetInstance().metaObservers_.Find(Subscription::GetPrefix({ "" })); + EXPECT_EQ(isExist, false); + EXPECT_EQ(metaObserver, nullptr); + ZLOGI("CloudServiceImplTest CloudServiceImpl_Deconstruct_Test end"); +} + /** * @tc.name: EnableCloud001 * @tc.desc: Test EnableCloud functions with user is invalid.