xref: /btstack/test/embedded/run_loop_base_test.cpp (revision a64cbea79fa5fb9daa58e135a19c6cce10f3e642)
1 #include "CppUTest/TestHarness.h"
2 #include "CppUTest/CommandLineTestRunner.h"
3 
4 #include "btstack_run_loop.h"
5 #include "btstack_memory.h"
6 
7 #define HEARTBEAT_PERIOD_MS 1000
8 
9 static btstack_timer_source_t timer_1;
10 static btstack_timer_source_t timer_2;
11 static btstack_data_source_t  data_source;
12 static bool data_source_called;
13 static bool timer_called;
14 
15 static void heartbeat_timeout_handler(btstack_timer_source_t * ts){
16     UNUSED(ts);
17     timer_called = true;
18 }
19 static void data_source_handler(btstack_data_source_t * ds, btstack_data_source_callback_type_t callback_type){
20     UNUSED(ds);
21     UNUSED(callback_type);
22     data_source_called = true;
23 }
24 
25 TEST_GROUP(RunLoopBase){
26         void setup(void){
27             btstack_memory_init();
28             btstack_run_loop_base_init();
29             data_source_called = false;
30             timer_called = false;
31         }
32         void teardown(void){
33             btstack_memory_deinit();
34         }
35 };
36 
37 TEST(RunLoopBase, DataSource){
38     btstack_run_loop_set_data_source_handler(&data_source, &data_source_handler);
39     btstack_run_loop_base_enable_data_source_callbacks(&data_source, DATA_SOURCE_CALLBACK_POLL);
40     btstack_run_loop_base_add_data_source(&data_source);
41     btstack_run_loop_base_disable_data_source_callbacks(&data_source, DATA_SOURCE_CALLBACK_POLL);
42     btstack_run_loop_base_remove_data_source(&data_source);
43 }
44 
45 TEST(RunLoopBase,Timer){
46     const uint32_t timeout_1 = 7;
47     const uint32_t timeout_2 = 10;
48     const uint32_t now_1   =  5;
49     const uint32_t now_2   = 11;
50     const uint32_t now_3   = 33;
51 
52     btstack_run_loop_set_timer_handler(&timer_1, heartbeat_timeout_handler);
53     timer_1.timeout = timeout_1;
54 
55     btstack_run_loop_set_timer_handler(&timer_2, heartbeat_timeout_handler);
56     timer_2.timeout = timeout_2;
57 
58     // test add/remove
59 
60     // add timer 2
61     btstack_run_loop_base_add_timer(&timer_2);
62     CHECK(btstack_run_loop_base_timers != NULL);
63     // remove timer
64     btstack_run_loop_base_remove_timer(&timer_2);
65     CHECK(btstack_run_loop_base_timers == NULL);
66 
67     // add timer 2
68     btstack_run_loop_base_add_timer(&timer_2);
69     // add timer 1
70     btstack_run_loop_base_add_timer(&timer_1);
71     // dump timers
72     btstack_run_loop_base_dump_timer();
73     // process timers for now_1
74     btstack_run_loop_base_process_timers(now_1);
75     CHECK(timer_called == false);
76     CHECK(btstack_run_loop_base_timers != NULL);
77     // get next timeout
78     int next_timeout = btstack_run_loop_base_get_time_until_timeout(now_1);
79     CHECK(next_timeout == (timeout_1 - now_1));
80     // get next timeout in future - timeout should be 0 = now
81     next_timeout = btstack_run_loop_base_get_time_until_timeout(now_3);
82     CHECK(next_timeout == 0);
83     // process timers for now_2
84     btstack_run_loop_base_process_timers(now_2);
85     CHECK(timer_called == true);
86     CHECK(btstack_run_loop_base_timers == NULL);
87     // get next timeout
88     next_timeout = btstack_run_loop_base_get_time_until_timeout(now_2);
89     CHECK(next_timeout == -1);
90 }
91 
92 TEST(RunLoopBase, Overrun){
93     const uint32_t t1 = 0xfffffff0UL;   // -16
94     const uint32_t t2 = 0xfffffff8UL;   //  -8
95     const uint32_t t3 = 50UL;
96     int next_timeout;
97 
98     btstack_run_loop_set_timer_handler(&timer_1, heartbeat_timeout_handler);
99     timer_1.timeout = t1 + 20;  // overrun
100     // add timer
101     btstack_run_loop_base_add_timer(&timer_1);
102     // verify timeout until correct
103     next_timeout = btstack_run_loop_base_get_time_until_timeout(t1);
104     CHECK(next_timeout == 20);
105     // process timers for t2
106     btstack_run_loop_base_process_timers(t2);
107     CHECK(timer_called == false);
108     // process timers for t3
109     btstack_run_loop_base_process_timers(t3);
110     CHECK(timer_called == true);
111 }
112 
113 int main (int argc, const char * argv[]){
114     return CommandLineTestRunner::RunAllTests(argc, argv);
115 }
116