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 #include "delay_actuator.h" 17 #include "block_data.h" 18 19 #include <gtest/gtest.h> 20 using namespace testing::ext; 21 namespace OHOS::Test { 22 class DelayActuatorTest : public testing::Test { 23 public: SetUpTestCase(void)24 static void SetUpTestCase(void){}; TearDownTestCase(void)25 static void TearDownTestCase(void){}; SetUp()26 void SetUp(){}; TearDown()27 void TearDown(){}; 28 }; 29 30 /** 31 * @tc.name: Execute_001 32 * @tc.desc: Execute normally 33 * @tc.type: FUNC 34 * @tc.require: 35 * @tc.author: ht 36 */ 37 HWTEST_F(DelayActuatorTest, Execute_001, TestSize.Level0) 38 { 39 auto delayActuator = std::make_shared<DelayActuator<int>>(nullptr, ActuatorBase::DEFAULT_MIN_EXECUTE_INTERVAL); 40 delayActuator->SetExecutorPool(std::make_shared<ExecutorPool>(1, 1)); 41 auto blockData = std::make_shared<BlockData<int>>(2, 0); __anonb5664fa50102(int data) 42 delayActuator->SetTask([blockData](int data) { 43 blockData->SetValue(data); 44 return 0; 45 }); 46 delayActuator->Execute(1); 47 EXPECT_EQ(blockData->GetValue(), 1); 48 } 49 50 /** 51 * @tc.name: Execute_002 52 * @tc.desc: When triggered once, the Task is executed normally with delayInterval_ delay 53 * @tc.type: FUNC 54 * @tc.require: 55 * @tc.author: ht 56 */ 57 HWTEST_F(DelayActuatorTest, Execute_002, TestSize.Level0) 58 { 59 uint32_t firstDelay = 1500; 60 uint32_t minInterval = 2000; 61 uint32_t maxInterval = 3000; 62 auto delayActuator = std::make_shared<DelayActuator<int>>(nullptr, firstDelay, minInterval, maxInterval); 63 delayActuator->SetExecutorPool(std::make_shared<ExecutorPool>(1, 1)); 64 auto blockData = std::make_shared<BlockData<int>>(1, 0); __anonb5664fa50202(int data) 65 delayActuator->SetTask([blockData](int data) { 66 blockData->SetValue(data); 67 return 0; 68 }); 69 delayActuator->Execute(1); 70 EXPECT_EQ(blockData->GetValue(), 0); 71 EXPECT_EQ(blockData->GetValue(), 1); 72 } 73 74 /** 75 * @tc.name: Execute_003 76 * @tc.desc: When triggered multiple times, the Task is executed normally with forceInterval_ delay 77 * @tc.type: FUNC 78 * @tc.require: 79 * @tc.author: ht 80 */ 81 HWTEST_F(DelayActuatorTest, Execute_003, TestSize.Level0) 82 { 83 uint32_t firstDelay = 200; 84 uint32_t minInterval = 200; 85 uint32_t maxInterval = 600; 86 auto delayActuator = std::make_shared<DelayActuator<int>>(nullptr, firstDelay, minInterval, maxInterval); 87 delayActuator->SetExecutorPool(std::make_shared<ExecutorPool>(1, 1)); 88 auto blockData = std::make_shared<BlockData<int>>(0, 0); __anonb5664fa50302(int data) 89 delayActuator->SetTask([blockData](int data) { 90 blockData->SetValue(data); 91 return 0; 92 }); 93 for (int i = 0; i < 5; i++) { 94 delayActuator->Execute(std::move(i)); 95 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 96 EXPECT_EQ(blockData->GetValue(), 0); 97 } 98 std::this_thread::sleep_for(std::chrono::milliseconds(200)); 99 EXPECT_EQ(blockData->GetValue(), 4); 100 } 101 102 /** 103 * @tc.name: Execute_004 104 * @tc.desc: Execute task after Defer deconstruction 105 * @tc.type: FUNC 106 * @tc.require: 107 * @tc.author: ht 108 */ 109 HWTEST_F(DelayActuatorTest, Execute_004, TestSize.Level0) 110 { 111 auto delayActuator = std::make_shared<DelayActuator<int>>(nullptr, ActuatorBase::DEFAULT_MIN_EXECUTE_INTERVAL); 112 delayActuator->SetExecutorPool(std::make_shared<ExecutorPool>(1, 1)); 113 auto blockData = std::make_shared<BlockData<int>>(1, 0); __anonb5664fa50402(int data) 114 delayActuator->SetTask([blockData](int data) { 115 blockData->SetValue(data); 116 return 0; 117 }); 118 { 119 ActuatorBase::Defer defer(delayActuator); 120 for (int i = 0; i < 3; i++) { 121 delayActuator->Execute(std::move(i)); 122 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 123 EXPECT_EQ(blockData->GetValue(), 0); 124 } 125 } 126 EXPECT_EQ(blockData->GetValue(), 2); 127 } 128 129 /** 130 * @tc.name: Execute_005 131 * @tc.desc: When triggered multiple times, use mergeFunc to merge parameters 132 * @tc.type: FUNC 133 * @tc.require: 134 * @tc.author: ht 135 */ 136 HWTEST_F(DelayActuatorTest, Execute_005, TestSize.Level0) 137 { 138 auto delayActuator = std::make_shared<DelayActuator<int>>( __anonb5664fa50502(int& out, int&& input) 139 [](int& out, int&& input) { 140 out += input; 141 }, 142 100, 100, 300); 143 delayActuator->SetExecutorPool(std::make_shared<ExecutorPool>(1, 1)); 144 auto blockData = std::make_shared<BlockData<int>>(0, 0); __anonb5664fa50602(int data) 145 delayActuator->SetTask([blockData](int data) { 146 blockData->SetValue(data); 147 return 0; 148 }); 149 for (int i = 0; i < 5; i++) { 150 delayActuator->Execute(std::move(i)); 151 EXPECT_EQ(blockData->GetValue(), 0); 152 } 153 std::this_thread::sleep_for(std::chrono::milliseconds(300)); 154 EXPECT_EQ(blockData->GetValue(), 10); 155 } 156 157 /** 158 * @tc.name: Execute_006 159 * @tc.desc: Testing MergeFunc for complex types 160 * @tc.type: FUNC 161 * @tc.require: 162 * @tc.author: ht 163 */ 164 HWTEST_F(DelayActuatorTest, Execute_006, TestSize.Level0) 165 { 166 using DelayActuatorT = DelayActuator<std::map<std::string, std::string>>; 167 auto delayActuator = std::make_shared<DelayActuatorT>( __anonb5664fa50702(auto& out, std::map<std::string, std::string>&& input) 168 [](auto& out, std::map<std::string, std::string>&& input) { 169 for (auto& [k, v] : input) { 170 out[k] += std::move(v); 171 } 172 }, 173 100); 174 delayActuator->SetExecutorPool(std::make_shared<ExecutorPool>(1, 1)); 175 auto blockData = std::make_shared<BlockData<std::map<std::string, std::string>>>(3); __anonb5664fa50802(const std::map<std::string, std::string>& data) 176 delayActuator->SetTask([blockData](const std::map<std::string, std::string>& data) { 177 blockData->SetValue(data); 178 return 0; 179 }); 180 std::map<std::string, std::string> data; 181 int len = 10; 182 for (int i = 0; i < len; i++) { 183 data.insert_or_assign(std::to_string(i), "t"); 184 delayActuator->Execute(std::move(data)); 185 } 186 auto val = blockData->GetValue(); 187 EXPECT_EQ(val.size(), len); 188 for (auto& [k, v] : val) { 189 EXPECT_EQ(v, std::string(len--, 't')); 190 } 191 } 192 193 /** 194 * @tc.name: Execute_007 195 * @tc.desc: Testing delay of first execution and minimum interval 196 * @tc.type: FUNC 197 * @tc.require: 198 * @tc.author: ht 199 */ 200 HWTEST_F(DelayActuatorTest, Execute_007, TestSize.Level0) 201 { 202 uint32_t firstDelay = 50; 203 uint32_t minInterval = 1500; 204 uint32_t maxInterval = 5000; 205 auto delayActuator = std::make_shared<DelayActuator<int>>(nullptr, firstDelay, minInterval, maxInterval); 206 delayActuator->SetExecutorPool(std::make_shared<ExecutorPool>(1, 1)); 207 auto blockData = std::make_shared<BlockData<int>>(1, 0); __anonb5664fa50902(int data) 208 delayActuator->SetTask([blockData](int data) { 209 blockData->SetValue(data); 210 return 0; 211 }); 212 delayActuator->Execute(1); 213 EXPECT_EQ(blockData->GetValue(), 1); 214 blockData->Clear(0); 215 delayActuator->Execute(2); 216 EXPECT_EQ(blockData->GetValue(), 0); 217 blockData->Clear(0); 218 EXPECT_EQ(blockData->GetValue(), 2); 219 } 220 } // namespace OHOS::Test 221