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 <cmath>
19 #include <optional>
20 #include <thread>
21
22 #include "gtest/gtest.h"
23
24 #include "chre/platform/linux/task_util/task_manager.h"
25
26 namespace {
27
28 uint32_t gVarTaskManager = 0;
29 uint32_t gTask1Var = 0;
30 uint32_t gTask2Var = 0;
31
__anone6a6847e0202() 32 constexpr auto incrementGVar = []() { ++gVarTaskManager; };
__anone6a6847e0302() 33 constexpr auto task1Func = []() { ++gTask1Var; };
__anone6a6847e0402() 34 constexpr auto task2Func = []() { ++gTask2Var; };
35
TEST(TaskManager,FlushTasks)36 TEST(TaskManager, FlushTasks) {
37 chre::TaskManager taskManager;
38 for (uint32_t i = 0; i < 50; ++i) {
39 taskManager.flushTasks();
40 }
41 }
42
TEST(TaskManager,MultipleNonRepeatingTasks)43 TEST(TaskManager, MultipleNonRepeatingTasks) {
44 chre::TaskManager taskManager;
45 gVarTaskManager = 0;
46 constexpr uint32_t numTasks = 50;
47 for (uint32_t i = 0; i < numTasks; ++i) {
48 taskManager.addTask(incrementGVar, std::chrono::milliseconds(0));
49 std::this_thread::sleep_for(std::chrono::milliseconds(50));
50 }
51 taskManager.flushTasks();
52 EXPECT_TRUE(gVarTaskManager == numTasks);
53 }
54
TEST(TaskManager,MultipleTypesOfTasks)55 TEST(TaskManager, MultipleTypesOfTasks) {
56 chre::TaskManager taskManager;
57 gVarTaskManager = 0;
58 constexpr uint32_t numTasks = 50;
59 for (uint32_t i = 0; i < numTasks; ++i) {
60 taskManager.addTask(incrementGVar, std::chrono::milliseconds(0));
61 std::this_thread::sleep_for(std::chrono::milliseconds(50));
62 }
63 uint32_t millisecondsToRepeat = 100;
64 std::optional<uint32_t> taskId = taskManager.addTask(
65 incrementGVar, std::chrono::milliseconds(millisecondsToRepeat));
66 EXPECT_TRUE(taskId.has_value());
67 uint32_t taskRepeatTimesMax = 11;
68 std::this_thread::sleep_for(
69 std::chrono::milliseconds(millisecondsToRepeat * taskRepeatTimesMax));
70 EXPECT_TRUE(taskManager.cancelTask(taskId.value()));
71 taskManager.flushTasks();
72 EXPECT_TRUE(gVarTaskManager >= numTasks + taskRepeatTimesMax - 1);
73 }
74
TEST(TaskManager,FlushTasksWithoutCancel)75 TEST(TaskManager, FlushTasksWithoutCancel) {
76 chre::TaskManager taskManager;
77 gVarTaskManager = 0;
78 constexpr uint32_t numTasks = 50;
79 for (uint32_t i = 0; i < numTasks; ++i) {
80 taskManager.addTask(incrementGVar, std::chrono::milliseconds(0));
81 std::this_thread::sleep_for(std::chrono::milliseconds(50));
82 }
83 uint32_t millisecondsToRepeat = 100;
84 std::optional<uint32_t> taskId = taskManager.addTask(
85 incrementGVar, std::chrono::milliseconds(millisecondsToRepeat));
86 EXPECT_TRUE(taskId.has_value());
87 uint32_t taskRepeatTimesMax = 11;
88 std::this_thread::sleep_for(
89 std::chrono::milliseconds(millisecondsToRepeat * taskRepeatTimesMax));
90 taskManager.flushTasks();
91 EXPECT_TRUE(gVarTaskManager >= numTasks + taskRepeatTimesMax - 1);
92 }
93
94 } // namespace
95