1 /******************************************************************************
2 *
3 * Copyright 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <base/message_loop/message_loop.h>
20 #include <base/run_loop.h>
21 #include <gtest/gtest.h>
22
23 #include "AlarmTestHarness.h"
24
25 #include "osi/include/alarm.h"
26 #include "osi/include/fixed_queue.h"
27 #include "osi/include/osi.h"
28 #include "osi/include/semaphore.h"
29 #include "osi/include/thread.h"
30
31 using base::Closure;
32 using base::TimeDelta;
33
34 static semaphore_t* semaphore;
35 static int cb_counter;
36 static int cb_misordered_counter;
37
38 static const uint64_t EPSILON_MS = 50;
39
msleep(uint64_t ms)40 static void msleep(uint64_t ms) { usleep(ms * 1000); }
41
42 base::MessageLoop* message_loop_;
43 base::RunLoop* run_loop_;
44 static semaphore_t* msg_loop_ready;
45
message_loop_run(UNUSED_ATTR void * context)46 void message_loop_run(UNUSED_ATTR void* context) {
47 message_loop_ = new base::MessageLoop();
48 run_loop_ = new base::RunLoop();
49
50 semaphore_post(msg_loop_ready);
51 run_loop_->Run();
52
53 delete message_loop_;
54 message_loop_ = nullptr;
55
56 delete run_loop_;
57 run_loop_ = nullptr;
58 }
59
get_message_loop()60 base::MessageLoop* get_message_loop() { return message_loop_; }
61
62 class AlarmTest : public AlarmTestHarness {
63 protected:
SetUp()64 virtual void SetUp() {
65 AlarmTestHarness::SetUp();
66 cb_counter = 0;
67 cb_misordered_counter = 0;
68
69 semaphore = semaphore_new(0);
70 }
71
TearDown()72 virtual void TearDown() {
73 semaphore_free(semaphore);
74 AlarmTestHarness::TearDown();
75 }
76 };
77
cb(UNUSED_ATTR void * data)78 static void cb(UNUSED_ATTR void* data) {
79 ++cb_counter;
80 semaphore_post(semaphore);
81 }
82
ordered_cb(void * data)83 static void ordered_cb(void* data) {
84 int i = PTR_TO_INT(data);
85 if (i != cb_counter) cb_misordered_counter++;
86 ++cb_counter;
87 semaphore_post(semaphore);
88 }
89
TEST_F(AlarmTest,test_new_free_simple)90 TEST_F(AlarmTest, test_new_free_simple) {
91 alarm_t* alarm = alarm_new("alarm_test.test_new_free_simple");
92 ASSERT_TRUE(alarm != NULL);
93 alarm_free(alarm);
94 }
95
TEST_F(AlarmTest,test_free_null)96 TEST_F(AlarmTest, test_free_null) { alarm_free(NULL); }
97
TEST_F(AlarmTest,test_simple_cancel)98 TEST_F(AlarmTest, test_simple_cancel) {
99 alarm_t* alarm = alarm_new("alarm_test.test_simple_cancel");
100 alarm_cancel(alarm);
101 alarm_free(alarm);
102 }
103
TEST_F(AlarmTest,test_cancel)104 TEST_F(AlarmTest, test_cancel) {
105 alarm_t* alarm = alarm_new("alarm_test.test_cancel");
106 alarm_set(alarm, 10, cb, NULL);
107 alarm_cancel(alarm);
108
109 msleep(10 + EPSILON_MS);
110
111 EXPECT_EQ(cb_counter, 0);
112 EXPECT_FALSE(WakeLockHeld());
113 alarm_free(alarm);
114 }
115
TEST_F(AlarmTest,test_cancel_idempotent)116 TEST_F(AlarmTest, test_cancel_idempotent) {
117 alarm_t* alarm = alarm_new("alarm_test.test_cancel_idempotent");
118 alarm_set(alarm, 10, cb, NULL);
119 alarm_cancel(alarm);
120 alarm_cancel(alarm);
121 alarm_cancel(alarm);
122 alarm_free(alarm);
123 }
124
TEST_F(AlarmTest,test_set_short)125 TEST_F(AlarmTest, test_set_short) {
126 alarm_t* alarm = alarm_new("alarm_test.test_set_short");
127
128 alarm_set(alarm, 10, cb, NULL);
129
130 EXPECT_EQ(cb_counter, 0);
131 EXPECT_TRUE(WakeLockHeld());
132
133 semaphore_wait(semaphore);
134
135 EXPECT_EQ(cb_counter, 1);
136 EXPECT_FALSE(WakeLockHeld());
137
138 alarm_free(alarm);
139 }
140
TEST_F(AlarmTest,test_set_short_periodic)141 TEST_F(AlarmTest, test_set_short_periodic) {
142 alarm_t* alarm = alarm_new_periodic("alarm_test.test_set_short_periodic");
143
144 alarm_set(alarm, 10, cb, NULL);
145
146 EXPECT_EQ(cb_counter, 0);
147 EXPECT_TRUE(WakeLockHeld());
148
149 for (int i = 1; i <= 10; i++) {
150 semaphore_wait(semaphore);
151
152 EXPECT_GE(cb_counter, i);
153 EXPECT_TRUE(WakeLockHeld());
154 }
155 alarm_cancel(alarm);
156 EXPECT_FALSE(WakeLockHeld());
157
158 alarm_free(alarm);
159 }
160
TEST_F(AlarmTest,test_set_zero_periodic)161 TEST_F(AlarmTest, test_set_zero_periodic) {
162 alarm_t* alarm = alarm_new_periodic("alarm_test.test_set_zero_periodic");
163
164 alarm_set(alarm, 0, cb, NULL);
165
166 EXPECT_TRUE(WakeLockHeld());
167
168 for (int i = 1; i <= 10; i++) {
169 semaphore_wait(semaphore);
170
171 EXPECT_GE(cb_counter, i);
172 EXPECT_TRUE(WakeLockHeld());
173 }
174 alarm_cancel(alarm);
175 EXPECT_FALSE(WakeLockHeld());
176
177 alarm_free(alarm);
178 }
179
TEST_F(AlarmTest,test_set_long)180 TEST_F(AlarmTest, test_set_long) {
181 alarm_t* alarm = alarm_new("alarm_test.test_set_long");
182 alarm_set(alarm, TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
183
184 EXPECT_EQ(cb_counter, 0);
185 EXPECT_FALSE(WakeLockHeld());
186
187 semaphore_wait(semaphore);
188
189 EXPECT_EQ(cb_counter, 1);
190 EXPECT_FALSE(WakeLockHeld());
191
192 alarm_free(alarm);
193 }
194
TEST_F(AlarmTest,test_set_short_short)195 TEST_F(AlarmTest, test_set_short_short) {
196 alarm_t* alarm[2] = {alarm_new("alarm_test.test_set_short_short_0"),
197 alarm_new("alarm_test.test_set_short_short_1")};
198
199 alarm_set(alarm[0], 10, cb, NULL);
200 alarm_set(alarm[1], 20, cb, NULL);
201
202 EXPECT_EQ(cb_counter, 0);
203 EXPECT_TRUE(WakeLockHeld());
204
205 semaphore_wait(semaphore);
206
207 EXPECT_EQ(cb_counter, 1);
208 EXPECT_TRUE(WakeLockHeld());
209
210 semaphore_wait(semaphore);
211
212 EXPECT_EQ(cb_counter, 2);
213 EXPECT_FALSE(WakeLockHeld());
214
215 alarm_free(alarm[0]);
216 alarm_free(alarm[1]);
217 }
218
TEST_F(AlarmTest,test_set_short_long)219 TEST_F(AlarmTest, test_set_short_long) {
220 alarm_t* alarm[2] = {alarm_new("alarm_test.test_set_short_long_0"),
221 alarm_new("alarm_test.test_set_short_long_1")};
222
223 alarm_set(alarm[0], 10, cb, NULL);
224 alarm_set(alarm[1], 10 + TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb,
225 NULL);
226
227 EXPECT_EQ(cb_counter, 0);
228 EXPECT_TRUE(WakeLockHeld());
229
230 semaphore_wait(semaphore);
231
232 EXPECT_EQ(cb_counter, 1);
233 EXPECT_FALSE(WakeLockHeld());
234
235 semaphore_wait(semaphore);
236
237 EXPECT_EQ(cb_counter, 2);
238 EXPECT_FALSE(WakeLockHeld());
239
240 alarm_free(alarm[0]);
241 alarm_free(alarm[1]);
242 }
243
TEST_F(AlarmTest,test_set_long_long)244 TEST_F(AlarmTest, test_set_long_long) {
245 alarm_t* alarm[2] = {alarm_new("alarm_test.test_set_long_long_0"),
246 alarm_new("alarm_test.test_set_long_long_1")};
247
248 alarm_set(alarm[0], TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
249 alarm_set(alarm[1], 2 * (TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS), cb,
250 NULL);
251
252 EXPECT_EQ(cb_counter, 0);
253 EXPECT_FALSE(WakeLockHeld());
254
255 semaphore_wait(semaphore);
256
257 EXPECT_EQ(cb_counter, 1);
258 EXPECT_FALSE(WakeLockHeld());
259
260 semaphore_wait(semaphore);
261
262 EXPECT_EQ(cb_counter, 2);
263 EXPECT_FALSE(WakeLockHeld());
264
265 alarm_free(alarm[0]);
266 alarm_free(alarm[1]);
267 }
268
TEST_F(AlarmTest,test_is_scheduled)269 TEST_F(AlarmTest, test_is_scheduled) {
270 alarm_t* alarm = alarm_new("alarm_test.test_is_scheduled");
271
272 EXPECT_FALSE(alarm_is_scheduled((alarm_t*)NULL));
273 EXPECT_FALSE(alarm_is_scheduled(alarm));
274 alarm_set(alarm, TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
275 EXPECT_TRUE(alarm_is_scheduled(alarm));
276
277 EXPECT_EQ(cb_counter, 0);
278 EXPECT_FALSE(WakeLockHeld());
279
280 semaphore_wait(semaphore);
281
282 EXPECT_FALSE(alarm_is_scheduled(alarm));
283 EXPECT_EQ(cb_counter, 1);
284 EXPECT_FALSE(WakeLockHeld());
285
286 alarm_free(alarm);
287 }
288
289 // Test whether the callbacks are invoked in the expected order
TEST_F(AlarmTest,test_callback_ordering)290 TEST_F(AlarmTest, test_callback_ordering) {
291 alarm_t* alarms[100];
292
293 for (int i = 0; i < 100; i++) {
294 const std::string alarm_name =
295 "alarm_test.test_callback_ordering[" + std::to_string(i) + "]";
296 alarms[i] = alarm_new(alarm_name.c_str());
297 }
298
299 for (int i = 0; i < 100; i++) {
300 alarm_set(alarms[i], 100, ordered_cb, INT_TO_PTR(i));
301 }
302
303 for (int i = 1; i <= 100; i++) {
304 semaphore_wait(semaphore);
305 EXPECT_GE(cb_counter, i);
306 }
307 EXPECT_EQ(cb_counter, 100);
308 EXPECT_EQ(cb_misordered_counter, 0);
309
310 for (int i = 0; i < 100; i++) alarm_free(alarms[i]);
311
312 EXPECT_FALSE(WakeLockHeld());
313 }
314
315 // Test whether the callbacks are involed in the expected order on a
316 // message loop.
TEST_F(AlarmTest,test_callback_ordering_on_mloop)317 TEST_F(AlarmTest, test_callback_ordering_on_mloop) {
318 alarm_t* alarms[100];
319
320 // Initialize MesageLoop, and wait till it's initialized.
321 msg_loop_ready = semaphore_new(0);
322 thread_t* message_loop_thread_ = thread_new("btu message loop");
323 if (!message_loop_thread_) {
324 FAIL() << "unable to create btu message loop thread.";
325 }
326
327 thread_post(message_loop_thread_, message_loop_run, nullptr);
328 semaphore_wait(msg_loop_ready);
329 semaphore_free(msg_loop_ready);
330
331 for (int i = 0; i < 100; i++) {
332 const std::string alarm_name =
333 "alarm_test.test_callback_ordering_on_mloop[" + std::to_string(i) + "]";
334 alarms[i] = alarm_new(alarm_name.c_str());
335 }
336
337 for (int i = 0; i < 100; i++) {
338 alarm_set_on_mloop(alarms[i], 100, ordered_cb, INT_TO_PTR(i));
339 }
340
341 for (int i = 1; i <= 100; i++) {
342 semaphore_wait(semaphore);
343 EXPECT_GE(cb_counter, i);
344 }
345 EXPECT_EQ(cb_counter, 100);
346 EXPECT_EQ(cb_misordered_counter, 0);
347
348 for (int i = 0; i < 100; i++) alarm_free(alarms[i]);
349
350 message_loop_->task_runner()->PostTask(FROM_HERE,
351 run_loop_->QuitWhenIdleClosure());
352 thread_free(message_loop_thread_);
353 EXPECT_FALSE(WakeLockHeld());
354 }
355
356 // Try to catch any race conditions between the timer callback and |alarm_free|.
TEST_F(AlarmTest,test_callback_free_race)357 TEST_F(AlarmTest, test_callback_free_race) {
358 for (int i = 0; i < 1000; ++i) {
359 const std::string alarm_name =
360 "alarm_test.test_callback_free_race[" + std::to_string(i) + "]";
361 alarm_t* alarm = alarm_new(alarm_name.c_str());
362 alarm_set(alarm, 0, cb, NULL);
363 alarm_free(alarm);
364 }
365 alarm_cleanup();
366 }
367
remove_cb(void * data)368 static void remove_cb(void* data) {
369 alarm_free((alarm_t*)data);
370 semaphore_post(semaphore);
371 }
372
TEST_F(AlarmTest,test_delete_during_callback)373 TEST_F(AlarmTest, test_delete_during_callback) {
374 for (int i = 0; i < 1000; ++i) {
375 alarm_t* alarm = alarm_new("alarm_test.test_delete_during_callback");
376 alarm_set(alarm, 0, remove_cb, alarm);
377 semaphore_wait(semaphore);
378 }
379 alarm_cleanup();
380 }
381