blob: 954468c299a3e820016711cd80c47e0ca4545671 [file] [log] [blame]
Zach Johnson2bbb33c2014-08-15 16:49:15 -07001/******************************************************************************
2 *
3 * Copyright (C) 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
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070019#include <gtest/gtest.h>
Zach Johnson2bbb33c2014-08-15 16:49:15 -070020
21#include "AlarmTestHarness.h"
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070022
23extern "C" {
Pavlin Radoslavovc196f212015-09-23 20:39:53 -070024#include "osi/include/alarm.h"
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080025#include "osi/include/fixed_queue.h"
Pavlin Radoslavovc196f212015-09-23 20:39:53 -070026#include "osi/include/osi.h"
27#include "osi/include/semaphore.h"
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080028#include "osi/include/thread.h"
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070029}
30
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070031static semaphore_t *semaphore;
32static int cb_counter;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080033static int cb_misordered_counter;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070034
35static const uint64_t EPSILON_MS = 5;
36
37static void msleep(uint64_t ms) {
38 usleep(ms * 1000);
39}
40
Zach Johnson2bbb33c2014-08-15 16:49:15 -070041class AlarmTest : public AlarmTestHarness {
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070042 protected:
43 virtual void SetUp() {
Zach Johnson2bbb33c2014-08-15 16:49:15 -070044 AlarmTestHarness::SetUp();
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070045 cb_counter = 0;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080046 cb_misordered_counter = 0;
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070047
48 semaphore = semaphore_new(0);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070049 }
50
51 virtual void TearDown() {
Zach Johnsonee2aa452014-08-26 20:16:03 -070052 semaphore_free(semaphore);
Zach Johnson2bbb33c2014-08-15 16:49:15 -070053 AlarmTestHarness::TearDown();
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070054 }
55};
56
Sharvil Nanavatie0cdd3392014-07-17 15:45:40 -070057static void cb(UNUSED_ATTR void *data) {
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070058 ++cb_counter;
59 semaphore_post(semaphore);
60}
61
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080062static void ordered_cb(void *data) {
63 int i = PTR_TO_INT(data);
64 if (i != cb_counter)
65 cb_misordered_counter++;
66 ++cb_counter;
67 semaphore_post(semaphore);
68}
69
Zach Johnsonee2aa452014-08-26 20:16:03 -070070TEST_F(AlarmTest, test_new_free_simple) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080071 alarm_t *alarm = alarm_new("alarm_test.test_new_free_simple");
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070072 ASSERT_TRUE(alarm != NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070073 alarm_free(alarm);
74}
75
76TEST_F(AlarmTest, test_free_null) {
77 alarm_free(NULL);
78}
79
80TEST_F(AlarmTest, test_simple_cancel) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080081 alarm_t *alarm = alarm_new("alarm_test.test_simple_cancel");
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070082 alarm_cancel(alarm);
83 alarm_free(alarm);
84}
85
86TEST_F(AlarmTest, test_cancel) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080087 alarm_t *alarm = alarm_new("alarm_test.test_cancel");
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070088 alarm_set(alarm, 10, cb, NULL);
89 alarm_cancel(alarm);
90
91 msleep(10 + EPSILON_MS);
92
93 EXPECT_EQ(cb_counter, 0);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080094 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -070095 alarm_free(alarm);
96}
97
98TEST_F(AlarmTest, test_cancel_idempotent) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080099 alarm_t *alarm = alarm_new("alarm_test.test_cancel_idempotent");
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700100 alarm_set(alarm, 10, cb, NULL);
101 alarm_cancel(alarm);
102 alarm_cancel(alarm);
103 alarm_cancel(alarm);
104 alarm_free(alarm);
105}
106
107TEST_F(AlarmTest, test_set_short) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800108 alarm_t *alarm = alarm_new("alarm_test.test_set_short");
109
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700110 alarm_set(alarm, 10, cb, NULL);
111
112 EXPECT_EQ(cb_counter, 0);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800113 EXPECT_TRUE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700114
115 semaphore_wait(semaphore);
116
117 EXPECT_EQ(cb_counter, 1);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800118 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700119
120 alarm_free(alarm);
121}
122
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800123TEST_F(AlarmTest, test_set_short_periodic) {
124 alarm_t *alarm = alarm_new_periodic("alarm_test.test_set_short_periodic");
125
126 alarm_set(alarm, 10, cb, NULL);
127
128 EXPECT_EQ(cb_counter, 0);
129 EXPECT_TRUE(WakeLockHeld());
130
131 for (int i = 1; i <= 10; i++) {
132 semaphore_wait(semaphore);
133
134 EXPECT_GE(cb_counter, i);
135 EXPECT_TRUE(WakeLockHeld());
136 }
137 alarm_cancel(alarm);
138 EXPECT_FALSE(WakeLockHeld());
139
140 alarm_free(alarm);
141}
142
Pavlin Radoslavov399f2172016-04-19 17:30:19 -0700143TEST_F(AlarmTest, test_set_zero_periodic) {
144 alarm_t *alarm = alarm_new_periodic("alarm_test.test_set_zero_periodic");
145
146 alarm_set(alarm, 0, cb, NULL);
147
Pavlin Radoslavov399f2172016-04-19 17:30:19 -0700148 EXPECT_TRUE(WakeLockHeld());
149
150 for (int i = 1; i <= 10; i++) {
151 semaphore_wait(semaphore);
152
153 EXPECT_GE(cb_counter, i);
154 EXPECT_TRUE(WakeLockHeld());
155 }
156 alarm_cancel(alarm);
157 EXPECT_FALSE(WakeLockHeld());
158
159 alarm_free(alarm);
160}
161
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700162TEST_F(AlarmTest, test_set_long) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800163 alarm_t *alarm = alarm_new("alarm_test.test_set_long");
Marie Janssenac786e32015-12-29 15:34:55 -0800164 alarm_set(alarm, TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700165
166 EXPECT_EQ(cb_counter, 0);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800167 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700168
169 semaphore_wait(semaphore);
170
171 EXPECT_EQ(cb_counter, 1);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800172 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700173
174 alarm_free(alarm);
175}
176
177TEST_F(AlarmTest, test_set_short_short) {
178 alarm_t *alarm[2] = {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800179 alarm_new("alarm_test.test_set_short_short_0"),
180 alarm_new("alarm_test.test_set_short_short_1")
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700181 };
182
183 alarm_set(alarm[0], 10, cb, NULL);
184 alarm_set(alarm[1], 20, cb, NULL);
185
186 EXPECT_EQ(cb_counter, 0);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800187 EXPECT_TRUE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700188
189 semaphore_wait(semaphore);
190
191 EXPECT_EQ(cb_counter, 1);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800192 EXPECT_TRUE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700193
194 semaphore_wait(semaphore);
195
196 EXPECT_EQ(cb_counter, 2);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800197 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700198
199 alarm_free(alarm[0]);
200 alarm_free(alarm[1]);
201}
202
203TEST_F(AlarmTest, test_set_short_long) {
204 alarm_t *alarm[2] = {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800205 alarm_new("alarm_test.test_set_short_long_0"),
206 alarm_new("alarm_test.test_set_short_long_1")
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700207 };
208
209 alarm_set(alarm[0], 10, cb, NULL);
210 alarm_set(alarm[1], 10 + TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
211
212 EXPECT_EQ(cb_counter, 0);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800213 EXPECT_TRUE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700214
215 semaphore_wait(semaphore);
216
217 EXPECT_EQ(cb_counter, 1);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800218 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700219
220 semaphore_wait(semaphore);
221
222 EXPECT_EQ(cb_counter, 2);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800223 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700224
225 alarm_free(alarm[0]);
226 alarm_free(alarm[1]);
227}
228
229TEST_F(AlarmTest, test_set_long_long) {
230 alarm_t *alarm[2] = {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800231 alarm_new("alarm_test.test_set_long_long_0"),
232 alarm_new("alarm_test.test_set_long_long_1")
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700233 };
234
Marie Janssenac786e32015-12-29 15:34:55 -0800235 alarm_set(alarm[0], TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
236 alarm_set(alarm[1], 2 * (TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS), cb, NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700237
238 EXPECT_EQ(cb_counter, 0);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800239 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700240
241 semaphore_wait(semaphore);
242
243 EXPECT_EQ(cb_counter, 1);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800244 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700245
246 semaphore_wait(semaphore);
247
248 EXPECT_EQ(cb_counter, 2);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800249 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700250
251 alarm_free(alarm[0]);
252 alarm_free(alarm[1]);
253}
254
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800255TEST_F(AlarmTest, test_is_scheduled) {
256 alarm_t *alarm = alarm_new("alarm_test.test_is_scheduled");
257
258 EXPECT_FALSE(alarm_is_scheduled((alarm_t *)NULL));
259 EXPECT_FALSE(alarm_is_scheduled(alarm));
260 alarm_set(alarm, TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
261 EXPECT_TRUE(alarm_is_scheduled(alarm));
262
263 EXPECT_EQ(cb_counter, 0);
264 EXPECT_FALSE(WakeLockHeld());
265
266 semaphore_wait(semaphore);
267
268 EXPECT_FALSE(alarm_is_scheduled(alarm));
269 EXPECT_EQ(cb_counter, 1);
270 EXPECT_FALSE(WakeLockHeld());
271
272 alarm_free(alarm);
273}
274
275// Test whether the callbacks are invoked in the expected order
276TEST_F(AlarmTest, test_callback_ordering) {
277 alarm_t *alarms[100];
278
279 for (int i = 0; i < 100; i++) {
280 const std::string alarm_name = "alarm_test.test_callback_ordering[" +
281 std::to_string(i) + "]";
282 alarms[i] = alarm_new(alarm_name.c_str());
283 }
284
285 for (int i = 0; i < 100; i++) {
286 alarm_set(alarms[i], 100, ordered_cb, INT_TO_PTR(i));
287 }
288
289 for (int i = 1; i <= 100; i++) {
290 semaphore_wait(semaphore);
291 EXPECT_GE(cb_counter, i);
292 }
293 EXPECT_EQ(cb_counter, 100);
294 EXPECT_EQ(cb_misordered_counter, 0);
295
296 for (int i = 0; i < 100; i++)
297 alarm_free(alarms[i]);
298
299 EXPECT_FALSE(WakeLockHeld());
300}
301
302// Test whether the callbacks are involed in the expected order on a
303// separate queue.
304TEST_F(AlarmTest, test_callback_ordering_on_queue) {
305 alarm_t *alarms[100];
306 fixed_queue_t *queue = fixed_queue_new(SIZE_MAX);
307 thread_t *thread = thread_new("timers.test_callback_ordering_on_queue.thread");
308
309 alarm_register_processing_queue(queue, thread);
310
311 for (int i = 0; i < 100; i++) {
312 const std::string alarm_name =
313 "alarm_test.test_callback_ordering_on_queue[" +
314 std::to_string(i) + "]";
315 alarms[i] = alarm_new(alarm_name.c_str());
316 }
317
318 for (int i = 0; i < 100; i++) {
319 alarm_set_on_queue(alarms[i], 100, ordered_cb, INT_TO_PTR(i), queue);
320 }
321
322 for (int i = 1; i <= 100; i++) {
323 semaphore_wait(semaphore);
324 EXPECT_GE(cb_counter, i);
325 }
326 EXPECT_EQ(cb_counter, 100);
327 EXPECT_EQ(cb_misordered_counter, 0);
328
329 for (int i = 0; i < 100; i++)
330 alarm_free(alarms[i]);
331
332 EXPECT_FALSE(WakeLockHeld());
333
334 alarm_unregister_processing_queue(queue);
335 fixed_queue_free(queue, NULL);
336 thread_free(thread);
337}
338
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700339// Try to catch any race conditions between the timer callback and |alarm_free|.
340TEST_F(AlarmTest, test_callback_free_race) {
341 for (int i = 0; i < 1000; ++i) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800342 const std::string alarm_name = "alarm_test.test_callback_free_race[" +
343 std::to_string(i) + "]";
344 alarm_t *alarm = alarm_new(alarm_name.c_str());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700345 alarm_set(alarm, 0, cb, NULL);
346 alarm_free(alarm);
347 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800348 alarm_cleanup();
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700349}