1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define LOG_TAG "DelayNotifyTest"
17 #include <gtest/gtest.h>
18
19 #include "logger.h"
20 #include "common.h"
21 #include "executor_pool.h"
22 #include "rdb_helper.h"
23 #include "rdb_store_impl.h"
24 #include "delay_notify.h"
25 #include "block_data.h"
26
27 using namespace testing::ext;
28 using namespace OHOS::NativeRdb;
29 using namespace OHOS::Rdb;
30 using namespace OHOS::DistributedRdb;
31 class DelayNotifyTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 };
38
SetUpTestCase(void)39 void DelayNotifyTest::SetUpTestCase(void)
40 {
41 }
42
TearDownTestCase(void)43 void DelayNotifyTest::TearDownTestCase(void)
44 {
45 }
46
SetUp()47 void DelayNotifyTest::SetUp()
48 {
49 }
50
TearDown()51 void DelayNotifyTest::TearDown()
52 {
53 }
54
55 /**
56 * @tc.name: StartTimer_Test_001
57 * @tc.desc: Check the normal process of starting the timer.
58 * @tc.type: FUNC
59 */
60 HWTEST_F(DelayNotifyTest, StartTimer_Test_001, TestSize.Level1)
61 {
62 auto delayNotifier = std::make_shared<DelayNotify>();
63 delayNotifier->SetExecutorPool(std::make_shared<OHOS::ExecutorPool>(5, 0));
64 auto block = std::make_shared<OHOS::BlockData<bool>>(1, false);
__anon3952c87b0102(const RdbChangedData &, const RdbNotifyConfig &)65 delayNotifier->SetTask([block](const RdbChangedData &, const RdbNotifyConfig &){
66 block->SetValue(true);
67 return 0;
68 });
69 delayNotifier->isFull_ = true;
70 delayNotifier->UpdateNotify(RdbChangedData());
71 EXPECT_TRUE(block->GetValue());
72 }
73
74 /**
75 * @tc.name: StartTimer_Test_002
76 * @tc.desc: Check if the function ExecuteTask() can avoid crashing when the delayNotify is destructed prematurely
77 * in the main process of starting the timer.
78 * @tc.type: FUNC
79 */
80 HWTEST_F(DelayNotifyTest, StartTimer_Test_002, TestSize.Level1)
81 {
82 auto delayNotifier = std::make_shared<DelayNotify>();
83 auto pool = std::make_shared<OHOS::ExecutorPool>(5, 0);
84 delayNotifier->SetExecutorPool(pool);
85 auto block = std::make_shared<OHOS::BlockData<bool>>(1, false);
__anon3952c87b0202(const RdbChangedData &, const RdbNotifyConfig &)86 delayNotifier->SetTask([block](const RdbChangedData &, const RdbNotifyConfig &){
87 block->SetValue(true);
88 return 0;
89 });
90 delayNotifier->UpdateNotify(RdbChangedData());
91 delayNotifier->delaySyncTaskId_ = OHOS::ExecutorPool::INVALID_TASK_ID;
92 delayNotifier.reset();
93 ASSERT_NO_FATAL_FAILURE(EXPECT_FALSE(block->GetValue()));
94 }