ReactOS 0.4.16-dev-257-g6aa11ac
test_timers.c
Go to the documentation of this file.
1#include "test_timers.h"
2
3#include "lwip/def.h"
4#include "lwip/timeouts.h"
5#include "arch/sys_arch.h"
6
7/* Setups/teardown functions */
8
9static struct sys_timeo* old_list_head;
10
11static void
13{
14 struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
16 *list_head = NULL;
17}
18
19static void
21{
22 struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
24 lwip_sys_now = 0;
25}
26
27static int fired[3];
28static void
30{
32 fired[index] = 1;
33}
34
35#define HANDLER_EXECUTION_TIME 5
36static int cyclic_fired;
37static void
39{
40 cyclic_fired = 1;
42}
43
45
46static void
48{
49 struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
50
51 /* verify normal timer expiration */
52 lwip_sys_now = offset + 0;
53 sys_timeout(test_cyclic.interval_ms, lwip_cyclic_timer, &test_cyclic);
54
55 cyclic_fired = 0;
56 sys_check_timeouts();
57 fail_unless(cyclic_fired == 0);
58
59 lwip_sys_now = offset + test_cyclic.interval_ms;
60 sys_check_timeouts();
61 fail_unless(cyclic_fired == 1);
62
63 fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + test_cyclic.interval_ms - HANDLER_EXECUTION_TIME));
64
65 sys_untimeout(lwip_cyclic_timer, &test_cyclic);
66
67
68 /* verify "overload" - next cyclic timer execution is already overdue twice */
69 lwip_sys_now = offset + 0;
70 sys_timeout(test_cyclic.interval_ms, lwip_cyclic_timer, &test_cyclic);
71
72 cyclic_fired = 0;
73 sys_check_timeouts();
74 fail_unless(cyclic_fired == 0);
75
76 lwip_sys_now = offset + 2*test_cyclic.interval_ms;
77 sys_check_timeouts();
78 fail_unless(cyclic_fired == 1);
79
80 fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + test_cyclic.interval_ms));
81}
82
83START_TEST(test_cyclic_timers)
84{
86
87 /* check without u32_t wraparound */
89
90 /* check with u32_t wraparound */
91 do_test_cyclic_timers(0xfffffff0);
92}
93END_TEST
94
95/* reproduce bug #52748: the bug in timeouts.c */
96START_TEST(test_bug52748)
97{
99
100 memset(&fired, 0, sizeof(fired));
101
102 lwip_sys_now = 50;
103 sys_timeout(20, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
104 sys_timeout( 5, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2));
105
106 lwip_sys_now = 55;
107 sys_check_timeouts();
108 fail_unless(fired[0] == 0);
109 fail_unless(fired[1] == 0);
110 fail_unless(fired[2] == 1);
111
112 lwip_sys_now = 60;
113 sys_timeout(10, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1));
114 sys_check_timeouts();
115 fail_unless(fired[0] == 0);
116 fail_unless(fired[1] == 0);
117 fail_unless(fired[2] == 1);
118
119 lwip_sys_now = 70;
120 sys_check_timeouts();
121 fail_unless(fired[0] == 1);
122 fail_unless(fired[1] == 1);
123 fail_unless(fired[2] == 1);
124}
125END_TEST
126
127static void
129{
130 struct sys_timeo** list_head = sys_timeouts_get_next_timeout();
131
132 lwip_sys_now = offset + 0;
133
134 sys_timeout(10, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
135 fail_unless(sys_timeouts_sleeptime() == 10);
136 sys_timeout(20, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1));
137 fail_unless(sys_timeouts_sleeptime() == 10);
138 sys_timeout( 5, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2));
139 fail_unless(sys_timeouts_sleeptime() == 5);
140
141 /* linked list correctly sorted? */
142 fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + 5));
143 fail_unless((*list_head)->next->time == (u32_t)(lwip_sys_now + 10));
144 fail_unless((*list_head)->next->next->time == (u32_t)(lwip_sys_now + 20));
145
146 /* check timers expire in correct order */
147 memset(&fired, 0, sizeof(fired));
148
149 lwip_sys_now += 4;
150 sys_check_timeouts();
151 fail_unless(fired[2] == 0);
152
153 lwip_sys_now += 1;
154 sys_check_timeouts();
155 fail_unless(fired[2] == 1);
156
157 lwip_sys_now += 4;
158 sys_check_timeouts();
159 fail_unless(fired[0] == 0);
160
161 lwip_sys_now += 1;
162 sys_check_timeouts();
163 fail_unless(fired[0] == 1);
164
165 lwip_sys_now += 9;
166 sys_check_timeouts();
167 fail_unless(fired[1] == 0);
168
169 lwip_sys_now += 1;
170 sys_check_timeouts();
171 fail_unless(fired[1] == 1);
172
173 sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
174 sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1));
175 sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2));
176}
177
179{
180 LWIP_UNUSED_ARG(_i);
181
182 /* check without u32_t wraparound */
184
185 /* check with u32_t wraparound */
186 do_test_timers(0xfffffff0);
187}
188END_TEST
189
190START_TEST(test_long_timer)
191{
192 LWIP_UNUSED_ARG(_i);
193
194 memset(&fired, 0, sizeof(fired));
195 lwip_sys_now = 0;
196
197 sys_timeout(LWIP_UINT32_MAX / 4, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
198 fail_unless(sys_timeouts_sleeptime() == LWIP_UINT32_MAX / 4);
199
200 sys_check_timeouts();
201 fail_unless(fired[0] == 0);
202
204
205 sys_check_timeouts();
206 fail_unless(fired[0] == 0);
207
209
210 sys_check_timeouts();
211 fail_unless(fired[0] == 0);
212
213 lwip_sys_now += 1;
214
215 sys_check_timeouts();
216 fail_unless(fired[0] == 1);
217
218 sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0));
219}
220END_TEST
221
223Suite *
225{
226 testfunc tests[] = {
227 TESTFUNC(test_bug52748),
228 TESTFUNC(test_cyclic_timers),
230 TESTFUNC(test_long_timer),
231 };
233}
#define START_TEST(x)
Definition: atltest.h:75
#define index(s, c)
Definition: various.h:29
#define LWIP_ARRAYSIZE(x)
Definition: def.h:69
#define NULL
Definition: types.h:112
GLuint index
Definition: glext.h:6031
GLintptr offset
Definition: glext.h:5920
uint32_t u32_t
Definition: arch.h:129
#define LWIP_PTR_NUMERIC_CAST(target_type, val)
Definition: arch.h:252
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:373
#define LWIP_UINT32_MAX
Definition: arch.h:207
u32_t lwip_sys_now
Definition: sys_arch.c:45
Suite * create_suite(const char *name, testfunc *tests, size_t num_tests, SFun setup, SFun teardown)
#define TESTFUNC(x)
Definition: lwip_check.h:22
static struct test_info tests[]
static void test_timers(void)
Definition: msg.c:10249
#define memset(x, y, z)
Definition: compat.h:39
Definition: list.h:15
static int cyclic_fired
Definition: test_timers.c:36
static void dummy_cyclic_handler(void)
Definition: test_timers.c:38
static void timers_setup(void)
Definition: test_timers.c:12
static void do_test_cyclic_timers(u32_t offset)
Definition: test_timers.c:47
static void timers_teardown(void)
Definition: test_timers.c:20
struct lwip_cyclic_timer test_cyclic
Definition: test_timers.c:44
static int fired[3]
Definition: test_timers.c:27
#define HANDLER_EXECUTION_TIME
Definition: test_timers.c:35
static void dummy_handler(void *arg)
Definition: test_timers.c:29
static struct sys_timeo * old_list_head
Definition: test_timers.c:9
END_TEST Suite * timers_suite(void)
Definition: test_timers.c:224
static END_TEST void do_test_timers(u32_t offset)
Definition: test_timers.c:128