libamxp  1.4.0
Patterns C Implementation
test_timer.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #include <sys/signalfd.h>
55 #include <signal.h>
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59 
60 
61 #include <stdarg.h>
62 #include <stddef.h>
63 #include <setjmp.h>
64 #include <fcntl.h>
65 #include <cmocka.h>
66 
67 #include <amxc/amxc.h>
68 #include <amxc/amxc_timestamp.h>
69 #include <amxp/amxp_timer.h>
70 
71 #include "test_timer.h"
72 
73 #include <amxc/amxc_macros.h>
74 
75 static amxp_timer_t* t1 = NULL;
76 static amxp_timer_t* t2 = NULL;
77 
78 static void timer_delete_timers_cb(UNUSED amxp_timer_t* timer, UNUSED void* priv) {
81 }
82 
83 static void timer_delete_add_timers_cb(UNUSED amxp_timer_t* timer, UNUSED void* priv) {
86  amxp_timer_start(t1, 1000);
87 }
88 
89 static void timer_callback(amxp_timer_t* timer, void* priv) {
90  check_expected(timer);
91  check_expected(priv);
92 
93  assert_ptr_not_equal(timer, NULL);
94 }
95 
96 static void read_sigalrm(void) {
97  sigset_t mask;
98  int sfd;
99  struct signalfd_siginfo fdsi;
100  ssize_t s;
101 
102  sigemptyset(&mask);
103  sigaddset(&mask, SIGALRM);
104 
105  sigprocmask(SIG_BLOCK, &mask, NULL);
106 
107  sfd = signalfd(-1, &mask, 0);
108  s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
109  assert_int_equal(s, sizeof(struct signalfd_siginfo));
110  if(fdsi.ssi_signo == SIGALRM) {
111  printf("Got SIGALRM\n");
112  } else {
113  printf("Read unexpected signal\n");
114  }
115 }
116 
117 void test_can_create_timer(UNUSED void** state) {
118  amxp_timer_t* timer = NULL;
119 
120  assert_int_not_equal(amxp_timer_new(NULL, NULL, NULL), 0);
121  amxp_timer_delete(NULL);
122 
123  assert_int_equal(amxp_timer_new(&timer, NULL, NULL), 0);
124  assert_ptr_not_equal(timer, NULL);
125  assert_int_equal(timer->state, amxp_timer_off);
126  assert_ptr_equal(timer->cb, NULL);
127  assert_ptr_equal(timer->priv, NULL);
128 
129  assert_int_not_equal(amxp_timer_new(&timer, NULL, NULL), 0);
130 
131  amxp_timer_delete(&timer);
132  assert_ptr_equal(timer, NULL);
133  amxp_timer_delete(&timer);
134 }
135 
136 void test_can_start_timers(UNUSED void** state) {
137  amxp_timer_t* timer1 = NULL;
138  amxp_timer_t* timer2 = NULL;
139  amxc_ts_t ts1;
140  amxc_ts_t ts2;
141 
142  assert_int_equal(amxp_timer_new(&timer1, NULL, NULL), 0);
143  assert_ptr_not_equal(timer1, NULL);
144  assert_int_equal(amxp_timer_new(&timer2, NULL, NULL), 0);
145  assert_ptr_not_equal(timer2, NULL);
146 
147  assert_int_equal(amxp_timer_start(timer1, 1000), 0);
148  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_running);
150  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_running);
151 
152  assert_int_equal(amxp_timer_start(timer2, 3000), 0);
153  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_running);
155  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_running);
156 
157 
158  amxc_ts_now(&ts1);
159  read_sigalrm();
160  amxc_ts_now(&ts2);
163  assert_int_equal(ts2.sec - ts1.sec, 1);
164  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_off);
165  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_running);
166 
167  read_sigalrm();
168  amxc_ts_now(&ts2);
171  assert_int_equal(ts2.sec - ts1.sec, 3);
172  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_off);
173  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_off);
174 
175  assert_int_not_equal(amxp_timer_start(NULL, 1000), 0);
176 
177  amxp_timer_delete(&timer1);
178  amxp_timer_delete(&timer2);
179 }
180 
182  amxp_timer_t* timer1 = NULL;
183 
184  assert_int_equal(amxp_timer_new(&timer1, timer_callback, NULL), 0);
185  assert_ptr_not_equal(timer1, NULL);
186 
187  assert_int_equal(amxp_timer_start(timer1, 500), 0);
188  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_running);
189 
190  expect_any(timer_callback, timer);
191  expect_any(timer_callback, priv);
192 
193  read_sigalrm();
194 
197 
198  amxp_timer_delete(&timer1);
199 }
200 
202  amxp_timer_t* timer1 = NULL;
203  amxp_timer_t* timer2 = NULL;
204  int count = 11;
205  int remaining = 0;
206 
207  assert_int_equal(amxp_timer_new(&timer1, NULL, NULL), 0);
208  assert_ptr_not_equal(timer1, NULL);
209  assert_int_equal(amxp_timer_new(&timer2, NULL, NULL), 0);
210  assert_ptr_not_equal(timer2, NULL);
211 
212  assert_int_equal(amxp_timer_set_interval(timer1, 1000), 0);
213  assert_int_equal(amxp_timer_start(timer1, 1000), 0);
214  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_running);
216  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_running);
217 
218  assert_int_equal(amxp_timer_start(timer2, 10200), 0);
219  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_running);
221  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_running);
222 
223  while(amxp_timer_get_state(timer2) != amxp_timer_off) {
224  read_sigalrm();
227  count--;
228  if(count > 0) {
229  remaining = amxp_timer_remaining_time(timer2);
230  assert_true((remaining / 1000) <= (count - 1));
231  }
232  }
233 
234  assert_int_equal(count, 0);
235 
236  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_running);
237  assert_int_equal(amxp_timer_stop(timer1), 0);
238  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_off);
239 
240  assert_int_not_equal(amxp_timer_set_interval(NULL, 1000), 0);
241  assert_int_equal(amxp_timer_remaining_time(NULL), 0);
242  assert_int_not_equal(amxp_timer_stop(NULL), 0);
243 
244  amxp_timer_delete(&timer1);
245  amxp_timer_delete(&timer2);
246 }
247 
248 void test_can_restart_timer(UNUSED void** state) {
249  amxc_ts_t start_time;
250  amxc_ts_t end_time;
251 
252  amxp_timer_t* timer = NULL;
253  amxp_timer_new(&timer, NULL, NULL);
254  amxp_timer_start(timer, 3000);
255  sleep(1);
256  amxc_ts_now(&start_time);
257  amxp_timer_start(timer, 3000);
258  read_sigalrm();
259  amxc_ts_now(&end_time);
260  assert_int_equal(end_time.sec - start_time.sec, 3);
261 
262  amxp_timer_delete(&timer);
263 }
264 
266  amxc_ts_t start_time;
267  amxc_ts_t end_time;
268  amxp_timer_t* timer1 = NULL;
269  amxp_timer_t* timer2 = NULL;
270 
271  amxp_timer_new(&timer1, timer_callback, NULL);
272  amxp_timer_new(&timer2, NULL, NULL);
273 
274  expect_any(timer_callback, timer);
275  expect_any(timer_callback, priv);
276 
277  amxp_timer_start(timer1, 0);
278  amxp_timer_start(timer2, 3000);
279  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_running);
280  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_running);
281  amxc_ts_now(&start_time);
282  read_sigalrm();
283  amxc_ts_now(&end_time);
286  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_off);
287  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_running);
288  assert_int_equal(end_time.sec - start_time.sec, 0);
289 
290  amxp_timer_delete(&timer1);
291  amxp_timer_delete(&timer2);
292 }
293 
295  amxc_ts_t start_time;
296  amxc_ts_t end_time;
297  amxp_timer_t* timer1 = NULL;
298  amxp_timer_t* timer2 = NULL;
299 
300  amxp_timer_new(&timer1, timer_callback, NULL);
301  amxp_timer_new(&timer2, NULL, NULL);
302 
303  expect_any(timer_callback, timer);
304  expect_any(timer_callback, priv);
305 
306  amxp_timer_start(timer2, 3000);
307  amxp_timer_start(timer1, 0);
308  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_running);
309  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_running);
310  amxc_ts_now(&start_time);
311  read_sigalrm();
312  amxc_ts_now(&end_time);
315  assert_int_equal(amxp_timer_get_state(timer1), amxp_timer_off);
316  assert_int_equal(amxp_timer_get_state(timer2), amxp_timer_running);
317  assert_int_equal(end_time.sec - start_time.sec, 0);
318 
319  amxp_timer_delete(&timer1);
320  amxp_timer_delete(&timer2);
321 }
322 
326 
327  amxp_timer_start(t1, 3000);
328  amxp_timer_start(t2, 10000);
329 
330  assert_int_equal(amxp_timer_get_state(t1), amxp_timer_running);
331  assert_int_equal(amxp_timer_get_state(t2), amxp_timer_running);
332 
333  read_sigalrm();
336 }
337 
340  amxp_timer_new(&t2, NULL, NULL);
341 
342  amxp_timer_start(t1, 1000);
343 
344  assert_int_equal(amxp_timer_get_state(t1), amxp_timer_running);
345 
346  read_sigalrm();
349 
350  read_sigalrm();
353 }
static sigset_t mask
Definition: amxp_syssig.c:68
Ambiorix timer API header file.
#define UNUSED
Definition: main.c:68
int amxp_timer_start(amxp_timer_t *timer, unsigned int timeout_msec)
Starts or resets a timer.
Definition: amxp_timer.c:296
unsigned int amxp_timer_remaining_time(amxp_timer_t *timer)
Get the remaining time of the timer.
Definition: amxp_timer.c:281
amxp_timer_state_t amxp_timer_get_state(amxp_timer_t *timer)
Get the timer's state.
Definition: amxp_timer.c:334
void amxp_timers_check(void)
Check all timers and call the callback function when the timer is in Timer expired state.
Definition: amxp_timer.c:183
int amxp_timer_stop(amxp_timer_t *timer)
Stops the timer.
Definition: amxp_timer.c:319
void amxp_timers_calculate(void)
Caclulates the remaining time of all timers.
Definition: amxp_timer.c:144
void amxp_timer_delete(amxp_timer_t **timer)
Deletes a timer.
Definition: amxp_timer.c:247
int amxp_timer_new(amxp_timer_t **timer, amxp_timer_cb_t cb, void *priv)
Allocate and initializes a new timer.
Definition: amxp_timer.c:229
int amxp_timer_set_interval(amxp_timer_t *timer, unsigned int msec)
Sets the interval of a timer in milli seconds.
Definition: amxp_timer.c:259
@ amxp_timer_off
Definition: amxp_timer.h:149
@ amxp_timer_running
Definition: amxp_timer.h:151
The timer type.
Definition: amxp_timer.h:163
void * priv
Definition: amxp_timer.h:168
amxp_timer_state_t state
Definition: amxp_timer.h:166
amxp_timer_cb_t cb
Definition: amxp_timer.h:167
int count
Definition: test_syssig.c:80
void test_timer_callback_is_called(UNUSED void **state)
Definition: test_timer.c:181
void test_timer_can_delete_timers_in_callback(UNUSED void **state)
Definition: test_timer.c:323
void test_timer_can_delete_add_timers_in_callback(UNUSED void **state)
Definition: test_timer.c:338
void test_can_start_timers(UNUSED void **state)
Definition: test_timer.c:136
static void timer_delete_timers_cb(UNUSED amxp_timer_t *timer, UNUSED void *priv)
Definition: test_timer.c:78
void test_timer_with_0_timeout_test2(UNUSED void **state)
Definition: test_timer.c:294
static amxp_timer_t * t1
Definition: test_timer.c:75
static void timer_delete_add_timers_cb(UNUSED amxp_timer_t *timer, UNUSED void *priv)
Definition: test_timer.c:83
static void read_sigalrm(void)
Definition: test_timer.c:96
void test_can_start_interval_timer(UNUSED void **state)
Definition: test_timer.c:201
void test_can_create_timer(UNUSED void **state)
Definition: test_timer.c:117
static void timer_callback(amxp_timer_t *timer, void *priv)
Definition: test_timer.c:89
void test_can_restart_timer(UNUSED void **state)
Definition: test_timer.c:248
void test_timer_with_0_timeout_test1(UNUSED void **state)
Definition: test_timer.c:265
static amxp_timer_t * t2
Definition: test_timer.c:76