2018-01-04 12:37:05 +00:00
|
|
|
#include "test_timers.h"
|
|
|
|
|
|
|
|
#include "lwip/def.h"
|
|
|
|
#include "lwip/timeouts.h"
|
|
|
|
#include "arch/sys_arch.h"
|
|
|
|
|
|
|
|
/* Setups/teardown functions */
|
|
|
|
|
|
|
|
static struct sys_timeo* old_list_head;
|
|
|
|
|
|
|
|
static void
|
|
|
|
timers_setup(void)
|
|
|
|
{
|
2018-01-11 08:53:07 +00:00
|
|
|
struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
|
2018-01-04 12:37:05 +00:00
|
|
|
old_list_head = *list_head;
|
|
|
|
*list_head = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
timers_teardown(void)
|
|
|
|
{
|
2018-01-11 08:53:07 +00:00
|
|
|
struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
|
2018-01-04 12:37:05 +00:00
|
|
|
*list_head = old_list_head;
|
|
|
|
lwip_sys_now = 0;
|
|
|
|
}
|
|
|
|
|
2018-01-05 06:49:39 +00:00
|
|
|
static int fired[3];
|
2018-01-08 07:34:17 +00:00
|
|
|
static void
|
|
|
|
dummy_handler(void* arg)
|
2018-01-04 12:37:05 +00:00
|
|
|
{
|
2018-01-05 06:49:39 +00:00
|
|
|
int index = LWIP_PTR_NUMERIC_CAST(int, arg);
|
|
|
|
fired[index] = 1;
|
2018-01-04 12:37:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 12:45:51 +00:00
|
|
|
#define HANDLER_EXECUTION_TIME 5
|
|
|
|
static int cyclic_fired;
|
2018-01-08 07:34:17 +00:00
|
|
|
static void
|
|
|
|
dummy_cyclic_handler(void)
|
2018-01-05 12:45:51 +00:00
|
|
|
{
|
|
|
|
cyclic_fired = 1;
|
|
|
|
lwip_sys_now += HANDLER_EXECUTION_TIME;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct lwip_cyclic_timer test_cyclic = {10, dummy_cyclic_handler};
|
|
|
|
|
2018-01-08 07:34:17 +00:00
|
|
|
static void
|
|
|
|
do_test_cyclic_timers(u32_t offset)
|
2018-01-05 12:45:51 +00:00
|
|
|
{
|
2018-01-11 08:53:07 +00:00
|
|
|
struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
|
2018-01-05 12:45:51 +00:00
|
|
|
|
|
|
|
/* verify normal timer expiration */
|
|
|
|
lwip_sys_now = offset + 0;
|
|
|
|
sys_timeout(test_cyclic.interval_ms, lwip_cyclic_timer, &test_cyclic);
|
|
|
|
|
|
|
|
cyclic_fired = 0;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(cyclic_fired == 0);
|
|
|
|
|
|
|
|
lwip_sys_now = offset + test_cyclic.interval_ms;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(cyclic_fired == 1);
|
|
|
|
|
|
|
|
fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + test_cyclic.interval_ms - HANDLER_EXECUTION_TIME));
|
|
|
|
|
|
|
|
sys_untimeout(lwip_cyclic_timer, &test_cyclic);
|
|
|
|
|
|
|
|
|
|
|
|
/* verify "overload" - next cyclic timer execution is already overdue twice */
|
|
|
|
lwip_sys_now = offset + 0;
|
|
|
|
sys_timeout(test_cyclic.interval_ms, lwip_cyclic_timer, &test_cyclic);
|
|
|
|
|
|
|
|
cyclic_fired = 0;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(cyclic_fired == 0);
|
|
|
|
|
|
|
|
lwip_sys_now = offset + 2*test_cyclic.interval_ms;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(cyclic_fired == 1);
|
|
|
|
|
|
|
|
fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + test_cyclic.interval_ms));
|
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(test_cyclic_timers)
|
|
|
|
{
|
|
|
|
LWIP_UNUSED_ARG(_i);
|
|
|
|
|
|
|
|
/* check without u32_t wraparound */
|
|
|
|
do_test_cyclic_timers(0);
|
|
|
|
|
|
|
|
/* check with u32_t wraparound */
|
|
|
|
do_test_cyclic_timers(0xfffffff0);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
/* reproduce bug #52748: the bug in timeouts.c */
|
2018-01-05 07:20:36 +00:00
|
|
|
START_TEST(test_bug52748)
|
2018-01-04 12:37:05 +00:00
|
|
|
{
|
2018-01-05 07:20:36 +00:00
|
|
|
LWIP_UNUSED_ARG(_i);
|
|
|
|
|
|
|
|
memset(&fired, 0, sizeof(fired));
|
|
|
|
|
|
|
|
lwip_sys_now = 50;
|
|
|
|
sys_timeout(20, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
|
|
|
|
sys_timeout( 5, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2));
|
|
|
|
|
|
|
|
lwip_sys_now = 55;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(fired[0] == 0);
|
|
|
|
fail_unless(fired[1] == 0);
|
|
|
|
fail_unless(fired[2] == 1);
|
|
|
|
|
|
|
|
lwip_sys_now = 60;
|
|
|
|
sys_timeout(10, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1));
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(fired[0] == 0);
|
|
|
|
fail_unless(fired[1] == 0);
|
|
|
|
fail_unless(fired[2] == 1);
|
|
|
|
|
|
|
|
lwip_sys_now = 70;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(fired[0] == 1);
|
|
|
|
fail_unless(fired[1] == 1);
|
|
|
|
fail_unless(fired[2] == 1);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2018-01-08 07:34:17 +00:00
|
|
|
static void
|
|
|
|
do_test_timers(u32_t offset)
|
2018-01-05 07:20:36 +00:00
|
|
|
{
|
2018-01-11 08:53:07 +00:00
|
|
|
struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
|
2018-01-05 12:45:51 +00:00
|
|
|
|
|
|
|
lwip_sys_now = offset + 0;
|
2018-01-04 12:37:05 +00:00
|
|
|
|
2018-01-05 06:49:39 +00:00
|
|
|
sys_timeout(10, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
|
2018-01-04 12:37:05 +00:00
|
|
|
fail_unless(sys_timeouts_sleeptime() == 10);
|
2018-01-05 06:49:39 +00:00
|
|
|
sys_timeout(20, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1));
|
2018-01-04 12:37:05 +00:00
|
|
|
fail_unless(sys_timeouts_sleeptime() == 10);
|
2018-01-05 06:49:39 +00:00
|
|
|
sys_timeout( 5, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2));
|
2018-01-04 12:37:05 +00:00
|
|
|
fail_unless(sys_timeouts_sleeptime() == 5);
|
|
|
|
|
2018-01-05 06:49:39 +00:00
|
|
|
/* linked list correctly sorted? */
|
|
|
|
fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + 5));
|
|
|
|
fail_unless((*list_head)->next->time == (u32_t)(lwip_sys_now + 10));
|
|
|
|
fail_unless((*list_head)->next->next->time == (u32_t)(lwip_sys_now + 20));
|
|
|
|
|
|
|
|
/* check timers expire in correct order */
|
|
|
|
memset(&fired, 0, sizeof(fired));
|
|
|
|
|
|
|
|
lwip_sys_now += 4;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(fired[2] == 0);
|
|
|
|
|
|
|
|
lwip_sys_now += 1;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(fired[2] == 1);
|
|
|
|
|
|
|
|
lwip_sys_now += 4;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(fired[0] == 0);
|
|
|
|
|
|
|
|
lwip_sys_now += 1;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(fired[0] == 1);
|
|
|
|
|
|
|
|
lwip_sys_now += 9;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(fired[1] == 0);
|
|
|
|
|
|
|
|
lwip_sys_now += 1;
|
|
|
|
sys_check_timeouts();
|
|
|
|
fail_unless(fired[1] == 1);
|
|
|
|
|
|
|
|
sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
|
|
|
|
sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1));
|
|
|
|
sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2));
|
2018-01-05 12:45:51 +00:00
|
|
|
}
|
2018-01-04 12:37:05 +00:00
|
|
|
|
2018-01-05 12:45:51 +00:00
|
|
|
START_TEST(test_timers)
|
|
|
|
{
|
|
|
|
LWIP_UNUSED_ARG(_i);
|
2018-01-05 06:49:39 +00:00
|
|
|
|
2018-01-05 12:45:51 +00:00
|
|
|
/* check without u32_t wraparound */
|
|
|
|
do_test_timers(0);
|
2018-01-05 06:49:39 +00:00
|
|
|
|
2018-01-05 12:45:51 +00:00
|
|
|
/* check with u32_t wraparound */
|
|
|
|
do_test_timers(0xfffffff0);
|
2018-01-04 12:37:05 +00:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
/** Create the suite including all tests for this module */
|
|
|
|
Suite *
|
|
|
|
timers_suite(void)
|
|
|
|
{
|
|
|
|
testfunc tests[] = {
|
2018-01-05 10:42:14 +00:00
|
|
|
TESTFUNC(test_bug52748),
|
2018-01-05 12:45:51 +00:00
|
|
|
TESTFUNC(test_cyclic_timers),
|
2018-01-05 07:20:36 +00:00
|
|
|
TESTFUNC(test_timers)
|
2018-01-04 12:37:05 +00:00
|
|
|
};
|
|
|
|
return create_suite("TIMERS", tests, LWIP_ARRAYSIZE(tests), timers_setup, timers_teardown);
|
|
|
|
}
|