1 /*
2 * Copyright (C) 2024 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 <thread>
17
18 #include <meta/api/timer.h>
19 #include <meta/interface/intf_object_registry.h>
20 #include <meta/interface/intf_task_queue.h>
21 #include <meta/interface/intf_task_queue_registry.h>
22
23 #include "src/test_runner.h"
24 #include "src/test_macros.h"
25
26 using namespace testing::ext;
27
28 META_BEGIN_NAMESPACE()
29
30 class TimerTest : public testing::Test {
31 public:
SetUpTestSuite()32 static void SetUpTestSuite()
33 {
34 SetTest();
35 }
TearDownTestSuite()36 static void TearDownTestSuite()
37 {
38 ResetTest();
39 }
40 void SetUp() override
41 {
42 queue_ = GetObjectRegistry().Create<IThreadedTaskQueue>(ClassId::ThreadedTaskQueue);
43 queueId_ = interface_cast<IObjectInstance>(queue_)->GetInstanceId();
44 GetTaskQueueRegistry().RegisterTaskQueue(queue_, queueId_.ToUid());
45 }
46 void TearDown() override
47 {
48 GetTaskQueueRegistry().UnregisterTaskQueue(queueId_.ToUid());
49 }
50
51 protected:
52 ITaskQueue::Ptr queue_;
53 InstanceId queueId_;
54 };
55
56
57 /**
58 * @tc.name: TimerTest
59 * @tc.desc: test Ctor function
60 * @tc.type: FUNC
61 * @tc.require: I7DMS1
62 */
HWTEST_F(TimerTest, Ctor, TestSize.Level1)63 HWTEST_F(TimerTest, Ctor, TestSize.Level1)
64 {
65 {
66 std::atomic<int> count = 0;
67 Timer t (
68 TimeSpan::Milliseconds(100), [&count] { ++count; }, Timer::RECURRING, queueId_.ToUid());
69 EXPECT_TRUE(t.IsRunning());
70 EXPECT_EQ_TIMED(350, count, 3);
71 }
72 {
73 std::atomic<int> count = 0;
74 Timer t (
75 TimeSpan::Milliseconds(100), [&count] { ++count; }, Timer::RECURRING, queue_);
76 EXPECT_TRUE(t.IsRunning());
77 EXPECT_EQ_TIMED(350, count, 3);
78 }
79 }
80
81 /**
82 * @tc.name: TimerTest
83 * @tc.desc: test Moving function
84 * @tc.type: FUNC
85 * @tc.require: I7DMS1
86 */
HWTEST_F(TimerTest, Moving, TestSize.Level1)87 HWTEST_F(TimerTest, Moving, TestSize.Level1)
88 {
89 Timer t (
90 TimeSpan::Milliseconds(20), [] {}, Timer::RECURRING, queueId_.ToUid());
91 EXPECT_TRUE(t.IsRunning());
92 Timer t2 = BASE_NS::move(t);
93 EXPECT_FALSE(t.IsRunning());
94 EXPECT_TRUE(t2.IsRunning());
95 }
96
97 /**
98 * @tc.name: TimerTest
99 * @tc.desc: test Start function
100 * @tc.type: FUNC
101 * @tc.require: I7DMS1
102 */
HWTEST_F(TimerTest, Start, TestSize.Level1)103 HWTEST_F(TimerTest, Start, TestSize.Level1)
104 {
105 {
106 std::atomic<int> count = 0;
107 Timer t;
108 EXPECT_TRUE(t.Start(
109 TimeSpan::Milliseconds(100), [&count] { ++count; }, Timer::RECURRING, queueId_.ToUid()));
110 EXPECT_TRUE(t.IsRunning());
111 EXPECT_EQ_TIMED(350, count, 3);
112 }
113 {
114 std::atomic<int> count = 0;
115 Timer t;
116 EXPECT_TRUE(t.Start(
117 TimeSpan::Milliseconds(100), [&count] { ++count; }, Timer::RECURRING, queue_));
118 EXPECT_TRUE(t.IsRunning());
119 EXPECT_EQ_TIMED(350, count, 3);
120 }
121 }
122
123 /**
124 * @tc.name: TimerTest
125 * @tc.desc: test Stop function
126 * @tc.type: FUNC
127 * @tc.require: I7DMS1
128 */
HWTEST_F(TimerTest, Stop, TestSize.Level1)129 HWTEST_F(TimerTest, Stop, TestSize.Level1)
130 {
131 {
132 std::atomic<int> count = 0;
133 Timer t;
134 EXPECT_TRUE(t.Start(
135 TimeSpan::Milliseconds(100), [&count] { ++count; }, Timer::RECURRING, queueId_.ToUid()));
136 EXPECT_TRUE(t.IsRunning());
137 EXPECT_EQ_TIMED(150, count, 1);
138 t.Stop();
139 META_WAIT_TIMED(150);
140 EXPECT_EQ(count, 1);
141 EXPECT_FALSE(t.IsRunning());
142 }
143 {
144 std::atomic<int> count = 0;
145 {
146 Timer t;
147 EXPECT_TRUE(t.Start(
148 TimeSpan::Milliseconds(100), [&count] { ++count; }, Timer::RECURRING, queueId_.ToUid()));
149 EXPECT_TRUE(t.IsRunning());
150 EXPECT_EQ_TIMED(150, count, 1);
151 }
152 META_WAIT_TIMED(150);
153 EXPECT_EQ(count, 1);
154 }
155 }
156
157 /**
158 * @tc.name: TimerTest
159 * @tc.desc: test SingleShot function
160 * @tc.type: FUNC
161 * @tc.require: I7DMS1
162 */
HWTEST_F(TimerTest, SingleShot, TestSize.Level1)163 HWTEST_F(TimerTest, SingleShot, TestSize.Level1)
164 {
165 {
166 std::atomic<int> count = 0;
167 Timer t;
168 EXPECT_TRUE(t.Start(
169 TimeSpan::Milliseconds(100), [&count] { ++count; }, Timer::SINGLE_SHOT, queueId_.ToUid()));
170 EXPECT_TRUE(t.IsRunning());
171 EXPECT_EQ_TIMED(150, count, 1);
172 META_WAIT_TIMED(150);
173 EXPECT_EQ(count, 1);
174 EXPECT_FALSE(t.IsRunning());
175 }
176 {
177 std::atomic<int> count = 0;
178 SingleShotTimer(
179 TimeSpan::Milliseconds(100), [&count] { ++count; }, queueId_.ToUid());
180 EXPECT_EQ_TIMED(150, count, 1);
181 META_WAIT_TIMED(150);
182 EXPECT_EQ(count, 1);
183 }
184 }
185
186 /**
187 * @tc.name: TimerTest
188 * @tc.desc: test Detach function
189 * @tc.type: FUNC
190 * @tc.require: I7DMS1
191 */
HWTEST_F(TimerTest, Detach, TestSize.Level1)192 HWTEST_F(TimerTest, Detach, TestSize.Level1)
193 {
194 std::atomic<int> count = 0;
195 {
196 Timer t;
197 EXPECT_TRUE(t.Start(
198 TimeSpan::Milliseconds(100), [&count] { ++count; }, Timer::SINGLE_SHOT, queueId_.ToUid()));
199 t.Detach();
200 EXPECT_FALSE(t.IsRunning());
201 }
202 EXPECT_EQ_TIMED(150, count, 1);
203 META_WAIT_TIMED(150);
204 EXPECT_EQ(count, 1);
205 }
206
207 /**
208 * @tc.name: TimerTest
209 * @tc.desc: test BadQueue function
210 * @tc.type: FUNC
211 * @tc.require: I7DMS1
212 */
HWTEST_F(TimerTest, BadQueue, TestSize.Level1)213 HWTEST_F(TimerTest, BadQueue, TestSize.Level1)
214 {
215 {
216 Timer t(
217 TimeSpan::Milliseconds(20), [] {}, Timer::RECURRING,
218 BASE_NS::Uid { "b63ceb45-bdb3-4f23-bd95-bb640eba9144" });
219 EXPECT_TRUE(!t.IsRunning());
220 }
221 {
222 Timer t;
223 EXPECT_FALSE(t.Start(
224 TimeSpan::Milliseconds(20), [] {}, Timer::RECURRING,
225 BASE_NS::Uid { "b63ceb45-bdb3-4f23-bd95-bb640eba9144" }));
226 EXPECT_TRUE(!t.IsRunning());
227 }
228 {
229 Timer t(
230 TimeSpan::Milliseconds(20), [] {}, Timer::RECURRING, nullptr);
231 EXPECT_TRUE(!t.IsRunning());
232 }
233 {
234 Timer t;
235 EXPECT_FALSE(t.Start(
236 TimeSpan::Milliseconds(20), [] {}, Timer::RECURRING, nullptr));
237 EXPECT_TRUE(!t.IsRunning());
238 }
239 }
240
241 META_END_NAMESPACE()