rt-thread-official/examples/utest/testcases/kernel/timer_tc.c

727 lines
20 KiB
C
Raw Normal View History

2021-08-23 12:37:54 +08:00
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-08-12 luckyzjq the first version
*/
#include <rtthread.h>
#include <stdlib.h>
#include "utest.h"
#undef uassert_true
#define uassert_true(value) \
do \
{ \
if (!(value)) \
{ \
__utest_assert(value, "(" #value ") is false"); \
} \
} while (0)
2021-08-23 12:37:54 +08:00
static rt_uint8_t timer_flag_oneshot[] = {
RT_TIMER_FLAG_ONE_SHOT,
RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER,
};
static rt_uint8_t timer_flag_periodic[] = {
RT_TIMER_FLAG_PERIODIC,
RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER,
RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER,
};
typedef struct test_timer_struct
{
struct rt_timer static_timer; /* static timer handler */
rt_timer_t dynamic_timer; /* dynamic timer pointer */
rt_tick_t expect_tick; /* expect tick */
rt_ubase_t callbacks; /* timer callback times */
rt_bool_t is_static; /* static or dynamic timer */
2021-08-23 12:37:54 +08:00
} timer_struct;
static timer_struct timer;
static void timer_oneshot(void *param)
2021-08-23 12:37:54 +08:00
{
timer_struct *timer_call;
timer_call = (timer_struct *)param;
timer_call->callbacks++;
2021-08-23 12:37:54 +08:00
uassert_true(rt_tick_get() == timer_call->expect_tick);
2021-08-23 12:37:54 +08:00
}
static void timer_periodic(void *param)
2021-08-23 12:37:54 +08:00
{
rt_err_t result;
timer_struct *timer_call;
timer_call = (timer_struct *)param;
timer_call->callbacks++;
uassert_true(rt_tick_get() == timer_call->expect_tick);
2021-08-23 12:37:54 +08:00
if (timer_call->is_static)
2021-08-23 12:37:54 +08:00
{
timer_call->expect_tick = rt_tick_get() + timer_call->static_timer.init_tick;
2021-08-23 12:37:54 +08:00
}
else
2021-08-23 12:37:54 +08:00
{
timer_call->expect_tick = rt_tick_get() + timer_call->dynamic_timer->init_tick;
2021-08-23 12:37:54 +08:00
}
if (timer_call->callbacks == 5)
{
/* periodic timer can stop */
if (timer_call->is_static)
{
result = rt_timer_stop(&timer_call->static_timer);
}
else
{
result = rt_timer_stop(timer_call->dynamic_timer);
}
uassert_true(result == RT_EOK);
}
2021-08-23 12:37:54 +08:00
}
static void test_static_timer(void)
2021-08-23 12:37:54 +08:00
{
rt_err_t result;
timer.callbacks = 0;
timer.is_static = RT_TRUE;
2021-08-23 12:37:54 +08:00
/* one shot timer test */
for (int time_out = 1; time_out < 10; time_out++)
2021-08-23 12:37:54 +08:00
{
for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
{
rt_timer_init(&timer.static_timer,
"static_timer",
timer_oneshot,
2021-08-23 12:37:54 +08:00
&timer,
time_out,
timer_flag_oneshot[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(&timer.static_timer);
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
/* wait for timerout */
rt_thread_delay(3 * time_out + 1);
uassert_true(timer.callbacks == 1);
2021-08-23 12:37:54 +08:00
/* detach timer */
result = rt_timer_detach(&timer.static_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
2021-08-23 12:37:54 +08:00
}
}
/* periodic timer test */
for (int time_out = 1; time_out < 10; time_out++)
2021-08-23 12:37:54 +08:00
{
for (int i = 0; i < sizeof(timer_flag_periodic); i++)
{
rt_timer_init(&timer.static_timer,
"static_timer",
timer_periodic,
2021-08-23 12:37:54 +08:00
&timer,
time_out,
timer_flag_periodic[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(&timer.static_timer);
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
/* wait for timerout */
rt_thread_delay(5 * time_out + 1);
uassert_true(timer.callbacks >= 5);
2021-08-23 12:37:54 +08:00
/* detach timer */
result = rt_timer_detach(&timer.static_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
2021-08-23 12:37:54 +08:00
}
}
}
static void test_static_timer_start_twice(void)
{
rt_err_t result;
2021-08-23 12:37:54 +08:00
timer.callbacks = 0;
timer.is_static = RT_TRUE;
2021-08-23 12:37:54 +08:00
/* timer start twice test */
for (int time_out = 2; time_out < 10; time_out++)
{
for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
{
rt_timer_init(&timer.static_timer,
"static_timer",
timer_oneshot,
&timer,
time_out,
timer_flag_oneshot[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(&timer.static_timer);
uassert_true(result == RT_EOK);
rt_thread_delay(1);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(&timer.static_timer);
uassert_true(result == RT_EOK);
/* wait for timerout */
rt_thread_delay(3 * time_out + 1);
uassert_true(timer.callbacks == 1);
/* detach timer */
result = rt_timer_detach(&timer.static_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
}
}
2021-08-23 12:37:54 +08:00
}
static void timer_control(void *param)
2021-08-23 12:37:54 +08:00
{
rt_err_t result;
timer_struct *timer_call;
timer_call = (timer_struct *)param;
timer_call->callbacks++;
2021-08-23 12:37:54 +08:00
uassert_true(rt_tick_get() == timer_call->expect_tick);
2021-08-23 12:37:54 +08:00
/* periodic timer can stop */
if (timer_call->is_static)
{
result = rt_timer_stop(&timer_call->static_timer);
}
else
2021-08-23 12:37:54 +08:00
{
result = rt_timer_stop(timer_call->dynamic_timer);
2021-08-23 12:37:54 +08:00
}
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
}
static void test_static_timer_control(void)
{
rt_err_t result;
int set_data;
int get_data;
timer.callbacks = 0;
timer.is_static = RT_TRUE;
2021-08-23 12:37:54 +08:00
rt_timer_init(&timer.static_timer,
"static_timer",
timer_control,
2021-08-23 12:37:54 +08:00
&timer,
5,
RT_TIMER_FLAG_PERIODIC);
/* test set data */
set_data = 10;
2021-08-23 12:37:54 +08:00
result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_SET_TIME, &set_data);
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
/* test get data */
result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_GET_TIME, &get_data);
uassert_true(result == RT_EOK);
uassert_true(set_data == get_data);
2021-08-23 12:37:54 +08:00
/* calc expect tick */
timer.expect_tick = rt_tick_get() + set_data;
/* start timer */
result = rt_timer_start(&timer.static_timer);
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
rt_thread_delay(3 * set_data + 1);
2021-08-23 12:37:54 +08:00
/* detach timer */
result = rt_timer_detach(&timer.static_timer);
uassert_true(result == RT_EOK);
uassert_true(timer.callbacks == 1);
}
static void timer_start_in_callback(void *param)
{
rt_err_t result;
timer_struct *timer_call;
timer_call = (timer_struct *)param;
timer_call->callbacks++;
uassert_true(rt_tick_get() == timer_call->expect_tick);
if (timer_call->is_static)
2021-08-23 12:37:54 +08:00
{
timer_call->expect_tick = rt_tick_get() + timer_call->static_timer.init_tick;
result = rt_timer_start(&timer_call->static_timer);
2021-08-23 12:37:54 +08:00
}
else
2021-08-23 12:37:54 +08:00
{
timer_call->expect_tick = rt_tick_get() + timer_call->dynamic_timer->init_tick;
result = rt_timer_start(timer_call->dynamic_timer);
2021-08-23 12:37:54 +08:00
}
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
}
static void timer_start_stop_in_callback(void *param)
2021-08-23 12:37:54 +08:00
{
rt_err_t result;
2021-08-23 12:37:54 +08:00
timer_struct *timer_call;
timer_call = (timer_struct *)param;
timer_call->callbacks++;
2021-08-23 12:37:54 +08:00
uassert_true(rt_tick_get() == timer_call->expect_tick);
if (timer_call->is_static)
{
result = rt_timer_start(&timer_call->static_timer);
}
else
{
result = rt_timer_start(timer_call->dynamic_timer);
}
uassert_true(result == RT_EOK);
if (timer_call->is_static)
{
result = rt_timer_stop(&timer_call->static_timer);
}
else
2021-08-23 12:37:54 +08:00
{
result = rt_timer_stop(timer_call->dynamic_timer);
2021-08-23 12:37:54 +08:00
}
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
}
static void test_static_timer_op_in_callback(void)
2021-08-23 12:37:54 +08:00
{
rt_err_t result;
timer.callbacks = 0;
timer.is_static = RT_TRUE;
/* start in callback test */
for (int time_out = 1; time_out < 10; time_out++)
2021-08-23 12:37:54 +08:00
{
for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
{
rt_timer_init(&timer.static_timer,
"static_timer",
timer_start_in_callback,
&timer,
time_out,
timer_flag_oneshot[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(&timer.static_timer);
uassert_true(result == RT_EOK);
/* wait for timerout */
rt_thread_delay(5 * time_out + 1);
uassert_true(timer.callbacks >= 5);
/* detach timer */
result = rt_timer_detach(&timer.static_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
}
2021-08-23 12:37:54 +08:00
}
/* start & stop in callback test */
for (int time_out = 1; time_out < 10; time_out++)
2021-08-23 12:37:54 +08:00
{
for (int i = 0; i < sizeof(timer_flag_periodic); i++)
{
rt_timer_init(&timer.static_timer,
"static_timer",
timer_start_stop_in_callback,
&timer,
time_out,
timer_flag_periodic[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
2021-08-23 12:37:54 +08:00
/* start timer */
result = rt_timer_start(&timer.static_timer);
uassert_true(result == RT_EOK);
/* wait for timerout */
rt_thread_delay(3 * time_out + 1);
uassert_true(timer.callbacks == 1);
/* detach timer */
result = rt_timer_detach(&timer.static_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
}
}
2021-08-23 12:37:54 +08:00
}
#ifdef RT_USING_HEAP
static void test_dynamic_timer(void)
2021-08-23 12:37:54 +08:00
{
rt_err_t result;
timer.callbacks = 0;
timer.is_static = RT_FALSE;
2021-08-23 12:37:54 +08:00
/* one shot timer test */
for (int time_out = 1; time_out < 10; time_out++)
2021-08-23 12:37:54 +08:00
{
for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
{
timer.dynamic_timer = rt_timer_create("dynamic_timer",
timer_oneshot,
2021-08-23 12:37:54 +08:00
&timer,
time_out,
timer_flag_oneshot[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(timer.dynamic_timer);
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
/* wait for timerout */
rt_thread_delay(3 * time_out + 1);
uassert_true(timer.callbacks == 1);
2021-08-23 12:37:54 +08:00
/* detach timer */
result = rt_timer_delete(timer.dynamic_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
2021-08-23 12:37:54 +08:00
}
}
/* periodic timer test */
for (int time_out = 1; time_out < 10; time_out++)
2021-08-23 12:37:54 +08:00
{
for (int i = 0; i < sizeof(timer_flag_periodic); i++)
{
timer.dynamic_timer = rt_timer_create("dynamic_timer",
timer_periodic,
2021-08-23 12:37:54 +08:00
&timer,
time_out,
timer_flag_periodic[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(timer.dynamic_timer);
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
/* wait for timerout */
rt_thread_delay(5 * time_out + 1);
uassert_true(timer.callbacks >= 5);
2021-08-23 12:37:54 +08:00
/* detach timer */
result = rt_timer_delete(timer.dynamic_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
2021-08-23 12:37:54 +08:00
}
}
}
static void test_dynamic_timer_control(void)
{
rt_err_t result;
int set_data;
int get_data;
timer.callbacks = 0;
timer.is_static = RT_FALSE;
2021-08-23 12:37:54 +08:00
timer.dynamic_timer = rt_timer_create("dynamic_timer",
timer_control,
2021-08-23 12:37:54 +08:00
&timer,
5,
RT_TIMER_FLAG_PERIODIC);
/* test set data */
set_data = 10;
2021-08-23 12:37:54 +08:00
result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_SET_TIME, &set_data);
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
/* test get data */
result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_GET_TIME, &get_data);
uassert_true(result == RT_EOK);
uassert_true(set_data == get_data);
2021-08-23 12:37:54 +08:00
/* calc expect tick */
timer.expect_tick = rt_tick_get() + set_data;
/* start timer */
result = rt_timer_start(timer.dynamic_timer);
uassert_true(result == RT_EOK);
2021-08-23 12:37:54 +08:00
rt_thread_delay(3 * set_data + 1);
2021-08-23 12:37:54 +08:00
/* detach timer */
result = rt_timer_delete(timer.dynamic_timer);
uassert_true(result == RT_EOK);
uassert_true(timer.callbacks == 1);
}
static void test_dynamic_timer_start_twice(void)
{
rt_err_t result;
timer.callbacks = 0;
timer.is_static = RT_FALSE;
/* timer start twice test */
for (int time_out = 2; time_out < 10; time_out++)
2021-08-23 12:37:54 +08:00
{
for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
{
timer.dynamic_timer = rt_timer_create("dynamic_timer",
timer_oneshot,
&timer,
time_out,
timer_flag_oneshot[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(timer.dynamic_timer);
uassert_true(result == RT_EOK);
rt_thread_delay(1);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(timer.dynamic_timer);
uassert_true(result == RT_EOK);
/* wait for timerout */
rt_thread_delay(3 * time_out + 1);
uassert_true(timer.callbacks == 1);
/* detach timer */
result = rt_timer_delete(timer.dynamic_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
}
2021-08-23 12:37:54 +08:00
}
}
static void test_dynamic_timer_op_in_callback(void)
{
rt_err_t result;
2021-08-23 12:37:54 +08:00
timer.callbacks = 0;
timer.is_static = RT_FALSE;
/* start in callback test */
for (int time_out = 1; time_out < 10; time_out++)
2021-08-23 12:37:54 +08:00
{
for (int i = 0; i < sizeof(timer_flag_oneshot); i++)
{
timer.dynamic_timer = rt_timer_create("dynamic_timer",
timer_start_in_callback,
&timer,
time_out,
timer_flag_oneshot[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(timer.dynamic_timer);
uassert_true(result == RT_EOK);
/* wait for timerout */
rt_thread_delay(5 * time_out + 1);
uassert_true(timer.callbacks >= 5);
/* detach timer */
result = rt_timer_delete(timer.dynamic_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
}
2021-08-23 12:37:54 +08:00
}
/* start & stop in callback test */
for (int time_out = 1; time_out < 10; time_out++)
{
for (int i = 0; i < sizeof(timer_flag_periodic); i++)
{
timer.dynamic_timer = rt_timer_create("dynamic_timer",
timer_start_stop_in_callback,
&timer,
time_out,
timer_flag_periodic[i]);
/* calc expect tick */
timer.expect_tick = rt_tick_get() + time_out;
/* start timer */
result = rt_timer_start(timer.dynamic_timer);
uassert_true(result == RT_EOK);
/* wait for timerout */
rt_thread_delay(3 * time_out + 1);
uassert_true(timer.callbacks == 1);
2021-08-23 12:37:54 +08:00
/* detach timer */
result = rt_timer_delete(timer.dynamic_timer);
uassert_true(result == RT_EOK);
timer.callbacks = 0;
}
}
}
2021-08-23 12:37:54 +08:00
#endif /* RT_USING_HEAP */
#define TEST_TIME_S 60 // test 60 seconds
#define STRESS_TIMERS 100
static struct rt_timer stress_timer[STRESS_TIMERS];
static void timer_stress(void *param)
{
rt_timer_t stress_timer = (rt_timer_t)param;
if (rand() % 2 == 0)
{
rt_timer_start(stress_timer);
}
else
{
rt_timer_stop(stress_timer);
}
}
static void test_timer_stress(void)
{
rt_tick_t start;
rt_ubase_t iters = 0;
LOG_I("timer stress test begin, it will take %d seconds", 3*TEST_TIME_S);
for (int i = 0; i < sizeof(timer_flag_periodic); i++)
{
for (int j = 0; j < STRESS_TIMERS; j++)
{
rt_timer_init(&stress_timer[j],
"stress_timer",
timer_stress,
&stress_timer[j],
j + 1,
timer_flag_periodic[i]);
}
start = rt_tick_get();
while (rt_tick_get() - start <= TEST_TIME_S * RT_TICK_PER_SECOND)
{
for (int j = 0; j < STRESS_TIMERS; j++)
{
if (rand() % 2 == 0)
{
rt_timer_start(&stress_timer[j]);
}
else
{
rt_timer_stop(&stress_timer[j]);
}
}
iters ++;
}
for (int j = 0; j < STRESS_TIMERS; j++)
{
rt_timer_detach(&stress_timer[j]);
}
}
LOG_I("success after %lu iterations", iters);
}
2021-08-23 12:37:54 +08:00
static rt_err_t utest_tc_init(void)
{
timer.dynamic_timer = RT_NULL;
timer.callbacks = 0;
2021-08-23 12:37:54 +08:00
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
timer.dynamic_timer = RT_NULL;
timer.callbacks = 0;
2021-08-23 12:37:54 +08:00
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(test_static_timer);
2021-08-23 12:37:54 +08:00
UTEST_UNIT_RUN(test_static_timer_control);
UTEST_UNIT_RUN(test_static_timer_start_twice);
UTEST_UNIT_RUN(test_static_timer_op_in_callback);
2021-08-23 12:37:54 +08:00
#ifdef RT_USING_HEAP
UTEST_UNIT_RUN(test_dynamic_timer);
2021-08-23 12:37:54 +08:00
UTEST_UNIT_RUN(test_dynamic_timer_control);
UTEST_UNIT_RUN(test_dynamic_timer_start_twice);
UTEST_UNIT_RUN(test_dynamic_timer_op_in_callback);
2021-08-23 12:37:54 +08:00
#endif /* RT_USING_HEAP */
UTEST_UNIT_RUN(test_timer_stress);
2021-08-23 12:37:54 +08:00
}
UTEST_TC_EXPORT(testcase, "testcases.kernel.timer_tc", utest_tc_init, utest_tc_cleanup, 1000);
/*********************** end of file ****************************/