From 14fd9b84b952f388ab4b9b70abb694c08db449d9 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Wed, 7 Sep 2016 23:26:28 +0200 Subject: [PATCH] pubsubd: test on channels - init, add, get, del all --- lib/pubsubd.c | 11 +++- pubsub/Makefile | 9 ++- pubsub/test-chan-lists.c | 120 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 pubsub/test-chan-lists.c diff --git a/lib/pubsubd.c b/lib/pubsubd.c index facc71f..ed7e222 100644 --- a/lib/pubsubd.c +++ b/lib/pubsubd.c @@ -114,7 +114,7 @@ void pubsubd_subscriber_init (struct app_list_head **chans) { void pubsubd_channels_print (const struct channels *chans) { - printf ("\033[36mmchannels\033[00m\n\n"); + printf ("\033[36mmchannels\033[00m\n"); if (chans == NULL) return ; @@ -130,13 +130,19 @@ void pubsubd_channel_print (const struct channel *c) if (c == NULL || c->chan == NULL) return; - printf ( "\033[32mchan %s\033[00m\n\t", c->chan); + if (c->chan == NULL) { + printf ( "\033[32mchan name not available\033[00m\n"); + } + else { + printf ( "\033[32mchan %s\033[00m\n", c->chan); + } if (c->alh == NULL) return; struct app_list_elm *ale = NULL; LIST_FOREACH(ale, c->alh, entries) { + printf ("\t"); srv_process_print (ale->p); } } @@ -361,6 +367,7 @@ int pubsubd_get_new_process (struct service *srv, struct app_list_elm *ale int index = 0; int version = 0; + // chan name char chan[BUFSIZ]; memset (chan, 0, BUFSIZ); diff --git a/pubsub/Makefile b/pubsub/Makefile index 6b95c76..332cf7c 100644 --- a/pubsub/Makefile +++ b/pubsub/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-Wall -g +CFLAGS=-Wall -g -Wextra LDFLAGS= -pthread CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change EXEC=$(basename $(wildcard *.c)) @@ -15,8 +15,11 @@ $(EXEC): $(OBJECTS) $(CFILES) .c.o: $(CC) -c $(CFLAGS) $< -o $@ +$(TESTS): + valgrind --show-leak-kinds=all --leak-check=full -v --track-origins=yes ./$(basename $@) + clean: - -rm $(OBJECTS) + @-rm $(OBJECTS) mrproper: clean - rm $(EXEC) + @-rm $(EXEC) diff --git a/pubsub/test-chan-lists.c b/pubsub/test-chan-lists.c new file mode 100644 index 0000000..fbcada6 --- /dev/null +++ b/pubsub/test-chan-lists.c @@ -0,0 +1,120 @@ +#include "../lib/pubsubd.h" +#include + +#define TEST_NAME "test-chan-lists" + +int +create_chan (struct channel *c, const char * name) { + if (c == NULL) { + return 1; + } + + if (c->chan == NULL) + c->chan = malloc (BUFSIZ); + memset (c->chan, 0, BUFSIZ); + memcpy (c->chan, name, strlen (name)); + c->chanlen = strlen (name); + return 0; +} + +void +ohshit(int rvalue, const char* str) { + fprintf(stderr, "%s\n", str); + exit(rvalue); +} + +int +main(int argc, char **argv, char **env) +{ + struct service srv; + memset (&srv, 0, sizeof (struct service)); + srv_init (argc, argv, env, &srv, TEST_NAME, NULL); + printf ("Listening on %s.\n", srv.spath); + + // creates the service named pipe, that listens to client applications + if (srv_create (&srv)) + ohshit(1, "service_create error"); + + // init chans list + struct channels chans; + memset (&chans, 0, sizeof (struct channels)); + pubsubd_channels_init (&chans); + + // for each new process + // struct app_list_elm ale1; + // memset (&ale1, 0, sizeof (struct app_list_elm)); + + // warning : this is a local structure, not exactly the same in the prog. + struct channel chan; + memset (&chan, 0, sizeof (struct channel)); + create_chan (&chan, "coucou"); + + // to emulate + // pubsubd_get_new_process (&srv, &ale1, &chans, &chan); + + // FIRST CHAN TO BE ADDED + // search for the chan in channels, add it if not found + struct channel *new_chan = NULL; + new_chan = pubsubd_channel_get (&chans, &chan); + if (new_chan == NULL) { + new_chan = pubsubd_channels_add (&chans, &chan); + pubsubd_subscriber_init (&new_chan->alh); + } + else { + ohshit (2, "error : new chan, can't be found in channels yet"); + } + pubsubd_channel_free (&chan); + + printf ("print the channels, 1 chan\n"); + printf ("--\n"); + pubsubd_channels_print (&chans); + printf ("--\n"); + + // SAME CHAN, SHOULD NOT BE ADDED + create_chan (&chan, "coucou"); + // search for the chan in channels, add it if not found + new_chan = pubsubd_channel_get (&chans, &chan); + if (new_chan == NULL) { + ohshit (3, "error : same chan, shouldn't be added in channels"); + } + else { + printf ("already in the 'channels' structure\n"); + } + + printf ("print the channels, 1 chan\n"); + printf ("--\n"); + pubsubd_channels_print (&chans); + printf ("--\n"); + + // NEW CHAN, SHOULD BE ADDED + create_chan (&chan, "salut"); + // search for the chan in channels, add it if not found + new_chan = pubsubd_channel_get (&chans, &chan); + if (new_chan == NULL) { + new_chan = pubsubd_channels_add (&chans, &chan); + pubsubd_subscriber_init (&new_chan->alh); + } + else { + ohshit (4, "error : new chan, should be added in channels"); + } + pubsubd_channel_free (&chan); + + printf ("print the channels, 2 chans\n"); + printf ("--\n"); + pubsubd_channels_print (&chans); + printf ("--\n"); + + // end the application + pubsubd_channels_del_all (&chans); + + printf ("\nshould be empty now\n"); + printf ("--\n"); + pubsubd_channels_print (&chans); + printf ("--\n"); + + // the application will shut down, and remove the service named pipe + if (srv_close (&srv)) + ohshit (1, "service_close error"); + + return EXIT_SUCCESS; +}