libamxp  1.4.0
Patterns C Implementation
test_connection.c File Reference
#include <sys/time.h>
#include <sys/resource.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <inttypes.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <cmocka.h>
#include <amxc/amxc_macros.h>
#include <amxc/amxc.h>
#include <amxp/amxp_signal.h>
#include <amxp/amxp_slot.h>
#include <amxp/amxp_connection.h>
#include "test_connection.h"

Go to the source code of this file.

Functions

static void reader (UNUSED int fd, UNUSED void *priv)
 
static void writer (UNUSED int fd, UNUSED void *priv)
 
static void connection_check_signal (const char *const sig_name, const amxc_var_t *const data, UNUSED void *const priv)
 
int test_connection_setup (UNUSED void **state)
 
int test_connection_teardown (UNUSED void **state)
 
void test_can_add_connection (UNUSED void **state)
 
void test_can_fetch_connection (UNUSED void **state)
 
void test_can_add_wait_write_connection (UNUSED void **state)
 
void test_can_store_event_loop_data (UNUSED void **state)
 
void test_can_iterate_connections (UNUSED void **state)
 
void test_can_remove_connection (UNUSED void **state)
 
void test_can_add_listener (UNUSED void **state)
 
void test_can_fetch_listener (UNUSED void **state)
 
void test_can_remove_listener (UNUSED void **state)
 

Variables

static int fds [2]
 
static char * el_data = "Some Data"
 

Function Documentation

◆ connection_check_signal()

static void connection_check_signal ( const char *const  sig_name,
const amxc_var_t *const  data,
UNUSED void *const  priv 
)
static

Definition at line 88 of file test_connection.c.

90  {
91  uint32_t data_type = amxc_var_type_of(data);
92  uint32_t data_value = GET_UINT32(data, NULL);
93 
94  check_expected(sig_name);
95  check_expected(data_type);
96  check_expected(data_value);
97 }

◆ reader()

static void reader ( UNUSED int  fd,
UNUSED void *  priv 
)
static

Definition at line 80 of file test_connection.c.

80  {
81 
82 }

◆ test_can_add_connection()

void test_can_add_connection ( UNUSED void **  state)

Definition at line 121 of file test_connection.c.

121  {
122  expect_string(connection_check_signal, sig_name, "connection-added");
123  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
124  expect_value(connection_check_signal, data_value, fds[0]);
125 
126  assert_int_equal(amxp_connection_add(fds[0], reader, "test://localhost:1700", AMXP_CONNECTION_CUSTOM, NULL), 0);
127  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 1);
128  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
129 }
#define AMXP_CONNECTION_CUSTOM
int amxp_connection_add(int fd, amxp_fd_cb_t reader, const char *uri, uint32_t type, void *priv)
Adds a file descriptor (fd) to the list of fds that must be watched.
amxc_llist_t * amxp_connection_get_listeners(void)
Get of the current listen sockets of the application.
amxc_llist_t * amxp_connection_get_connections(void)
Get a list of the current connections of the application.
static void reader(UNUSED int fd, UNUSED void *priv)
static int fds[2]
static void connection_check_signal(const char *const sig_name, const amxc_var_t *const data, UNUSED void *const priv)

◆ test_can_add_listener()

void test_can_add_listener ( UNUSED void **  state)

Definition at line 203 of file test_connection.c.

203  {
204  expect_string(connection_check_signal, sig_name, "listen-added");
205  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
206  expect_value(connection_check_signal, data_value, fds[0]);
207 
208  assert_int_equal(amxp_connection_add(fds[0], reader, "test://localhost:1700", AMXP_CONNECTION_LISTEN, NULL), 0);
209  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 0);
210  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 1);
211 }
#define AMXP_CONNECTION_LISTEN

◆ test_can_add_wait_write_connection()

void test_can_add_wait_write_connection ( UNUSED void **  state)

Definition at line 141 of file test_connection.c.

141  {
142  expect_string(connection_check_signal, sig_name, "connection-wait-write");
143  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
144  expect_value(connection_check_signal, data_value, fds[0]);
145 
146  assert_int_equal(amxp_connection_wait_write(fds[0], writer), 0);
147  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 1);
148  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
149 }
int amxp_connection_wait_write(int fd, amxp_fd_cb_t can_write_cb)
Adds a watcher to check if a fd is ready for write.
static void writer(UNUSED int fd, UNUSED void *priv)

◆ test_can_fetch_connection()

void test_can_fetch_connection ( UNUSED void **  state)

Definition at line 131 of file test_connection.c.

131  {
133 
134  assert_non_null(con);
135  assert_int_equal(con->fd, fds[0]);
136  assert_ptr_equal(con->reader, reader);
137  assert_string_equal(con->uri, "test://localhost:1700");
138  assert_int_equal(con->type, AMXP_CONNECTION_CUSTOM);
139 }
amxp_connection_t * amxp_connection_get(int fd)
Gets the connection data for a file descriptor.
amxp_fd_cb_t reader

◆ test_can_fetch_listener()

void test_can_fetch_listener ( UNUSED void **  state)

Definition at line 213 of file test_connection.c.

213  {
215 
216  assert_non_null(con);
217  assert_int_equal(con->fd, fds[0]);
218  assert_ptr_equal(con->reader, reader);
219  assert_string_equal(con->uri, "test://localhost:1700");
220  assert_int_equal(con->type, AMXP_CONNECTION_LISTEN);
221 }

◆ test_can_iterate_connections()

void test_can_iterate_connections ( UNUSED void **  state)

Definition at line 159 of file test_connection.c.

159  {
160  amxp_connection_t* con = NULL;
161 
162  expect_string(connection_check_signal, sig_name, "connection-added");
163  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
164  expect_value(connection_check_signal, data_value, fds[1]);
165 
166  assert_int_equal(amxp_connection_add(fds[1], reader, "test://localhost:1700", AMXP_CONNECTION_BUS, NULL), 0);
167  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 2);
168  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
169 
171  assert_non_null(con);
172  assert_int_equal(con->fd, fds[0]);
173  assert_ptr_equal(con->reader, reader);
174  assert_string_equal(con->uri, "test://localhost:1700");
175  assert_int_equal(con->type, AMXP_CONNECTION_CUSTOM);
176  assert_non_null(con);
178  assert_null(con);
179 }
#define AMXP_CONNECTION_BUS
amxp_connection_t * amxp_connection_get_next(amxp_connection_t *con, uint32_t type)
Gets the next connection data for a file descriptor.
amxp_connection_t * amxp_connection_get_first(uint32_t type)
Gets the first connection of the given type.

◆ test_can_remove_connection()

void test_can_remove_connection ( UNUSED void **  state)

Definition at line 181 of file test_connection.c.

181  {
182  expect_string(connection_check_signal, sig_name, "connection-deleted");
183  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
184  expect_value(connection_check_signal, data_value, fds[0]);
185 
186  assert_int_equal(amxp_connection_remove(fds[0]), 0);
187  assert_null(amxp_connection_get(fds[0]));
188 
189  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 1);
190  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
191 
192  expect_string(connection_check_signal, sig_name, "connection-deleted");
193  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
194  expect_value(connection_check_signal, data_value, fds[1]);
195 
196  assert_int_equal(amxp_connection_remove(fds[1]), 0);
197  assert_null(amxp_connection_get(fds[1]));
198 
199  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 0);
200  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
201 }
int amxp_connection_remove(int fd)
Removes the fd from the connection list.

◆ test_can_remove_listener()

void test_can_remove_listener ( UNUSED void **  state)

Definition at line 223 of file test_connection.c.

223  {
224  expect_string(connection_check_signal, sig_name, "listen-deleted");
225  expect_value(connection_check_signal, data_type, AMXC_VAR_ID_FD);
226  expect_value(connection_check_signal, data_value, fds[0]);
227 
228  assert_int_equal(amxp_connection_remove(fds[0]), 0);
229  assert_null(amxp_connection_get(fds[0]));
230 
231  assert_int_equal(amxc_llist_size(amxp_connection_get_connections()), 0);
232  assert_int_equal(amxc_llist_size(amxp_connection_get_listeners()), 0);
233 }

◆ test_can_store_event_loop_data()

void test_can_store_event_loop_data ( UNUSED void **  state)

Definition at line 151 of file test_connection.c.

151  {
153  assert_int_equal(amxp_connection_set_el_data(fds[0], el_data), 0);
154  assert_int_not_equal(amxp_connection_set_el_data(fds[1], el_data), 0);
155 
156  assert_ptr_equal(con->el_data, el_data);
157 }
int amxp_connection_set_el_data(int fd, void *el_data)
Sets event-loop data.
static char * el_data

◆ test_connection_setup()

int test_connection_setup ( UNUSED void **  state)

Definition at line 99 of file test_connection.c.

99  {
100  assert_int_equal(pipe(fds), 0);
101 
102  while(amxp_signal_read() == 0) {
103  }
104 
105  amxp_slot_connect(NULL, "connection-added", NULL, connection_check_signal, NULL);
106  amxp_slot_connect(NULL, "connection-deleted", NULL, connection_check_signal, NULL);
107  amxp_slot_connect(NULL, "listen-added", NULL, connection_check_signal, NULL);
108  amxp_slot_connect(NULL, "listen-deleted", NULL, connection_check_signal, NULL);
109  amxp_slot_connect(NULL, "connection-wait-write", NULL, connection_check_signal, NULL);
110 
111  return 0;
112 }
int amxp_signal_read(void)
Reads from the amxp signal file descriptor.
Definition: amxp_signal.c:769
int amxp_slot_connect(amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, const char *const expression, amxp_slot_fn_t fn, void *const priv)
Connects a slot (function) to a named signal of a signal manager.
Definition: amxp_slot.c:300

◆ test_connection_teardown()

int test_connection_teardown ( UNUSED void **  state)

Definition at line 114 of file test_connection.c.

114  {
116  close(fds[0]);
117  close(fds[1]);
118  return 0;
119 }
void amxp_slot_disconnect_all(amxp_slot_fn_t fn)
Disconnects a slot from all signals it was connected to.
Definition: amxp_slot.c:459

◆ writer()

static void writer ( UNUSED int  fd,
UNUSED void *  priv 
)
static

Definition at line 84 of file test_connection.c.

84  {
85 
86 }

Variable Documentation

◆ el_data

char* el_data = "Some Data"
static

Definition at line 78 of file test_connection.c.

◆ fds

int fds[2]
static

Definition at line 77 of file test_connection.c.