From 4a32978c536462d0f96b319735ad6af4b92b8290 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Fri, 9 Sep 2016 13:29:36 +0200
Subject: [PATCH] pubsubd: test pipes
---
pubsub/test-pipe-read.c | 1 +
pubsub/test-pipe-write.c | 58 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+)
create mode 100644 pubsub/test-pipe-write.c
diff --git a/pubsub/test-pipe-read.c b/pubsub/test-pipe-read.c
index a2cb78f..7ab462c 100644
--- a/pubsub/test-pipe-read.c
+++ b/pubsub/test-pipe-read.c
@@ -42,6 +42,7 @@ main(int argc, char **argv)
struct pubsub_msg m;
pubsubd_msg_unserialize (&m, buf, msize);
pubsubd_msg_print (&m);
+ pubsubd_msg_free (&m);
sleep (1);
}
diff --git a/pubsub/test-pipe-write.c b/pubsub/test-pipe-write.c
new file mode 100644
index 0000000..3520928
--- /dev/null
+++ b/pubsub/test-pipe-write.c
@@ -0,0 +1,58 @@
+#include "../lib/pubsubd.h"
+#include
+
+#define CHAN "chan1"
+#define MESSAGE "coucou"
+
+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 write 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 ("Writing on %s %d times.\n", path, nb);
+
+ char *buf = NULL;
+ size_t msize = 0;
+
+ int ret = 0;
+
+ while (nb--) {
+ struct pubsub_msg msg;
+ memset (&msg, 0, sizeof (struct pubsub_msg));
+ msg.type = PUBSUB_TYPE_MESSAGE;
+ msg.chan = malloc (strlen (CHAN) + 1);
+ strncpy ((char *)msg.chan, CHAN, strlen (CHAN) + 1);
+ msg.chanlen = strlen (CHAN) + 1;
+ msg.data = malloc (strlen (MESSAGE) + 1);
+ strncpy ((char *)msg.data, MESSAGE, strlen (CHAN) + 1);
+ msg.datalen = strlen (MESSAGE) + 1;
+
+ pubsubd_msg_serialize (&msg, &buf, &msize);
+ pubsubd_msg_print (&msg);
+ pubsubd_msg_free (&msg);
+ ret = file_write (path, buf, msize);
+ if (ret != msize) {
+ fprintf (stderr, "msg not written\n");
+ }
+ sleep (1);
+ }
+
+ return EXIT_SUCCESS;
+}