libamxrt  0.4.2
Ambiorix Run Time Library
All Data Structures Files Functions Variables Typedefs Macros Modules Pages
test_amxrt_eloop.h File Reference
#include <amxrt/amxrt.h>
#include <amxc/amxc.h>
#include <amxp/amxp.h>
#include <amxd/amxd_object.h>
#include <amxo/amxo.h>

Go to the source code of this file.

Functions

int test_amxrt_el_setup (UNUSED void **state)
 
int test_amxrt_el_teardown (UNUSED void **state)
 
void test_eventloop_is_working (UNUSED void **state)
 
void test_can_add_fds (UNUSED void **state)
 
void test_can_add_fds_before_el_start (UNUSED void **state)
 
void test_can_wait_ready_write (UNUSED void **state)
 
void test_can_remove_fds (UNUSED void **state)
 
void test_can_destroy_el_without_create (UNUSED void **state)
 
void test_runtime_run_creates_el (UNUSED void **state)
 
void test_eventloop_cbs (UNUSED void **state)
 
void test_eventloop_event_base_new_fails (UNUSED void **state)
 

Function Documentation

◆ test_amxrt_el_setup()

int test_amxrt_el_setup ( UNUSED void **  state)

Definition at line 142 of file test_amxrt_eloop.c.

142  {
143  amxc_llist_init(&functions);
144  return 0;
145 }
static amxc_llist_t functions

◆ test_amxrt_el_teardown()

int test_amxrt_el_teardown ( UNUSED void **  state)

Definition at line 147 of file test_amxrt_eloop.c.

147  {
148  amxd_dm_t* dm = amxrt_get_dm();
149  amxc_llist_clean(&functions, remove_cb_fns);
150 
151  amxd_dm_clean(dm);
152  return 0;
153 }
amxd_dm_t * amxrt_get_dm(void)
Gets the runtime data model storage.
Definition: amxrt.c:309
static void remove_cb_fns(amxc_llist_it_t *it)

◆ test_can_add_fds()

void test_can_add_fds ( UNUSED void **  state)

Definition at line 240 of file test_amxrt_eloop.c.

240  {
241  amxp_timer_t* timer = NULL;
242  amxp_timer_t* timer_write = NULL;
243  int data_pipe[2];
244  int retval = pipe(data_pipe);
245  assert_int_equal(retval, 0);
246 
247  will_return_always(__wrap_event_base_new, 0);
248  will_return_always(__wrap_event_new, 0);
249  will_return_always(__wrap_event_add, 0);
250  will_return_always(__wrap_event_base_dispatch, 0);
251 
252  amxp_sigmngr_add_signal(NULL, "connection-added");
253  amxp_sigmngr_add_signal(NULL, "connection-deleted");
254 
255  amxp_timer_new(&timer, test_add_fd, data_pipe);
256  amxp_timer_start(timer, 1000);
257 
258  amxp_timer_new(&timer_write, test_write_fd, data_pipe + 1);
259  amxp_timer_set_interval(timer_write, 2000);
260  amxp_timer_start(timer_write, 1000);
261 
262  assert_int_equal(amxrt_el_create(), 0);
263  assert_int_equal(amxrt_el_start(), 0);
264 
265  assert_non_null(amxrt_el_get_connection(amxp_connection_get_connections(), data_pipe[0]));
266  assert_null(amxrt_el_get_connection(amxp_connection_get_connections(), data_pipe[1]));
267 
268  assert_int_equal(amxrt_el_destroy(), 0);
269 
270  amxp_timer_delete(&timer);
271  amxp_timer_delete(&timer_write);
272 
273  amxp_connection_remove(data_pipe[0]);
274  amxp_connection_remove(data_pipe[1]);
275 
276  close(data_pipe[0]);
277  close(data_pipe[1]);
278 }
PRIVATE amxp_connection_t * amxrt_el_get_connection(amxc_llist_t *cons, int fd)
int amxrt_el_destroy(void)
Cleans-up the event loop components.
int amxrt_el_start(void)
Starts the event loop.
int amxrt_el_create(void)
Creates and initializes all needed event loop components.
static void test_add_fd(UNUSED amxp_timer_t *timer, void *priv)
static void test_write_fd(UNUSED amxp_timer_t *timer, void *priv)
int __wrap_event_add(struct event *ev, const struct timeval *timeout)
struct event_base * __wrap_event_base_new(void)
int __wrap_event_new(struct event_base *base, evutil_socket_t fd, short events, event_callback_fn callback, void *callback_arg)
int __wrap_event_base_dispatch(struct event_base *base)

◆ test_can_add_fds_before_el_start()

void test_can_add_fds_before_el_start ( UNUSED void **  state)

Definition at line 280 of file test_amxrt_eloop.c.

280  {
281  amxp_timer_t* timer_write = NULL;
282  int data_pipe[2];
283  int retval = pipe(data_pipe);
284  assert_int_equal(retval, 0);
285 
286  will_return_always(__wrap_event_base_new, 0);
287  will_return_always(__wrap_event_new, 0);
288  will_return_always(__wrap_event_add, 0);
289  will_return_always(__wrap_event_base_dispatch, 0);
290 
291  amxp_sigmngr_add_signal(NULL, "connection-added");
292  amxp_sigmngr_add_signal(NULL, "connection-deleted");
293 
294  assert_int_equal(amxrt_el_create(), 0);
295 
296  amxp_timer_new(&timer_write, test_write_fd, data_pipe + 1);
297  amxp_timer_set_interval(timer_write, 2000);
298  amxp_timer_start(timer_write, 1000);
299 
300  printf("Adding connection\n");
301  amxp_connection_add(data_pipe[0], test_reader, "test://127.0.0.1:5001", AMXO_CUSTOM, NULL);
302 
303  assert_int_equal(amxrt_el_start(), 0);
304 
305  assert_non_null(amxrt_el_get_connection(amxp_connection_get_connections(), data_pipe[0]));
306  assert_null(amxrt_el_get_connection(amxp_connection_get_connections(), data_pipe[1]));
307 
308  assert_int_equal(amxrt_el_destroy(), 0);
309 
310  amxp_timer_delete(&timer_write);
311 
312  amxp_connection_remove(data_pipe[0]);
313  amxp_connection_remove(data_pipe[1]);
314 
315  close(data_pipe[0]);
316  close(data_pipe[1]);
317 }
static void test_reader(int fd, UNUSED void *priv)

◆ test_can_destroy_el_without_create()

void test_can_destroy_el_without_create ( UNUSED void **  state)

Definition at line 414 of file test_amxrt_eloop.c.

414  {
415  assert_int_equal(amxrt_el_destroy(), 0);
416 }

◆ test_can_remove_fds()

void test_can_remove_fds ( UNUSED void **  state)

Definition at line 381 of file test_amxrt_eloop.c.

381  {
382  amxp_timer_t* timer = NULL;
383  int data_pipe[2];
384  int retval = pipe(data_pipe);
385  assert_int_equal(retval, 0);
386 
387  will_return_always(__wrap_event_base_new, 0);
388  will_return_always(__wrap_event_new, 0);
389  will_return_always(__wrap_event_add, 0);
390  will_return_always(__wrap_event_base_dispatch, 0);
391 
392  amxp_sigmngr_add_signal(NULL, "connection-added");
393  amxp_sigmngr_add_signal(NULL, "connection-deleted");
394 
395  amxp_timer_new(&timer, test_remove_fd, data_pipe);
396  amxp_timer_start(timer, 1000);
397 
398  amxp_connection_add(data_pipe[0], test_reader, "test://127.0.0.1:5001", AMXO_CUSTOM, NULL);
399 
400  assert_int_equal(amxrt_el_create(), 0);
401  assert_int_equal(amxrt_el_start(), 0);
402 
403  assert_null(amxrt_el_get_connection(amxp_connection_get_connections(), data_pipe[0]));
404  assert_null(amxrt_el_get_connection(amxp_connection_get_connections(), data_pipe[1]));
405 
406  assert_int_equal(amxrt_el_destroy(), 0);
407 
408  amxp_timer_delete(&timer);
409 
410  close(data_pipe[0]);
411  close(data_pipe[1]);
412 }
static void test_remove_fd(UNUSED amxp_timer_t *timer, void *priv)

◆ test_can_wait_ready_write()

void test_can_wait_ready_write ( UNUSED void **  state)

Definition at line 337 of file test_amxrt_eloop.c.

337  {
338  int data_pipe[2];
339  int retval = pipe(data_pipe);
340  assert_int_equal(retval, 0);
341  amxp_timer_t* timer = NULL;
342 
343  will_return_always(__wrap_event_base_new, 0);
344  will_return_always(__wrap_event_new, 0);
345  will_return_always(__wrap_event_add, 0);
346  will_return_always(__wrap_event_base_dispatch, 0);
347 
348  amxp_sigmngr_add_signal(NULL, "connection-wait-write");
349 
350  amxp_connection_add(data_pipe[0], test_reader, "test://127.0.0.1:5001", AMXO_CUSTOM, NULL);
351  amxp_connection_add(data_pipe[1], test_reader, "test://127.0.0.1:5001", AMXO_CUSTOM, NULL);
352 
353  amxp_timer_new(&timer, test_add_wait_for_write, data_pipe + 1);
354  amxp_timer_start(timer, 1000);
355 
356  assert_int_equal(amxrt_el_create(), 0);
357  assert_int_equal(amxrt_el_start(), 0);
358 
359  assert_non_null(amxrt_el_get_connection(amxp_connection_get_connections(), data_pipe[0]));
360  assert_non_null(amxrt_el_get_connection(amxp_connection_get_connections(), data_pipe[1]));
361 
362  assert_int_equal(amxrt_el_destroy(), 0);
363 
364  amxp_connection_remove(data_pipe[0]);
365  amxp_connection_remove(data_pipe[1]);
366 
367  close(data_pipe[0]);
368  close(data_pipe[1]);
369 }
static void test_add_wait_for_write(UNUSED amxp_timer_t *timer, void *priv)

◆ test_eventloop_cbs()

void test_eventloop_cbs ( UNUSED void **  state)

Definition at line 435 of file test_amxrt_eloop.c.

435  {
436  will_return_always(__wrap_event_base_new, 0);
437  will_return_always(__wrap_event_new, 1);
438  will_return_always(__wrap_event_add, 1);
439 
440  assert_int_equal(amxrt_el_create(), 0);
441  will_return(__wrap_event_base_dispatch, 1);
442  assert_int_equal(amxrt_el_start(), 0);
443  will_return(__wrap_event_base_dispatch, 2);
444  assert_int_equal(amxrt_el_start(), 0);
445 
446  assert_int_equal(amxrt_el_destroy(), 0);
447 }

◆ test_eventloop_event_base_new_fails()

void test_eventloop_event_base_new_fails ( UNUSED void **  state)

Definition at line 449 of file test_amxrt_eloop.c.

449  {
450  will_return_always(__wrap_event_base_new, 1);
451  assert_int_not_equal(amxrt_el_create(), 0);
452  assert_int_not_equal(amxrt_el_start(), 0);
453  assert_int_not_equal(amxrt_el_stop(), 0);
454  assert_int_equal(amxrt_el_destroy(), 0);
455 }
int amxrt_el_stop(void)
Stops the event loop.

◆ test_eventloop_is_working()

void test_eventloop_is_working ( UNUSED void **  state)

Definition at line 171 of file test_amxrt_eloop.c.

171  {
172  amxp_timer_t* timer = NULL;
173  int counter = 0;
174 
175  will_return_always(__wrap_event_base_new, 0);
176  will_return_always(__wrap_event_new, 0);
177  will_return_always(__wrap_event_add, 0);
178  will_return_always(__wrap_event_base_dispatch, 0);
179 
180  amxp_sigmngr_add_signal(NULL, "AMXP-TEST-SIGNAL");
181  amxp_slot_connect(NULL, "AMXP-TEST-SIGNAL", NULL, test_amxp_signal, &counter);
182  amxp_sigmngr_emit_signal(NULL, "AMXP-TEST-SIGNAL", NULL);
183 
184  amxp_timer_new(&timer, test_stop_eventloop, &counter);
185  amxp_timer_start(timer, 1000);
186 
187  printf("Create eventloop\n");
188  fflush(stdout);
189  assert_int_equal(amxrt_el_create(), 0);
190  printf("Start eventloop\n");
191  fflush(stdout);
192  assert_int_equal(amxrt_el_start(), 0);
193  printf("eventloop stopped - because of AMXP-TEST-SIGNAL\n");
194  fflush(stdout);
195  assert_int_equal(counter, 1);
196  printf("Start eventloop\n");
197  fflush(stdout);
198  assert_int_equal(amxrt_el_start(), 0);
199  printf("eventloop stopped - because of timer\n");
200  fflush(stdout);
201  assert_int_equal(counter, 2);
202  assert_int_equal(amxrt_el_destroy(), 0);
203 
204  amxp_timer_delete(&timer);
205 }
static void test_stop_eventloop(UNUSED amxp_timer_t *timer, void *priv)
static void test_amxp_signal(UNUSED const char *const sig_name, UNUSED const amxc_var_t *const data, void *const priv)

◆ test_runtime_run_creates_el()

void test_runtime_run_creates_el ( UNUSED void **  state)

Definition at line 418 of file test_amxrt_eloop.c.

418  {
419  amxp_timer_t* timer = NULL;
420  int counter = 0;
421 
422  amxp_timer_new(&timer, test_stop_eventloop, &counter);
423  amxp_timer_start(timer, 1000);
424 
425  will_return_always(__wrap_event_base_new, 0);
426  will_return_always(__wrap_event_new, 0);
427  will_return_always(__wrap_event_add, 0);
428  will_return_always(__wrap_event_base_dispatch, 0);
429 
430  assert_int_equal(amxrt_el_create(), 0);
431  assert_int_equal(amxrt_run(), 0);
432  assert_int_equal(amxrt_el_destroy(), 0);
433 }
int amxrt_run(void)
Starts the event loop.
Definition: amxrt.c:425