Zach Johnson | 2bbb33c | 2014-08-15 16:49:15 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 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 Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
Zach Johnson | 2bbb33c | 2014-08-15 16:49:15 -0700 | [diff] [blame] | 20 | |
| 21 | #include "AlarmTestHarness.h" |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 22 | |
| 23 | extern "C" { |
Pavlin Radoslavov | c196f21 | 2015-09-23 20:39:53 -0700 | [diff] [blame] | 24 | #include "osi/include/alarm.h" |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 25 | #include "osi/include/fixed_queue.h" |
Pavlin Radoslavov | c196f21 | 2015-09-23 20:39:53 -0700 | [diff] [blame] | 26 | #include "osi/include/osi.h" |
| 27 | #include "osi/include/semaphore.h" |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 28 | #include "osi/include/thread.h" |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 31 | static semaphore_t *semaphore; |
| 32 | static int cb_counter; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 33 | static int cb_misordered_counter; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 34 | |
| 35 | static const uint64_t EPSILON_MS = 5; |
| 36 | |
| 37 | static void msleep(uint64_t ms) { |
| 38 | usleep(ms * 1000); |
| 39 | } |
| 40 | |
Zach Johnson | 2bbb33c | 2014-08-15 16:49:15 -0700 | [diff] [blame] | 41 | class AlarmTest : public AlarmTestHarness { |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 42 | protected: |
| 43 | virtual void SetUp() { |
Zach Johnson | 2bbb33c | 2014-08-15 16:49:15 -0700 | [diff] [blame] | 44 | AlarmTestHarness::SetUp(); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 45 | cb_counter = 0; |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 46 | cb_misordered_counter = 0; |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 47 | |
| 48 | semaphore = semaphore_new(0); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | virtual void TearDown() { |
Zach Johnson | ee2aa45 | 2014-08-26 20:16:03 -0700 | [diff] [blame] | 52 | semaphore_free(semaphore); |
Zach Johnson | 2bbb33c | 2014-08-15 16:49:15 -0700 | [diff] [blame] | 53 | AlarmTestHarness::TearDown(); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
Sharvil Nanavati | e0cdd339 | 2014-07-17 15:45:40 -0700 | [diff] [blame] | 57 | static void cb(UNUSED_ATTR void *data) { |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 58 | ++cb_counter; |
| 59 | semaphore_post(semaphore); |
| 60 | } |
| 61 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 62 | static 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 Johnson | ee2aa45 | 2014-08-26 20:16:03 -0700 | [diff] [blame] | 70 | TEST_F(AlarmTest, test_new_free_simple) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 71 | alarm_t *alarm = alarm_new("alarm_test.test_new_free_simple"); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(alarm != NULL); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 73 | alarm_free(alarm); |
| 74 | } |
| 75 | |
| 76 | TEST_F(AlarmTest, test_free_null) { |
| 77 | alarm_free(NULL); |
| 78 | } |
| 79 | |
| 80 | TEST_F(AlarmTest, test_simple_cancel) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 81 | alarm_t *alarm = alarm_new("alarm_test.test_simple_cancel"); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 82 | alarm_cancel(alarm); |
| 83 | alarm_free(alarm); |
| 84 | } |
| 85 | |
| 86 | TEST_F(AlarmTest, test_cancel) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 87 | alarm_t *alarm = alarm_new("alarm_test.test_cancel"); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 88 | alarm_set(alarm, 10, cb, NULL); |
| 89 | alarm_cancel(alarm); |
| 90 | |
| 91 | msleep(10 + EPSILON_MS); |
| 92 | |
| 93 | EXPECT_EQ(cb_counter, 0); |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 94 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 95 | alarm_free(alarm); |
| 96 | } |
| 97 | |
| 98 | TEST_F(AlarmTest, test_cancel_idempotent) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 99 | alarm_t *alarm = alarm_new("alarm_test.test_cancel_idempotent"); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 100 | alarm_set(alarm, 10, cb, NULL); |
| 101 | alarm_cancel(alarm); |
| 102 | alarm_cancel(alarm); |
| 103 | alarm_cancel(alarm); |
| 104 | alarm_free(alarm); |
| 105 | } |
| 106 | |
| 107 | TEST_F(AlarmTest, test_set_short) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 108 | alarm_t *alarm = alarm_new("alarm_test.test_set_short"); |
| 109 | |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 110 | alarm_set(alarm, 10, cb, NULL); |
| 111 | |
| 112 | EXPECT_EQ(cb_counter, 0); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 113 | EXPECT_TRUE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 114 | |
| 115 | semaphore_wait(semaphore); |
| 116 | |
| 117 | EXPECT_EQ(cb_counter, 1); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 118 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 119 | |
| 120 | alarm_free(alarm); |
| 121 | } |
| 122 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 123 | TEST_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 Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 143 | TEST_F(AlarmTest, test_set_long) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 144 | alarm_t *alarm = alarm_new("alarm_test.test_set_long"); |
Marie Janssen | ac786e3 | 2015-12-29 15:34:55 -0800 | [diff] [blame] | 145 | alarm_set(alarm, TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 146 | |
| 147 | EXPECT_EQ(cb_counter, 0); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 148 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 149 | |
| 150 | semaphore_wait(semaphore); |
| 151 | |
| 152 | EXPECT_EQ(cb_counter, 1); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 153 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 154 | |
| 155 | alarm_free(alarm); |
| 156 | } |
| 157 | |
| 158 | TEST_F(AlarmTest, test_set_short_short) { |
| 159 | alarm_t *alarm[2] = { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 160 | alarm_new("alarm_test.test_set_short_short_0"), |
| 161 | alarm_new("alarm_test.test_set_short_short_1") |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 162 | }; |
| 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 Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 168 | EXPECT_TRUE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 169 | |
| 170 | semaphore_wait(semaphore); |
| 171 | |
| 172 | EXPECT_EQ(cb_counter, 1); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 173 | EXPECT_TRUE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 174 | |
| 175 | semaphore_wait(semaphore); |
| 176 | |
| 177 | EXPECT_EQ(cb_counter, 2); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 178 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 179 | |
| 180 | alarm_free(alarm[0]); |
| 181 | alarm_free(alarm[1]); |
| 182 | } |
| 183 | |
| 184 | TEST_F(AlarmTest, test_set_short_long) { |
| 185 | alarm_t *alarm[2] = { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 186 | alarm_new("alarm_test.test_set_short_long_0"), |
| 187 | alarm_new("alarm_test.test_set_short_long_1") |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 188 | }; |
| 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 Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 194 | EXPECT_TRUE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 195 | |
| 196 | semaphore_wait(semaphore); |
| 197 | |
| 198 | EXPECT_EQ(cb_counter, 1); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 199 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 200 | |
| 201 | semaphore_wait(semaphore); |
| 202 | |
| 203 | EXPECT_EQ(cb_counter, 2); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 204 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 205 | |
| 206 | alarm_free(alarm[0]); |
| 207 | alarm_free(alarm[1]); |
| 208 | } |
| 209 | |
| 210 | TEST_F(AlarmTest, test_set_long_long) { |
| 211 | alarm_t *alarm[2] = { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 212 | alarm_new("alarm_test.test_set_long_long_0"), |
| 213 | alarm_new("alarm_test.test_set_long_long_1") |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
Marie Janssen | ac786e3 | 2015-12-29 15:34:55 -0800 | [diff] [blame] | 216 | 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 Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 218 | |
| 219 | EXPECT_EQ(cb_counter, 0); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 220 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 221 | |
| 222 | semaphore_wait(semaphore); |
| 223 | |
| 224 | EXPECT_EQ(cb_counter, 1); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 225 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 226 | |
| 227 | semaphore_wait(semaphore); |
| 228 | |
| 229 | EXPECT_EQ(cb_counter, 2); |
Marie Janssen | 1b2bd2c | 2015-11-16 10:35:17 -0800 | [diff] [blame] | 230 | EXPECT_FALSE(WakeLockHeld()); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 231 | |
| 232 | alarm_free(alarm[0]); |
| 233 | alarm_free(alarm[1]); |
| 234 | } |
| 235 | |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 236 | TEST_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 |
| 257 | TEST_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. |
| 285 | TEST_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 Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 320 | // Try to catch any race conditions between the timer callback and |alarm_free|. |
| 321 | TEST_F(AlarmTest, test_callback_free_race) { |
| 322 | for (int i = 0; i < 1000; ++i) { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 323 | 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 Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 326 | alarm_set(alarm, 0, cb, NULL); |
| 327 | alarm_free(alarm); |
| 328 | } |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame^] | 329 | alarm_cleanup(); |
Sharvil Nanavati | f0e7c8b | 2014-07-01 18:42:56 -0700 | [diff] [blame] | 330 | } |