xref: /aosp_15_r20/system/chre/platform/linux/tests/task_test.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <chrono>
18 #include <thread>
19 
20 #include "gtest/gtest.h"
21 
22 #include "chre/platform/linux/task_util/task.h"
23 
24 namespace {
25 
26 using chre::task_manager_internal::Task;
27 
28 uint32_t gVarTask = 0;
29 
__anon778f0f550202() 30 constexpr auto incrementGVar = []() { ++gVarTask; };
31 
TEST(Task,Execute)32 TEST(Task, Execute) {
33   gVarTask = 0;
34   std::chrono::milliseconds waitTime(100);
35   Task task(incrementGVar, waitTime, 0);
36   EXPECT_FALSE(task.isReadyToExecute());
37   std::this_thread::sleep_for(waitTime);
38   EXPECT_TRUE(task.isReadyToExecute());
39   task.execute();
40   EXPECT_TRUE(gVarTask == 1);
41   EXPECT_TRUE(task.isRepeating());
42   EXPECT_FALSE(task.isReadyToExecute());
43   auto timeDiff =
44       std::chrono::steady_clock::now() - task.getExecutionTimestamp();
45   EXPECT_TRUE(
46       std::chrono::duration_cast<std::chrono::nanoseconds>(timeDiff).count() <=
47       waitTime.count());
48   task.cancel();
49   EXPECT_FALSE(task.isRepeating());
50 }
51 
TEST(Task,ExecuteNoRepeat)52 TEST(Task, ExecuteNoRepeat) {
53   gVarTask = 0;
54   std::chrono::nanoseconds waitTime(0);
55   Task task(incrementGVar, waitTime, 0);
56   EXPECT_TRUE(task.isReadyToExecute());
57   task.execute();
58   EXPECT_TRUE(gVarTask == 1);
59   EXPECT_TRUE(task.isReadyToExecute());
60   EXPECT_FALSE(task.isRepeating());
61 }
62 
TEST(Task,ComparisonOperators)63 TEST(Task, ComparisonOperators) {
64   constexpr uint32_t numTasks = 6;
65   Task tasks[numTasks] = {Task(incrementGVar, std::chrono::nanoseconds(0), 0),
66                           Task(incrementGVar, std::chrono::nanoseconds(10), 1),
67                           Task(incrementGVar, std::chrono::nanoseconds(20), 2),
68                           Task(incrementGVar, std::chrono::nanoseconds(30), 3),
69                           Task(incrementGVar, std::chrono::nanoseconds(40), 4),
70                           Task(incrementGVar, std::chrono::nanoseconds(50), 5)};
71 
72   for (uint32_t i = 0; i < numTasks; ++i) {
73     if (i < numTasks - 1) {
74       EXPECT_TRUE(tasks[i] < tasks[i + 1]);
75       EXPECT_TRUE(tasks[i] <= tasks[i + 1]);
76       EXPECT_FALSE(tasks[i] > tasks[i + 1]);
77       EXPECT_FALSE(tasks[i] >= tasks[i + 1]);
78     }
79   }
80 }
81 
82 }  // namespace
83