1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "RecurrentTimer.h"
#include <android-base/thread_annotations.h>
#include <gtest/gtest.h>
#include <chrono>
#include <memory>
#include <mutex>
namespace android {
namespace hardware {
namespace automotive {
namespace vehicle {
class RecurrentTimerTest : public testing::Test {
public:
std::shared_ptr<RecurrentTimer::Callback> getCallback(size_t token) {
return std::make_shared<RecurrentTimer::Callback>([this, token] {
std::scoped_lock<std::mutex> lockGuard(mLock);
mCallbacks.push_back(token);
});
}
std::vector<size_t> getCalledCallbacks() {
std::scoped_lock<std::mutex> lockGuard(mLock);
return mCallbacks;
}
void clearCalledCallbacks() {
std::scoped_lock<std::mutex> lockGuard(mLock);
mCallbacks.clear();
}
size_t countTimerCallbackQueue(RecurrentTimer* timer) {
std::scoped_lock<std::mutex> lockGuard(timer->mLock);
return timer->mCallbackQueue.size();
}
private:
std::mutex mLock;
std::vector<size_t> mCallbacks GUARDED_BY(mLock);
};
TEST_F(RecurrentTimerTest, testRegisterCallback) {
RecurrentTimer timer;
// 0.1s
int64_t interval = 100000000;
auto action = getCallback(0);
timer.registerTimerCallback(interval, action);
std::this_thread::sleep_for(std::chrono::seconds(1));
timer.unregisterTimerCallback(action);
// Theoretically trigger 10 times, but check for at least 9 times to be stable.
ASSERT_GE(getCalledCallbacks().size(), static_cast<size_t>(9));
}
TEST_F(RecurrentTimerTest, testRegisterUnregisterRegister) {
RecurrentTimer timer;
// 0.1s
int64_t interval = 100000000;
auto action = getCallback(0);
timer.registerTimerCallback(interval, action);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
timer.unregisterTimerCallback(action);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
clearCalledCallbacks();
timer.registerTimerCallback(interval, action);
std::this_thread::sleep_for(std::chrono::seconds(1));
// Theoretically trigger 10 times, but check for at least 9 times to be stable.
ASSERT_GE(getCalledCallbacks().size(), static_cast<size_t>(9));
}
TEST_F(RecurrentTimerTest, testDestroyTimerWithCallback) {
std::unique_ptr<RecurrentTimer> timer = std::make_unique<RecurrentTimer>();
// 0.1s
int64_t interval = 100000000;
auto action = getCallback(0);
timer->registerTimerCallback(interval, action);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
timer.reset();
clearCalledCallbacks();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
ASSERT_TRUE(getCalledCallbacks().empty());
}
TEST_F(RecurrentTimerTest, testRegisterMultipleCallbacks) {
RecurrentTimer timer;
// 0.1s
int64_t interval1 = 100000000;
auto action1 = getCallback(1);
timer.registerTimerCallback(interval1, action1);
// 0.05s
int64_t interval2 = 50000000;
auto action2 = getCallback(2);
timer.registerTimerCallback(interval2, action2);
// 0.03s
int64_t interval3 = 30000000;
auto action3 = getCallback(3);
timer.registerTimerCallback(interval3, action3);
std::this_thread::sleep_for(std::chrono::seconds(1));
timer.unregisterTimerCallback(action1);
timer.unregisterTimerCallback(action2);
timer.unregisterTimerCallback(action3);
size_t action1Count = 0;
size_t action2Count = 0;
size_t action3Count = 0;
for (size_t token : getCalledCallbacks()) {
if (token == 1) {
action1Count++;
}
if (token == 2) {
action2Count++;
}
if (token == 3) {
action3Count++;
}
}
// Theoretically trigger 10 times, but check for at least 9 times to be stable.
ASSERT_GE(action1Count, static_cast<size_t>(9));
// Theoretically trigger 20 times, but check for at least 15 times to be stable.
ASSERT_GE(action2Count, static_cast<size_t>(15));
// Theoretically trigger 33 times, but check for at least 25 times to be stable.
ASSERT_GE(action3Count, static_cast<size_t>(25));
}
TEST_F(RecurrentTimerTest, testRegisterSameCallbackMultipleTimes) {
RecurrentTimer timer;
// 0.02s
int64_t interval1 = 20000000;
// 0.01s
int64_t interval2 = 10000000;
auto action = getCallback(0);
for (int i = 0; i < 10; i++) {
timer.registerTimerCallback(interval1, action);
timer.registerTimerCallback(interval2, action);
}
clearCalledCallbacks();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Theoretically trigger 10 times, but check for at least 9 times to be stable.
ASSERT_GE(getCalledCallbacks().size(), static_cast<size_t>(9));
timer.unregisterTimerCallback(action);
// Make sure there is no item in the callback queue.
ASSERT_EQ(countTimerCallbackQueue(&timer), static_cast<size_t>(0));
}
TEST_F(RecurrentTimerTest, testRegisterCallbackMultipleTimesNoDeadLock) {
// We want to avoid the following situation:
// Caller holds a lock while calling registerTimerCallback, registerTimerCallback will try
// to obtain an internal lock inside timer.
// Meanwhile an recurrent action happens with timer holding an internal lock. The action
// tries to obtain the lock currently hold by the caller.
// The solution is that while calling recurrent actions, timer must not hold the internal lock.
std::unique_ptr<RecurrentTimer> timer = std::make_unique<RecurrentTimer>();
std::mutex lock;
for (size_t i = 0; i < 1000; i++) {
std::scoped_lock<std::mutex> lockGuard(lock);
auto action = std::make_shared<RecurrentTimer::Callback>([&lock] {
// While calling this function, the timer must not hold lock in order not to dead
// lock.
std::scoped_lock<std::mutex> lockGuard(lock);
});
// 10ms
int64_t interval = 10'000'000;
timer->registerTimerCallback(interval, action);
// Sleep for a little while to let the recurrent actions begin.
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
// Make sure we stop the timer before we destroy lock.
timer.reset();
}
} // namespace vehicle
} // namespace automotive
} // namespace hardware
} // namespace android
|