• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <thread>
16 
17 #include "TestRunner.h"
18 
19 #include <meta/api/timer.h>
20 #include <meta/interface/intf_object_registry.h>
21 #include <meta/interface/intf_task_queue.h>
22 #include <meta/interface/intf_task_queue_registry.h>
23 
24 #include "src/test_macros.h"
25 META_BEGIN_NAMESPACE()
26 
27 using namespace testing;
28 using namespace testing::ext;
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         TearDownTest();
39     }
SetUp()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 
TearDown()47     void TearDown() override
48     {
49         GetTaskQueueRegistry().UnregisterTaskQueue(queueId_.ToUid());
50     }
51 
52 protected:
53     ITaskQueue::Ptr queue_;
54     InstanceId queueId_;
55 };
56 
57 /**
58  * @tc.name: Ctor
59  * @tc.desc: test Ctor
60  * @tc.type:FUNC
61  * @tc.require:
62  */
63 HWTEST_F(TimerTest, Ctor, TestSize.Level1)
64 {
65     {
66         std::atomic<int> count = 0;
67         Timer t(
__anon8274578d0102null68             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(
__anon8274578d0202null75             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: Moving
83  * @tc.desc: test Moving
84  * @tc.type:FUNC
85  * @tc.require:
86  */
87 HWTEST_F(TimerTest, Moving, TestSize.Level1)
88 {
89     Timer t(
__anon8274578d0302null90         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: Start
99  * @tc.desc: test Start
100  * @tc.type:FUNC
101  * @tc.require:
102  */
103 HWTEST_F(TimerTest, Start, TestSize.Level1)
104 {
105     {
106         std::atomic<int> count = 0;
107         Timer t;
108         EXPECT_TRUE(t.Start(
__anon8274578d0402null109             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(
__anon8274578d0502null117             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: Stop
125  * @tc.desc: test Stop
126  * @tc.type:FUNC
127  * @tc.require:
128  */
129 HWTEST_F(TimerTest, Stop, TestSize.Level1)
130 {
131     {
132         std::atomic<int> count = 0;
133         Timer t;
134         EXPECT_TRUE(t.Start(
__anon8274578d0602null135             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(
__anon8274578d0702null148                 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: SingleShot
159  * @tc.desc: test SingleShot
160  * @tc.type:FUNC
161  * @tc.require:
162  */
163 HWTEST_F(TimerTest, SingleShot, TestSize.Level1)
164 {
165     {
166         std::atomic<int> count = 0;
167         Timer t;
168         EXPECT_TRUE(t.Start(
__anon8274578d0802null169             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(
__anon8274578d0902null179             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: Detach
188  * @tc.desc: test Detach
189  * @tc.type:FUNC
190  * @tc.require:
191  */
192 HWTEST_F(TimerTest, Detach, TestSize.Level1)
193 {
194     std::atomic<int> count = 0;
195     {
196         Timer t;
197         EXPECT_TRUE(t.Start(
__anon8274578d0a02null198             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: BadQueue
209  * @tc.desc: test BadQueue
210  * @tc.type:FUNC
211  * @tc.require:
212  */
213 HWTEST_F(TimerTest, BadQueue, TestSize.Level1)
214 {
215     {
216         Timer t(
__anon8274578d0b02null217             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(
__anon8274578d0c02null224             TimeSpan::Milliseconds(20), [] {}, Timer::RECURRING,
225             BASE_NS::Uid { "b63ceb45-bdb3-4f23-bd95-bb640eba9144" }));
226         EXPECT_TRUE(!t.IsRunning());
227     }
228     {
229         Timer t(
__anon8274578d0d02null230             TimeSpan::Milliseconds(20), [] {}, Timer::RECURRING, nullptr);
231         EXPECT_TRUE(!t.IsRunning());
232     }
233     {
234         Timer t;
235         EXPECT_FALSE(t.Start(
__anon8274578d0e02null236             TimeSpan::Milliseconds(20), [] {}, Timer::RECURRING, nullptr));
237         EXPECT_TRUE(!t.IsRunning());
238     }
239 }
240 
241 META_END_NAMESPACE()
242