From 0524f70bab98f09157d4be9f7a85d9a1a070f0fe Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Fri, 9 Sep 2016 12:33:47 +0200 Subject: [PATCH] pubsubd: corrections + tests --- lib/pubsubd.c | 15 ++----------- pubsub/msg-serialize.c | 2 +- pubsub/test-pipe-read.c | 49 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 pubsub/test-pipe-read.c diff --git a/lib/pubsubd.c b/lib/pubsubd.c index 1d83374..7b31021 100644 --- a/lib/pubsubd.c +++ b/lib/pubsubd.c @@ -561,19 +561,8 @@ void pubsub_disconnect (struct process *p) pubsubd_msg_serialize (&m, &buf, &msize); int ret = app_write (p, buf, msize); - switch (ret) { - case ER_FILE_WRITE : - fprintf (stderr, "err: ER_FILE_WRITE\n"); - break; - case ER_FILE_WRITE_PARAMS : - fprintf (stderr, "err: ER_FILE_WRITE_PARAMS\n"); - break; - case ER_FILE_OPEN : - fprintf (stderr, "err: ER_FILE_OPEN\n"); - break; - case ER_FILE_CLOSE : - fprintf (stderr, "err: ER_FILE_CLOSE\n"); - break; + if (ret != (int) msize) { + fprintf (stderr, "err: can't disconnect\n"); } pubsubd_msg_free (&m); diff --git a/pubsub/msg-serialize.c b/pubsub/msg-serialize.c index 3138f57..bcaa165 100644 --- a/pubsub/msg-serialize.c +++ b/pubsub/msg-serialize.c @@ -40,7 +40,7 @@ main(int argc, char **argv) pubsubd_msg_serialize (&msg, &data, &len); pubsubd_msg_free (&msg); - if (len != write (1, data, len)) { + if ((int) len != write (1, data, len)) { ohshit (1, "unable to write the data"); } diff --git a/pubsub/test-pipe-read.c b/pubsub/test-pipe-read.c new file mode 100644 index 0000000..a2cb78f --- /dev/null +++ b/pubsub/test-pipe-read.c @@ -0,0 +1,49 @@ +#include "../lib/pubsubd.h" +#include + +#define TEST_NAME "test-chan-lists" + +void +ohshit(int rvalue, const char* str) { + fprintf(stderr, "%s\n", str); + exit(rvalue); +} + +void usage (char **argv) { + fprintf (stderr, "usage: %s path times\n", argv[0]); + fprintf (stderr, "ex: %s /tmp/pipe 5 => you will read 5 times\n", argv[0]); + exit (1); +} + +int +main(int argc, char **argv) +{ + + if (argc != 3) + usage (argv); + + char *path = argv[1]; + int nb = atoi (argv[2]); + + printf ("Listening on %s %d times.\n", path, nb); + + char *buf = NULL; + size_t msize = 0; + + int ret = 0; + + while (nb--) { + ret = file_read (path, &buf, &msize); + if (ret == 0) { + printf ("no msg\n"); + nb++; + continue; + } + struct pubsub_msg m; + pubsubd_msg_unserialize (&m, buf, msize); + pubsubd_msg_print (&m); + sleep (1); + } + + return EXIT_SUCCESS; +}