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/run_loop.h>
20 #include <gtest/gtest.h>
21 
22 #include "AlarmTestHarness.h"
23 
24 #include "common/message_loop_thread.h"
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 
30 using base::Closure;
31 using base::TimeDelta;
32 using bluetooth::common::MessageLoopThread;
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 static MessageLoopThread* thread_;
43 
get_main_thread()44 bluetooth::common::MessageLoopThread* get_main_thread() { return thread_; }
45 
46 class AlarmTest : public AlarmTestHarness {
47  protected:
SetUp()48   void SetUp() override {
49     AlarmTestHarness::SetUp();
50     cb_counter = 0;
51     cb_misordered_counter = 0;
52 
53     semaphore = semaphore_new(0);
54   }
55 
TearDown()56   void TearDown() override {
57     semaphore_free(semaphore);
58     AlarmTestHarness::TearDown();
59   }
60 };
61 
cb(UNUSED_ATTR void * data)62 static void cb(UNUSED_ATTR void* data) {
63   ++cb_counter;
64   semaphore_post(semaphore);
65 }
66 
ordered_cb(void * data)67 static void ordered_cb(void* data) {
68   int i = PTR_TO_INT(data);
69   if (i != cb_counter) cb_misordered_counter++;
70   ++cb_counter;
71   semaphore_post(semaphore);
72 }
73 
TEST_F(AlarmTest,test_new_free_simple)74 TEST_F(AlarmTest, test_new_free_simple) {
75   alarm_t* alarm = alarm_new("alarm_test.test_new_free_simple");
76   ASSERT_TRUE(alarm != NULL);
77   alarm_free(alarm);
78 }
79 
TEST_F(AlarmTest,test_free_null)80 TEST_F(AlarmTest, test_free_null) { alarm_free(NULL); }
81 
TEST_F(AlarmTest,test_simple_cancel)82 TEST_F(AlarmTest, test_simple_cancel) {
83   alarm_t* alarm = alarm_new("alarm_test.test_simple_cancel");
84   alarm_cancel(alarm);
85   alarm_free(alarm);
86 }
87 
TEST_F(AlarmTest,test_cancel)88 TEST_F(AlarmTest, test_cancel) {
89   alarm_t* alarm = alarm_new("alarm_test.test_cancel");
90   alarm_set(alarm, 10, cb, NULL);
91   alarm_cancel(alarm);
92 
93   msleep(10 + EPSILON_MS);
94 
95   EXPECT_EQ(cb_counter, 0);
96   EXPECT_FALSE(WakeLockHeld());
97   alarm_free(alarm);
98 }
99 
TEST_F(AlarmTest,test_cancel_idempotent)100 TEST_F(AlarmTest, test_cancel_idempotent) {
101   alarm_t* alarm = alarm_new("alarm_test.test_cancel_idempotent");
102   alarm_set(alarm, 10, cb, NULL);
103   alarm_cancel(alarm);
104   alarm_cancel(alarm);
105   alarm_cancel(alarm);
106   alarm_free(alarm);
107 }
108 
TEST_F(AlarmTest,test_set_short)109 TEST_F(AlarmTest, test_set_short) {
110   alarm_t* alarm = alarm_new("alarm_test.test_set_short");
111 
112   alarm_set(alarm, 10, cb, NULL);
113 
114   EXPECT_EQ(cb_counter, 0);
115   EXPECT_TRUE(WakeLockHeld());
116 
117   semaphore_wait(semaphore);
118 
119   EXPECT_EQ(cb_counter, 1);
120   EXPECT_FALSE(WakeLockHeld());
121 
122   alarm_free(alarm);
123 }
124 
TEST_F(AlarmTest,test_set_short_periodic)125 TEST_F(AlarmTest, test_set_short_periodic) {
126   alarm_t* alarm = alarm_new_periodic("alarm_test.test_set_short_periodic");
127 
128   alarm_set(alarm, 10, cb, NULL);
129 
130   EXPECT_EQ(cb_counter, 0);
131   EXPECT_TRUE(WakeLockHeld());
132 
133   for (int i = 1; i <= 10; i++) {
134     semaphore_wait(semaphore);
135 
136     EXPECT_GE(cb_counter, i);
137     EXPECT_TRUE(WakeLockHeld());
138   }
139   alarm_cancel(alarm);
140   EXPECT_FALSE(WakeLockHeld());
141 
142   alarm_free(alarm);
143 }
144 
TEST_F(AlarmTest,test_set_zero_periodic)145 TEST_F(AlarmTest, test_set_zero_periodic) {
146   alarm_t* alarm = alarm_new_periodic("alarm_test.test_set_zero_periodic");
147 
148   alarm_set(alarm, 0, cb, NULL);
149 
150   EXPECT_TRUE(WakeLockHeld());
151 
152   for (int i = 1; i <= 10; i++) {
153     semaphore_wait(semaphore);
154 
155     EXPECT_GE(cb_counter, i);
156     EXPECT_TRUE(WakeLockHeld());
157   }
158   alarm_cancel(alarm);
159   EXPECT_FALSE(WakeLockHeld());
160 
161   alarm_free(alarm);
162 }
163 
TEST_F(AlarmTest,test_set_long)164 TEST_F(AlarmTest, test_set_long) {
165   alarm_t* alarm = alarm_new("alarm_test.test_set_long");
166   alarm_set(alarm, TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
167 
168   EXPECT_EQ(cb_counter, 0);
169   EXPECT_FALSE(WakeLockHeld());
170 
171   semaphore_wait(semaphore);
172 
173   EXPECT_EQ(cb_counter, 1);
174   EXPECT_FALSE(WakeLockHeld());
175 
176   alarm_free(alarm);
177 }
178 
TEST_F(AlarmTest,test_set_short_short)179 TEST_F(AlarmTest, test_set_short_short) {
180   alarm_t* alarm[2] = {alarm_new("alarm_test.test_set_short_short_0"),
181                        alarm_new("alarm_test.test_set_short_short_1")};
182 
183   alarm_set(alarm[0], 10, cb, NULL);
184   alarm_set(alarm[1], 20, cb, NULL);
185 
186   EXPECT_EQ(cb_counter, 0);
187   EXPECT_TRUE(WakeLockHeld());
188 
189   semaphore_wait(semaphore);
190 
191   EXPECT_EQ(cb_counter, 1);
192   EXPECT_TRUE(WakeLockHeld());
193 
194   semaphore_wait(semaphore);
195 
196   EXPECT_EQ(cb_counter, 2);
197   EXPECT_FALSE(WakeLockHeld());
198 
199   alarm_free(alarm[0]);
200   alarm_free(alarm[1]);
201 }
202 
TEST_F(AlarmTest,test_set_short_long)203 TEST_F(AlarmTest, test_set_short_long) {
204   alarm_t* alarm[2] = {alarm_new("alarm_test.test_set_short_long_0"),
205                        alarm_new("alarm_test.test_set_short_long_1")};
206 
207   alarm_set(alarm[0], 10, cb, NULL);
208   alarm_set(alarm[1], 10 + TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb,
209             NULL);
210 
211   EXPECT_EQ(cb_counter, 0);
212   EXPECT_TRUE(WakeLockHeld());
213 
214   semaphore_wait(semaphore);
215 
216   EXPECT_EQ(cb_counter, 1);
217   EXPECT_FALSE(WakeLockHeld());
218 
219   semaphore_wait(semaphore);
220 
221   EXPECT_EQ(cb_counter, 2);
222   EXPECT_FALSE(WakeLockHeld());
223 
224   alarm_free(alarm[0]);
225   alarm_free(alarm[1]);
226 }
227 
TEST_F(AlarmTest,test_set_long_long)228 TEST_F(AlarmTest, test_set_long_long) {
229   alarm_t* alarm[2] = {alarm_new("alarm_test.test_set_long_long_0"),
230                        alarm_new("alarm_test.test_set_long_long_1")};
231 
232   alarm_set(alarm[0], TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
233   alarm_set(alarm[1], 2 * (TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS), cb,
234             NULL);
235 
236   EXPECT_EQ(cb_counter, 0);
237   EXPECT_FALSE(WakeLockHeld());
238 
239   semaphore_wait(semaphore);
240 
241   EXPECT_EQ(cb_counter, 1);
242   EXPECT_FALSE(WakeLockHeld());
243 
244   semaphore_wait(semaphore);
245 
246   EXPECT_EQ(cb_counter, 2);
247   EXPECT_FALSE(WakeLockHeld());
248 
249   alarm_free(alarm[0]);
250   alarm_free(alarm[1]);
251 }
252 
TEST_F(AlarmTest,test_is_scheduled)253 TEST_F(AlarmTest, test_is_scheduled) {
254   alarm_t* alarm = alarm_new("alarm_test.test_is_scheduled");
255 
256   EXPECT_FALSE(alarm_is_scheduled((alarm_t*)NULL));
257   EXPECT_FALSE(alarm_is_scheduled(alarm));
258   alarm_set(alarm, TIMER_INTERVAL_FOR_WAKELOCK_IN_MS + EPSILON_MS, cb, NULL);
259   EXPECT_TRUE(alarm_is_scheduled(alarm));
260 
261   EXPECT_EQ(cb_counter, 0);
262   EXPECT_FALSE(WakeLockHeld());
263 
264   semaphore_wait(semaphore);
265 
266   EXPECT_FALSE(alarm_is_scheduled(alarm));
267   EXPECT_EQ(cb_counter, 1);
268   EXPECT_FALSE(WakeLockHeld());
269 
270   alarm_free(alarm);
271 }
272 
273 // Test whether the callbacks are invoked in the expected order
TEST_F(AlarmTest,test_callback_ordering)274 TEST_F(AlarmTest, test_callback_ordering) {
275   alarm_t* alarms[100];
276 
277   for (int i = 0; i < 100; i++) {
278     const std::string alarm_name =
279         "alarm_test.test_callback_ordering[" + std::to_string(i) + "]";
280     alarms[i] = alarm_new(alarm_name.c_str());
281   }
282 
283   for (int i = 0; i < 100; i++) {
284     alarm_set(alarms[i], 100, ordered_cb, INT_TO_PTR(i));
285   }
286 
287   for (int i = 1; i <= 100; i++) {
288     semaphore_wait(semaphore);
289     EXPECT_GE(cb_counter, i);
290   }
291   EXPECT_EQ(cb_counter, 100);
292   EXPECT_EQ(cb_misordered_counter, 0);
293 
294   for (int i = 0; i < 100; i++) alarm_free(alarms[i]);
295 
296   EXPECT_FALSE(WakeLockHeld());
297 }
298 
299 // Test whether the callbacks are involed in the expected order on a
300 // message loop.
TEST_F(AlarmTest,test_callback_ordering_on_mloop)301 TEST_F(AlarmTest, test_callback_ordering_on_mloop) {
302   alarm_t* alarms[100];
303 
304   // Initialize MesageLoop, and wait till it's initialized.
305   MessageLoopThread message_loop_thread("btu message loop");
306   message_loop_thread.StartUp();
307   if (!message_loop_thread.IsRunning()) {
308     FAIL() << "unable to create btu message loop thread.";
309   }
310   thread_ = &message_loop_thread;
311 
312   for (int i = 0; i < 100; i++) {
313     const std::string alarm_name =
314         "alarm_test.test_callback_ordering_on_mloop[" + 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_mloop(alarms[i], 100, ordered_cb, INT_TO_PTR(i));
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++) alarm_free(alarms[i]);
330 
331   message_loop_thread.ShutDown();
332   EXPECT_FALSE(WakeLockHeld());
333 }
334 
335 // Try to catch any race conditions between the timer callback and |alarm_free|.
TEST_F(AlarmTest,test_callback_free_race)336 TEST_F(AlarmTest, test_callback_free_race) {
337   for (int i = 0; i < 1000; ++i) {
338     const std::string alarm_name =
339         "alarm_test.test_callback_free_race[" + std::to_string(i) + "]";
340     alarm_t* alarm = alarm_new(alarm_name.c_str());
341     alarm_set(alarm, 0, cb, NULL);
342     alarm_free(alarm);
343   }
344   alarm_cleanup();
345 }
346 
remove_cb(void * data)347 static void remove_cb(void* data) {
348   alarm_free((alarm_t*)data);
349   semaphore_post(semaphore);
350 }
351 
TEST_F(AlarmTest,test_delete_during_callback)352 TEST_F(AlarmTest, test_delete_during_callback) {
353   for (int i = 0; i < 1000; ++i) {
354     alarm_t* alarm = alarm_new("alarm_test.test_delete_during_callback");
355     alarm_set(alarm, 0, remove_cb, alarm);
356     semaphore_wait(semaphore);
357   }
358   alarm_cleanup();
359 }
360