blob: c4a2dca6f5293b0e4cfd3ac0e7ad0530574f1dea [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
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700143TEST_F(AlarmTest, test_set_long) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800144 alarm_t *alarm = alarm_new("alarm_test.test_set_long");
Marie Janssenac786e32015-12-29 15:34:55 -0800145 alarm_set(alarm, TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700146
147 EXPECT_EQ(cb_counter, 0);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800148 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700149
150 semaphore_wait(semaphore);
151
152 EXPECT_EQ(cb_counter, 1);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800153 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700154
155 alarm_free(alarm);
156}
157
158TEST_F(AlarmTest, test_set_short_short) {
159 alarm_t *alarm[2] = {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800160 alarm_new("alarm_test.test_set_short_short_0"),
161 alarm_new("alarm_test.test_set_short_short_1")
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700162 };
163
164 alarm_set(alarm[0], 10, cb, NULL);
165 alarm_set(alarm[1], 20, cb, NULL);
166
167 EXPECT_EQ(cb_counter, 0);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800168 EXPECT_TRUE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700169
170 semaphore_wait(semaphore);
171
172 EXPECT_EQ(cb_counter, 1);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800173 EXPECT_TRUE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700174
175 semaphore_wait(semaphore);
176
177 EXPECT_EQ(cb_counter, 2);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800178 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700179
180 alarm_free(alarm[0]);
181 alarm_free(alarm[1]);
182}
183
184TEST_F(AlarmTest, test_set_short_long) {
185 alarm_t *alarm[2] = {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800186 alarm_new("alarm_test.test_set_short_long_0"),
187 alarm_new("alarm_test.test_set_short_long_1")
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700188 };
189
190 alarm_set(alarm[0], 10, cb, NULL);
191 alarm_set(alarm[1], 10 + TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
192
193 EXPECT_EQ(cb_counter, 0);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800194 EXPECT_TRUE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700195
196 semaphore_wait(semaphore);
197
198 EXPECT_EQ(cb_counter, 1);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800199 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700200
201 semaphore_wait(semaphore);
202
203 EXPECT_EQ(cb_counter, 2);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800204 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700205
206 alarm_free(alarm[0]);
207 alarm_free(alarm[1]);
208}
209
210TEST_F(AlarmTest, test_set_long_long) {
211 alarm_t *alarm[2] = {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800212 alarm_new("alarm_test.test_set_long_long_0"),
213 alarm_new("alarm_test.test_set_long_long_1")
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700214 };
215
Marie Janssenac786e32015-12-29 15:34:55 -0800216 alarm_set(alarm[0], TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
217 alarm_set(alarm[1], 2 * (TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS), cb, NULL);
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700218
219 EXPECT_EQ(cb_counter, 0);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800220 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700221
222 semaphore_wait(semaphore);
223
224 EXPECT_EQ(cb_counter, 1);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800225 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700226
227 semaphore_wait(semaphore);
228
229 EXPECT_EQ(cb_counter, 2);
Marie Janssen1b2bd2c2015-11-16 10:35:17 -0800230 EXPECT_FALSE(WakeLockHeld());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700231
232 alarm_free(alarm[0]);
233 alarm_free(alarm[1]);
234}
235
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800236TEST_F(AlarmTest, test_is_scheduled) {
237 alarm_t *alarm = alarm_new("alarm_test.test_is_scheduled");
238
239 EXPECT_FALSE(alarm_is_scheduled((alarm_t *)NULL));
240 EXPECT_FALSE(alarm_is_scheduled(alarm));
241 alarm_set(alarm, TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
242 EXPECT_TRUE(alarm_is_scheduled(alarm));
243
244 EXPECT_EQ(cb_counter, 0);
245 EXPECT_FALSE(WakeLockHeld());
246
247 semaphore_wait(semaphore);
248
249 EXPECT_FALSE(alarm_is_scheduled(alarm));
250 EXPECT_EQ(cb_counter, 1);
251 EXPECT_FALSE(WakeLockHeld());
252
253 alarm_free(alarm);
254}
255
256// Test whether the callbacks are invoked in the expected order
257TEST_F(AlarmTest, test_callback_ordering) {
258 alarm_t *alarms[100];
259
260 for (int i = 0; i < 100; i++) {
261 const std::string alarm_name = "alarm_test.test_callback_ordering[" +
262 std::to_string(i) + "]";
263 alarms[i] = alarm_new(alarm_name.c_str());
264 }
265
266 for (int i = 0; i < 100; i++) {
267 alarm_set(alarms[i], 100, ordered_cb, INT_TO_PTR(i));
268 }
269
270 for (int i = 1; i <= 100; i++) {
271 semaphore_wait(semaphore);
272 EXPECT_GE(cb_counter, i);
273 }
274 EXPECT_EQ(cb_counter, 100);
275 EXPECT_EQ(cb_misordered_counter, 0);
276
277 for (int i = 0; i < 100; i++)
278 alarm_free(alarms[i]);
279
280 EXPECT_FALSE(WakeLockHeld());
281}
282
283// Test whether the callbacks are involed in the expected order on a
284// separate queue.
285TEST_F(AlarmTest, test_callback_ordering_on_queue) {
286 alarm_t *alarms[100];
287 fixed_queue_t *queue = fixed_queue_new(SIZE_MAX);
288 thread_t *thread = thread_new("timers.test_callback_ordering_on_queue.thread");
289
290 alarm_register_processing_queue(queue, thread);
291
292 for (int i = 0; i < 100; i++) {
293 const std::string alarm_name =
294 "alarm_test.test_callback_ordering_on_queue[" +
295 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_on_queue(alarms[i], 100, ordered_cb, INT_TO_PTR(i), queue);
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++)
311 alarm_free(alarms[i]);
312
313 EXPECT_FALSE(WakeLockHeld());
314
315 alarm_unregister_processing_queue(queue);
316 fixed_queue_free(queue, NULL);
317 thread_free(thread);
318}
319
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700320// Try to catch any race conditions between the timer callback and |alarm_free|.
321TEST_F(AlarmTest, test_callback_free_race) {
322 for (int i = 0; i < 1000; ++i) {
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800323 const std::string alarm_name = "alarm_test.test_callback_free_race[" +
324 std::to_string(i) + "]";
325 alarm_t *alarm = alarm_new(alarm_name.c_str());
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700326 alarm_set(alarm, 0, cb, NULL);
327 alarm_free(alarm);
328 }
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800329 alarm_cleanup();
Sharvil Nanavatif0e7c8b2014-07-01 18:42:56 -0700330}