Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_timers.c Source File

test_timers.c

00001 #include "test_timers.h"
00002 
00003 #include "lwip/def.h"
00004 #include "lwip/timeouts.h"
00005 #include "arch/sys_arch.h"
00006 
00007 /* Setups/teardown functions */
00008 
00009 static struct sys_timeo* old_list_head;
00010 
00011 static void
00012 timers_setup(void)
00013 {
00014   struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
00015   old_list_head = *list_head;
00016   *list_head = NULL;
00017 }
00018 
00019 static void
00020 timers_teardown(void)
00021 {
00022   struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
00023   *list_head = old_list_head;
00024   lwip_sys_now = 0;
00025 }
00026 
00027 static int fired[3];
00028 static void
00029 dummy_handler(void* arg)
00030 {
00031   int index = LWIP_PTR_NUMERIC_CAST(int, arg);
00032   fired[index] = 1;
00033 }
00034 
00035 #define HANDLER_EXECUTION_TIME 5
00036 static int cyclic_fired;
00037 static void
00038 dummy_cyclic_handler(void)
00039 {
00040    cyclic_fired = 1;
00041    lwip_sys_now += HANDLER_EXECUTION_TIME;
00042 }
00043 
00044 struct lwip_cyclic_timer test_cyclic = {10, dummy_cyclic_handler};
00045 
00046 static void
00047 do_test_cyclic_timers(u32_t offset)
00048 {
00049   struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
00050 
00051   /* verify normal timer expiration */
00052   lwip_sys_now = offset + 0;
00053   sys_timeout(test_cyclic.interval_ms, lwip_cyclic_timer, &test_cyclic);
00054 
00055   cyclic_fired = 0;
00056   sys_check_timeouts();
00057   fail_unless(cyclic_fired == 0);
00058 
00059   lwip_sys_now = offset + test_cyclic.interval_ms;
00060   sys_check_timeouts();
00061   fail_unless(cyclic_fired == 1);
00062 
00063   fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + test_cyclic.interval_ms - HANDLER_EXECUTION_TIME));
00064   
00065   sys_untimeout(lwip_cyclic_timer, &test_cyclic);
00066 
00067 
00068   /* verify "overload" - next cyclic timer execution is already overdue twice */
00069   lwip_sys_now = offset + 0;
00070   sys_timeout(test_cyclic.interval_ms, lwip_cyclic_timer, &test_cyclic);
00071 
00072   cyclic_fired = 0;
00073   sys_check_timeouts();
00074   fail_unless(cyclic_fired == 0);
00075 
00076   lwip_sys_now = offset + 2*test_cyclic.interval_ms;
00077   sys_check_timeouts();
00078   fail_unless(cyclic_fired == 1);
00079 
00080   fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + test_cyclic.interval_ms));
00081 }
00082 
00083 START_TEST(test_cyclic_timers)
00084 {
00085   LWIP_UNUSED_ARG(_i);
00086 
00087   /* check without u32_t wraparound */
00088   do_test_cyclic_timers(0);
00089 
00090   /* check with u32_t wraparound */
00091   do_test_cyclic_timers(0xfffffff0);
00092 }
00093 END_TEST
00094 
00095 /* reproduce bug #52748: the bug in timeouts.c */
00096 START_TEST(test_bug52748)
00097 {
00098   LWIP_UNUSED_ARG(_i);
00099 
00100   memset(&fired, 0, sizeof(fired));
00101 
00102   lwip_sys_now = 50;
00103   sys_timeout(20, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
00104   sys_timeout( 5, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2));
00105 
00106   lwip_sys_now = 55;
00107   sys_check_timeouts();
00108   fail_unless(fired[0] == 0);
00109   fail_unless(fired[1] == 0);
00110   fail_unless(fired[2] == 1);
00111 
00112   lwip_sys_now = 60;
00113   sys_timeout(10, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1));
00114   sys_check_timeouts();
00115   fail_unless(fired[0] == 0);
00116   fail_unless(fired[1] == 0);
00117   fail_unless(fired[2] == 1);
00118 
00119   lwip_sys_now = 70;
00120   sys_check_timeouts();
00121   fail_unless(fired[0] == 1);
00122   fail_unless(fired[1] == 1);
00123   fail_unless(fired[2] == 1);
00124 }
00125 END_TEST
00126 
00127 static void
00128 do_test_timers(u32_t offset)
00129 {
00130   struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
00131   
00132   lwip_sys_now = offset + 0;
00133 
00134   sys_timeout(10, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
00135   fail_unless(sys_timeouts_sleeptime() == 10);
00136   sys_timeout(20, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1));
00137   fail_unless(sys_timeouts_sleeptime() == 10);
00138   sys_timeout( 5, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2));
00139   fail_unless(sys_timeouts_sleeptime() == 5);
00140 
00141   /* linked list correctly sorted? */
00142   fail_unless((*list_head)->time             == (u32_t)(lwip_sys_now + 5));
00143   fail_unless((*list_head)->next->time       == (u32_t)(lwip_sys_now + 10));
00144   fail_unless((*list_head)->next->next->time == (u32_t)(lwip_sys_now + 20));
00145   
00146   /* check timers expire in correct order */
00147   memset(&fired, 0, sizeof(fired));
00148 
00149   lwip_sys_now += 4;
00150   sys_check_timeouts();
00151   fail_unless(fired[2] == 0);
00152 
00153   lwip_sys_now += 1;
00154   sys_check_timeouts();
00155   fail_unless(fired[2] == 1);
00156 
00157   lwip_sys_now += 4;
00158   sys_check_timeouts();
00159   fail_unless(fired[0] == 0);
00160 
00161   lwip_sys_now += 1;
00162   sys_check_timeouts();
00163   fail_unless(fired[0] == 1);
00164 
00165   lwip_sys_now += 9;
00166   sys_check_timeouts();
00167   fail_unless(fired[1] == 0);
00168 
00169   lwip_sys_now += 1;
00170   sys_check_timeouts();
00171   fail_unless(fired[1] == 1);
00172 
00173   sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
00174   sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1));
00175   sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2));
00176 }
00177 
00178 START_TEST(test_timers)
00179 {
00180   LWIP_UNUSED_ARG(_i);
00181 
00182   /* check without u32_t wraparound */
00183   do_test_timers(0);
00184 
00185   /* check with u32_t wraparound */
00186   do_test_timers(0xfffffff0);
00187 }
00188 END_TEST
00189 
00190 START_TEST(test_long_timer)
00191 {
00192   LWIP_UNUSED_ARG(_i);
00193 
00194   memset(&fired, 0, sizeof(fired));
00195   lwip_sys_now = 0;
00196 
00197   sys_timeout(LWIP_UINT32_MAX / 4, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
00198   fail_unless(sys_timeouts_sleeptime() == LWIP_UINT32_MAX / 4);
00199 
00200   sys_check_timeouts();
00201   fail_unless(fired[0] == 0);
00202 
00203   lwip_sys_now += LWIP_UINT32_MAX / 8;
00204 
00205   sys_check_timeouts();
00206   fail_unless(fired[0] == 0);
00207 
00208   lwip_sys_now += LWIP_UINT32_MAX / 8;
00209 
00210   sys_check_timeouts();
00211   fail_unless(fired[0] == 0);
00212 
00213   lwip_sys_now += 1;
00214 
00215   sys_check_timeouts();
00216   fail_unless(fired[0] == 1);
00217 
00218   sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
00219 }
00220 END_TEST
00221 
00222 /** Create the suite including all tests for this module */
00223 Suite *
00224 timers_suite(void)
00225 {
00226   testfunc tests[] = {
00227     TESTFUNC(test_bug52748),
00228     TESTFUNC(test_cyclic_timers),
00229     TESTFUNC(test_timers),
00230     TESTFUNC(test_long_timer),
00231   };
00232   return create_suite("TIMERS", tests, LWIP_ARRAYSIZE(tests), timers_setup, timers_teardown);
00233 }