tests now compile again

pollfd
Karchnu 2020-07-04 11:01:24 +02:00
parent f89ab91917
commit fbb7394f31
5 changed files with 64 additions and 67 deletions

View File

@ -9,7 +9,7 @@
// test the behavior of the server when the client never read its messages
void send_message (struct ipc_connection_info *ci)
void send_message (struct ipc_ctx *ctx)
{
SECURE_DECLARATION (struct ipc_message, m);
SECURE_MALLOC (m.payload, 5, exit(EXIT_FAILURE));
@ -18,23 +18,24 @@ void send_message (struct ipc_connection_info *ci)
m.user_type = 42;
m.length = 5;
ipc_write (ci, &m);
ipc_write_fd (ctx->pollfd[0].fd /* only one connection */, &m);
ipc_message_empty (&m);
}
void read_message (struct ipc_connection_info *ci)
void read_message (struct ipc_ctx *ctx)
{
#if 0
SECURE_DECLARATION(struct ipc_event, event);
SECURE_DECLARATION(struct ipc_ctx, clients);
SECURE_DECLARATION (struct ipc_message, m);
long timer = 10;
int timer = 10000;
TEST_IPC_Q(ipc_read (ci, &m), EXIT_FAILURE);
// ctx, index, message
TEST_IPC_Q(ipc_read (ctx, 0 /* only one server here */, &m), EXIT_FAILURE);
ipc_wait_event (&clients, NULL, &event, &timer);
ipc_events_loop (ctx, &event, &timer);
switch (event.type) {
case IPC_EVENT_TYPE_MESSAGE : {
@ -52,37 +53,34 @@ void read_message (struct ipc_connection_info *ci)
break;
}
ipc_ctx_free (&clients);
ipc_ctx_free (ctx);
#else
SECURE_DECLARATION (struct ipc_message, m);
TEST_IPC_Q(ipc_read (ci, &m), EXIT_FAILURE);
TEST_IPC_Q(ipc_read (ctx, 0 /* only one server here */, &m), EXIT_FAILURE);
printf ("received message: %*.s\n", m.length, m.payload);
free (m.payload);
#endif
}
int main(int argc, char * argv[], char **env)
int main(void)
{
argc = argc;
argv = argv;
SECURE_DECLARATION(struct ipc_connection_info,srv1);
SECURE_DECLARATION(struct ipc_connection_info,srv2);
SECURE_DECLARATION(struct ipc_ctx, ctx1);
SECURE_DECLARATION(struct ipc_ctx, ctx2);
SECURE_DECLARATION(struct ipc_event, event);
TEST_IPC_Q (ipc_connection (env, &srv1, SERVICE_NAME), EXIT_FAILURE);
TEST_IPC_Q (ipc_connection (env, &srv2, SERVICE_NAME), EXIT_FAILURE);
TEST_IPC_Q (ipc_connection (&ctx1, SERVICE_NAME), EXIT_FAILURE);
TEST_IPC_Q (ipc_connection (&ctx2, SERVICE_NAME), EXIT_FAILURE);
send_message (&srv1);
read_message (&srv1);
send_message (&ctx1);
read_message (&ctx1);
TEST_IPC_Q (ipc_close (&srv1), EXIT_FAILURE);
TEST_IPC_Q (ipc_close_all (&ctx1), EXIT_FAILURE);
send_message (&srv2);
read_message (&srv2);
send_message (&ctx2);
read_message (&ctx2);
TEST_IPC_Q (ipc_close (&srv2), EXIT_FAILURE);
TEST_IPC_Q (ipc_close_all (&ctx2), EXIT_FAILURE);
return EXIT_SUCCESS;
}

View File

@ -7,43 +7,46 @@
#define SERVICE_NAME "pong"
int main_loop(int argc, char * argv[], char **env)
int main_loop(int argc, char * argv[])
{
argc = argc;
argv = argv;
SECURE_DECLARATION (struct ipc_connection_info, srv);
long timer = 10;
SECURE_DECLARATION (struct ipc_ctx, ctx);
int timer = 10000; // in ms
printf ("func 03 - server init...\n");
TEST_IPC_Q (ipc_server_init (env, &srv, SERVICE_NAME), EXIT_FAILURE);
TEST_IPC_Q (ipc_server_init (&ctx, SERVICE_NAME), EXIT_FAILURE);
printf ("func 03 - server init ok\n");
SECURE_DECLARATION (struct ipc_ctx, clients);
SECURE_DECLARATION (struct ipc_event, event);
printf ("func 01 - service polling...\n");
// listen only for a single client
while (1) {
TEST_IPC_WAIT_EVENT_Q (ipc_wait_event (&clients, &srv, &event, &timer), EXIT_FAILURE);
TEST_IPC_WAIT_EVENT_Q (ipc_events_loop (&ctx, &event, &timer), EXIT_FAILURE);
switch (event.type) {
case IPC_EVENT_TYPE_TIMER : {
fprintf (stderr, "time up!");
timer = 10;
fprintf (stderr, "time up!\n");
timer = 10000;
}
break;
case IPC_EVENT_TYPE_CONNECTION : {
printf ("connection establishment: %d \n", event.origin->fd);
printf ("connection establishment: %d \n", event.origin);
}
break;
case IPC_EVENT_TYPE_DISCONNECTION : {
printf ("client %d disconnecting\n", event.origin->fd);
printf ("client %d disconnecting\n", event.origin);
};
break;
case IPC_EVENT_TYPE_MESSAGE : {
printf ("received message: %s\n", ((struct ipc_message*) event.m)->payload);
ipc_write (event.origin, (struct ipc_message*) event.m);
ipc_write (&ctx, (struct ipc_message*) event.m);
}
break;
case IPC_EVENT_TYPE_TX : {
printf ("message sent to fd %d\n", event.origin);
}
break;
case IPC_EVENT_TYPE_NOT_SET :
@ -56,10 +59,11 @@ int main_loop(int argc, char * argv[], char **env)
}
}
printf ("func 03 - closing clients...\n");
ipc_ctx_free (&clients);
printf ("func 03 - closing server...\n");
TEST_IPC_Q (ipc_server_close(&srv), EXIT_FAILURE);
TEST_IPC_Q (ipc_close_all(&ctx), EXIT_FAILURE);
printf ("func 03 - freeing the context\n");
ipc_ctx_free (&ctx);
return 0;
}
@ -71,7 +75,7 @@ void exit_program(int signal)
}
int main(int argc, char * argv[], char **env)
int main(int argc, char * argv[])
{
signal (SIGHUP, exit_program);
signal (SIGALRM, exit_program);
@ -79,6 +83,6 @@ int main(int argc, char * argv[], char **env)
signal (SIGUSR2, exit_program);
signal (SIGTERM, exit_program);
signal (SIGINT, exit_program);
main_loop (argc, argv, env);
main_loop (argc, argv);
return EXIT_SUCCESS;
}

View File

@ -9,7 +9,7 @@
// test the behavior of the server when the client never read its messages
void send_message (struct ipc_connection_info *ci)
void send_message (struct ipc_ctx *ctx)
{
SECURE_DECLARATION (struct ipc_message, m);
SECURE_MALLOC (m.payload, 1, exit(EXIT_FAILURE));
@ -17,18 +17,20 @@ void send_message (struct ipc_connection_info *ci)
m.type = MSG_TYPE_DATA;
m.user_type = 42;
m.length = 0;
m.fd = ctx->pollfd[0].fd;
ipc_write (ci, &m);
// ipc_write_fd = write now, without waiting the fd to become available
ipc_write_fd (ctx->pollfd[0].fd, &m);
ipc_message_empty (&m);
}
void read_message (struct ipc_connection_info *ci)
void read_message (struct ipc_ctx *ctx)
{
SECURE_DECLARATION (struct ipc_message, m);
ipc_read (ci, &m);
ipc_read (ctx, 0 /* there is only one valid index */, &m);
if (m.length > 0) {
printf ("received message: %*.s\n", m.length, m.payload);
}
@ -41,19 +43,16 @@ void read_message (struct ipc_connection_info *ci)
free (m.payload);
}
int main(int argc, char * argv[], char **env)
int main(void)
{
argc = argc;
argv = argv;
SECURE_DECLARATION(struct ipc_ctx, ctx);
SECURE_DECLARATION(struct ipc_connection_info,srv1);
TEST_IPC_Q(ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
TEST_IPC_Q(ipc_connection (env, &srv1, SERVICE_NAME), EXIT_FAILURE);
send_message (&ctx);
read_message (&ctx);
send_message (&srv1);
read_message (&srv1);
TEST_IPC_Q(ipc_close (&srv1), EXIT_FAILURE);
TEST_IPC_Q(ipc_close_all (&ctx), EXIT_FAILURE);
return EXIT_SUCCESS;
}

View File

@ -1,4 +1,5 @@
#define _BSD_SOURCE
// #define _BSD_SOURCE
#define _DEFAULT_SOURCE
#include "../src/ipc.h"
@ -12,9 +13,9 @@
#define DEFAULT_MSG "coucou"
int main(int argc, char * argv[], char **env)
int main(int argc, char * argv[])
{
SECURE_DECLARATION(struct ipc_connection_info,srv);
SECURE_DECLARATION(struct ipc_ctx, ctx);
SECURE_DECLARATION(struct ipc_event, event);
SECURE_DECLARATION (struct ipc_message, write_m);
@ -46,20 +47,21 @@ int main(int argc, char * argv[], char **env)
memcpy(message_str, DEFAULT_MSG, strlen(DEFAULT_MSG));
}
TEST_IPC_Q (ipc_connection (env, &srv, SERVICE_NAME), EXIT_FAILURE);
TEST_IPC_Q (ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
SECURE_MALLOC (write_m.payload, strlen(message_str), exit(EXIT_FAILURE));
memcpy (write_m.payload, message_str, strlen(message_str));
write_m.type = MSG_TYPE_DATA;
write_m.user_type = 42;
write_m.fd = ctx.pollfd[0].fd;
write_m.length = strlen(message_str);
gettimeofday(&tval_before, NULL);
for (size_t i = 0 ; i < nb_rounds ; i++) {
ipc_write (&srv, &write_m);
ipc_write_fd (ctx.pollfd[0].fd, &write_m);
// reading
TEST_IPC_Q(ipc_read (&srv, &read_m), EXIT_FAILURE);
TEST_IPC_Q(ipc_read (&ctx, 0 /* only valid index */, &read_m), EXIT_FAILURE);
// printf ("received message (%d bytes): %*s\n", read_m.length, read_m.length, read_m.payload);
// ipc_message_empty (&read_m);
@ -71,7 +73,7 @@ int main(int argc, char * argv[], char **env)
printf("Time elapsed: %ld.%06ld\n", (long int)tval_result.tv_sec, (long int)tval_result.tv_usec);
// disconnection
TEST_IPC_Q (ipc_close (&srv), EXIT_FAILURE);
TEST_IPC_Q (ipc_close_all (&ctx), EXIT_FAILURE);
return EXIT_SUCCESS;
}

View File

@ -5,27 +5,21 @@
#include <string.h>
#define SERVICE_NAME "example"
#define VERSION 0
#define INDEX 0
int main(int argc, char * argv[])
{
char path[PATH_MAX];
char * sname = SERVICE_NAME;
int32_t index = INDEX;
int32_t version = VERSION;
if (argc == 4) {
if (argc == 2) {
sname = argv[1];
index = atoi(argv[2]);
version = atoi(argv[3]);
}
else if (argc != 1) {
fprintf (stderr, "usage: %s [service-name index version]\n", argv[0]);
return EXIT_FAILURE;
}
service_path (path, sname, index, version);
service_path (path, sname);
// printf ("servicename: %s, index: %d, version: %d\n", sname, index, version);
printf ("%s\n", path);