1 /*
2 * Copyright (C) 2023 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 <gtest/gtest.h>
18
19 #include "aidl/BackgroundWorker.h"
20
21 namespace aidl {
22 namespace google {
23 namespace hardware {
24 namespace power {
25 namespace impl {
26 namespace pixel {
27
28 using std::literals::chrono_literals::operator""s;
29 using std::literals::chrono_literals::operator""ms;
30 using std::literals::chrono_literals::operator""ns;
31
32 constexpr double kTIMING_TOLERANCE_MS = std::chrono::milliseconds(25).count();
33
34 // Use this work to package some work identifier val along with time t of when it was
35 // processed to validate that the waiting time is adhered to as closely as possible
36 struct work {
37 int64_t val{0};
38 std::chrono::steady_clock::time_point t;
39 };
40
getDurationMs(std::chrono::steady_clock::time_point endTime,std::chrono::steady_clock::time_point startTime)41 auto getDurationMs(std::chrono::steady_clock::time_point endTime,
42 std::chrono::steady_clock::time_point startTime) {
43 return std::chrono::duration<double, std::milli>(endTime - startTime);
44 }
45
TEST(PriorityQueueWorkerPool,testSingle)46 TEST(PriorityQueueWorkerPool, testSingle) {
47 const int pqId = 1;
48 std::condition_variable cv;
49 std::mutex m;
50 std::vector<work> vec;
51 vec.reserve(3);
52
53 auto p = std::make_shared<PriorityQueueWorkerPool>(1, "adpf_");
54 p->addCallback(pqId, [&](int64_t packageId) {
55 std::lock_guard<std::mutex> lock(m);
56 vec.push_back({packageId, std::chrono::steady_clock::now()});
57 cv.notify_all();
58 });
59
60 const auto tNow = std::chrono::steady_clock::now();
61 p->schedule(pqId, 500, tNow + 500ms);
62 p->schedule(pqId, 100, tNow + 100ms);
63 p->schedule(pqId, 300, tNow + 300ms);
64
65 std::unique_lock<std::mutex> lock(m);
66 EXPECT_EQ(0, vec.size());
67 cv.wait_for(lock, 1500ms, [&]() { return vec.size() == 3; });
68
69 EXPECT_EQ(3, vec.size());
70 EXPECT_EQ(100, vec[0].val);
71 EXPECT_NEAR(100, getDurationMs(vec[0].t, tNow).count(), kTIMING_TOLERANCE_MS);
72 EXPECT_EQ(300, vec[1].val);
73 EXPECT_NEAR(300, getDurationMs(vec[1].t, tNow).count(), kTIMING_TOLERANCE_MS);
74 EXPECT_EQ(500, vec[2].val);
75 EXPECT_NEAR(500, getDurationMs(vec[2].t, tNow).count(), kTIMING_TOLERANCE_MS);
76 }
77
TEST(TemplatePriorityQueueWorker,testSingle)78 TEST(TemplatePriorityQueueWorker, testSingle) {
79 std::condition_variable cv;
80 std::mutex m;
81 std::vector<work> vec;
82 vec.reserve(3);
83
84 auto p = std::make_shared<PriorityQueueWorkerPool>(1, "adpf_");
85 TemplatePriorityQueueWorker<int> worker{
86 [&](int i) {
87 std::lock_guard<std::mutex> lock(m);
88 vec.push_back({i, std::chrono::steady_clock::now()});
89 cv.notify_all();
90 },
91 p};
92
93 // Would be nice to have a pause option for testing
94 const auto tNow = std::chrono::steady_clock::now();
95 worker.schedule(303, tNow + 500ms);
96 worker.schedule(101, tNow + 100ms);
97 worker.schedule(202, tNow + 300ms);
98
99 std::unique_lock<std::mutex> lock(m);
100 EXPECT_EQ(0, vec.size());
101 cv.wait_for(lock, 1500ms, [&]() { return vec.size() == 3; });
102
103 EXPECT_EQ(3, vec.size());
104 EXPECT_EQ(101, vec[0].val);
105 EXPECT_NEAR(100, getDurationMs(vec[0].t, tNow).count(), kTIMING_TOLERANCE_MS);
106 EXPECT_EQ(202, vec[1].val);
107 EXPECT_NEAR(300, getDurationMs(vec[1].t, tNow).count(), kTIMING_TOLERANCE_MS);
108 EXPECT_EQ(303, vec[2].val);
109 EXPECT_NEAR(500, getDurationMs(vec[2].t, tNow).count(), kTIMING_TOLERANCE_MS);
110 }
111
TEST(TemplatePriorityQueueWorker,testDouble)112 TEST(TemplatePriorityQueueWorker, testDouble) {
113 std::condition_variable cv;
114 std::mutex m;
115 std::vector<work> vec;
116 vec.reserve(6);
117
118 auto p = std::make_shared<PriorityQueueWorkerPool>(1, "adpf_");
119 TemplatePriorityQueueWorker<int> worker1{
120 [&](int i) {
121 std::lock_guard<std::mutex> lock(m);
122 vec.push_back({i, std::chrono::steady_clock::now()});
123 cv.notify_all();
124 },
125 p};
126
127 TemplatePriorityQueueWorker<std::string> worker2{
128 [&](const std::string &s) {
129 std::lock_guard<std::mutex> lock(m);
130 vec.push_back({atoi(s.c_str()), std::chrono::steady_clock::now()});
131 cv.notify_all();
132 },
133 p};
134
135 // Would be nice to have a pause option for testing
136 const auto tNow = std::chrono::steady_clock::now();
137 worker1.schedule(5, tNow + 300ms);
138 worker1.schedule(1, tNow + 100ms);
139 worker1.schedule(3, tNow + 200ms);
140 worker2.schedule("2", tNow + 150ms);
141 worker2.schedule("4", tNow + 250ms);
142 worker2.schedule("6", tNow + 350ms);
143
144 std::unique_lock<std::mutex> lock(m);
145 EXPECT_EQ(0, vec.size());
146 cv.wait_for(lock, 1500ms, [&]() { return vec.size() == 6; });
147
148 EXPECT_EQ(6, vec.size());
149 EXPECT_EQ(1, vec[0].val);
150 EXPECT_NEAR(100, getDurationMs(vec[0].t, tNow).count(), kTIMING_TOLERANCE_MS);
151 EXPECT_EQ(2, vec[1].val);
152 EXPECT_NEAR(150, getDurationMs(vec[1].t, tNow).count(), kTIMING_TOLERANCE_MS);
153 EXPECT_EQ(3, vec[2].val);
154 EXPECT_NEAR(200, getDurationMs(vec[2].t, tNow).count(), kTIMING_TOLERANCE_MS);
155 EXPECT_EQ(4, vec[3].val);
156 EXPECT_NEAR(250, getDurationMs(vec[3].t, tNow).count(), kTIMING_TOLERANCE_MS);
157 EXPECT_EQ(5, vec[4].val);
158 EXPECT_NEAR(300, getDurationMs(vec[4].t, tNow).count(), kTIMING_TOLERANCE_MS);
159 EXPECT_EQ(6, vec[5].val);
160 EXPECT_NEAR(350, getDurationMs(vec[5].t, tNow).count(), kTIMING_TOLERANCE_MS);
161 }
162
163 } // namespace pixel
164 } // namespace impl
165 } // namespace power
166 } // namespace hardware
167 } // namespace google
168 } // namespace aidl
169