• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 <gtest/gtest.h>
17 
18 #include "ffrt_utils.h"
19 
20 namespace OHOS::MiscServices {
21 using namespace testing::ext;
22 using namespace testing;
23 
24 class FFRTTimerTest : public testing::Test {
25 public:
26     static void SetUpTestCase(void);
27     static void TearDownTestCase(void);
28     void SetUp();
29     void TearDown();
30 };
31 
SetUpTestCase(void)32 void FFRTTimerTest::SetUpTestCase(void) { }
33 
TearDownTestCase(void)34 void FFRTTimerTest::TearDownTestCase(void) { }
35 
SetUp(void)36 void FFRTTimerTest::SetUp(void) { }
37 
TearDown(void)38 void FFRTTimerTest::TearDown(void) { }
39 
40 /**
41  * @tc.name: SetTimerTest001
42  * @tc.desc: Set Timer
43  * @tc.type: FUNC
44  */
45 HWTEST_F(FFRTTimerTest, SetTimerTest001, TestSize.Level0)
46 {
47     FFRTTimer ffrtTimer;
48     std::string timerId = "ffrt_test";
49     int x = 4;
__anon16b2440a0102null50     FFRTTask ffrtTask = [&x] {
51         x /= 2;
52     };
53     ffrtTimer.SetTimer(timerId, ffrtTask, 0);
54     ffrt::wait();
55     EXPECT_TRUE(x == 2);
56 }
57 
58 /**
59  * @tc.name: SetTimerTest002
60  * @tc.desc: Set Timer
61  * @tc.type: FUNC
62  */
63 HWTEST_F(FFRTTimerTest, SetTimerTest002, TestSize.Level0)
64 {
65     FFRTTimer pbFfrtTimer("paste_ffrt_timer");
66     std::string pbTimerId = "paste_ffrt_test1";
67     int y = 8;
__anon16b2440a0202null68     FFRTTask pbFfrtTask = [&y] {
69         y /= 2;
70     };
71     pbFfrtTimer.SetTimer(pbTimerId, pbFfrtTask, 5);
72     ffrt::wait();
73     uint32_t taskId = pbFfrtTimer.GetTaskId(pbTimerId);
74     EXPECT_TRUE(taskId == 1);
75 }
76 
77 /**
78  * @tc.name: SubmitQueueTasksTest
79  * @tc.desc: Submit Queue Tasks
80  * @tc.type: FUNC
81  */
82 HWTEST_F(FFRTTimerTest, SubmitQueueTasksTest, TestSize.Level0)
83 {
84     std::vector<FFRTTask> tasks;
85     FFRTQueue que("QueueTasks");
86     std::vector<FFRTTask>().swap(tasks);
87     FFRTUtils::SubmitQueueTasks(tasks, que);
88     int x = 2;
89     int z = 19;
__anon16b2440a0302null90     FFRTTask task1 = [&x] {
91         x <<= 3;
92     };
__anon16b2440a0402null93     FFRTTask task2 = [&z] {
94         z /= 2;
95     };
96     tasks.push_back(task1);
97     tasks.push_back(task2);
98     FFRTUtils::SubmitQueueTasks(tasks, que);
99     EXPECT_TRUE(true);
100 }
101 
102 /**
103  * @tc.name: SubmitDelayTaskTest
104  * @tc.desc: Submit Delay Task
105  * @tc.type: FUNC
106  */
107 HWTEST_F(FFRTTimerTest, SubmitDelayTaskTest, TestSize.Level0)
108 {
109     std::shared_ptr<FFRTQueue> queu = std::make_shared<FFRTQueue>("delayTask");
110     uint32_t delayMs = 20;
111     int x = 10;
__anon16b2440a0502null112     FFRTTask task0 = [&x] {
113         x <<= 3;
114     };
115     FFRTHandle handle = FFRTUtils::SubmitDelayTask(task0, delayMs, queu);
116     FFRTUtils::CancelTask(handle, queu);
117     EXPECT_TRUE(true);
118 }
119 
120 /**
121  * @tc.name: SubmitTimeoutTaskTest
122  * @tc.desc: Submit Timeout Task
123  * @tc.type: FUNC
124  */
125 HWTEST_F(FFRTTimerTest, SubmitTimeoutTaskTest, TestSize.Level0)
126 {
127     uint32_t timeoutMs = 5;
128     int x = 9;
__anon16b2440a0602null129     FFRTTask task = [&x] {
130         x <<= 3;
131     };
132     bool res = FFRTUtils::SubmitTimeoutTask(task, timeoutMs);
133     EXPECT_TRUE(res);
134 }
135 } // namespace OHOS::MiscServices