[quagga-dev 16436] [PATCH 03/23] tests: ZeroMQ code

Philippe Guibert philippe.guibert at 6wind.com
Fri Dec 2 16:29:52 GMT 2016


From: David Lamparter <equinox at opensourcerouting.org>

The test consists in sending a message from a req(uest) to a rep(ly).
The rep will dump the received message and send back an ACK to say it
has well received the message.

To perform the zeromq test on your system, please do the following.
make check
once done, manually run
./tests/test_zmq_event
you will fall on a cli prompt.
perform the following command lines
test# configure
test(config)# log stdout
test(config)# end
test# zmq open rep ipc:///tmp/zeromqtest2
zmq socket 0x1f55ac0
test# zmq open req ipc:///tmp/zeromqtest2
zmq socket 0x1f56940
test# zmq send line

If operation is successfull, a similar message like below should appear:
2016/11/02 08:47:40 NONE: msg: 5(0x7ffddc8eb168) line
2016/11/02 08:47:40 NONE: more data: (0)0
2016/11/02 08:47:40 NONE: REP socket, sending reply
2016/11/02 08:47:40 NONE: msg: 4(0x7ffddc8eb168) ACK
2016/11/02 08:47:40 NONE: more data: (0)0

Signed-off-by: David Lamparter <equinox at opensourcerouting.org>
Signed-off-by: Philippe Guibert <philippe.guibert at 6wind.com>
---
 tests/.gitignore       |   1 +
 tests/Makefile.am      |   8 ++-
 tests/test-zmq-event.c | 181 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 189 insertions(+), 1 deletion(-)
 create mode 100644 tests/test-zmq-event.c

diff --git a/tests/.gitignore b/tests/.gitignore
index 3002b27149e1..535375b2bb81 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -38,4 +38,5 @@ teststream
 testnexthopiter
 testcommands
 test-commands-defun.c
+test_zmq_event
 site.exp
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c1194e9090c7..cb907b1b704e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -16,7 +16,7 @@ EXTRA_DIST = \
 	testcli.refout
 
 AM_CPPFLAGS = -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib -I$(top_srcdir)/lib/c-capnproto
-DEFS = @DEFS@ $(LOCAL_OPTS) -DSYSCONFDIR=\"$(sysconfdir)/\"
+DEFS = @DEFS@ $(LOCAL_OPTS) -DSYSCONFDIR=\"$(sysconfdir)/\" @ZEROMQ_CFLAGS@
 
 if BGPD
 TESTS_BGPD = aspathtest testbgpcap ecommtest testbgpmpattr testbgpmpath
@@ -31,6 +31,10 @@ check_PROGRAMS = testsig testsegv testbuffer testmemory heavy heavywq heavythrea
 		testcli \
 		$(TESTS_BGPD)
 
+if HAVE_ZEROMQ
+check_PROGRAMS += test_zmq_event
+endif
+
 ../vtysh/vtysh_cmd.c:
 	$(MAKE) -C ../vtysh vtysh_cmd.c
 
@@ -66,6 +70,7 @@ testnexthopiter_SOURCES = test-nexthop-iter.c prng.c
 testcommands_SOURCES = test-commands-defun.c test-commands.c prng.c
 test_timer_correctness_SOURCES = test-timer-correctness.c prng.c
 test_timer_performance_SOURCES = test-timer-performance.c prng.c
+test_zmq_event_SOURCES = test-zmq-event.c common-cli.c
 
 testcli_LDADD = ../lib/libzebra.la @LIBCAP@
 testsig_LDADD = ../lib/libzebra.la @LIBCAP@
@@ -88,3 +93,4 @@ testnexthopiter_LDADD = ../lib/libzebra.la @LIBCAP@
 testcommands_LDADD = ../lib/libzebra.la @LIBCAP@
 test_timer_correctness_LDADD = ../lib/libzebra.la @LIBCAP@
 test_timer_performance_LDADD = ../lib/libzebra.la @LIBCAP@
+test_zmq_event_LDADD = ../lib/libzebra.la @LIBCAP@ $(ZEROMQ_LIBS)
diff --git a/tests/test-zmq-event.c b/tests/test-zmq-event.c
new file mode 100644
index 000000000000..9ebee874cae9
--- /dev/null
+++ b/tests/test-zmq-event.c
@@ -0,0 +1,181 @@
+/*
+ * Test program to verify that scheduled timers are executed in the
+ * correct order.
+ *
+ * Copyright (C) 2013 by Open Source Routing.
+ * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (C) 2015 by David Lamparter, for NetDEF, Inc.
+ *
+ * This file is part of Quagga
+ *
+ * Quagga is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * Quagga is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Quagga; see the file COPYING.  If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include <zebra.h>
+
+#include "qzmq.h"
+#include "thread.h"
+#include "memory.h"
+#include "pqueue.h"
+#include "prng.h"
+#include "log.h"
+#include "command.h"
+#include "common-cli.h"
+
+static int dummy_req = 0, dummy_rep = 1;
+
+static void callback (void *arg, void *zmqsock, zmq_msg_t *msg)
+{
+  int64_t more = 0;
+  size_t more_size;
+  int ret;
+
+  do
+    {
+      void *data = zmq_msg_data (msg);
+      size_t size = zmq_msg_size (msg);
+
+      zlog_info ("msg: %zu(%p) %s", size, data, (const char *)data);
+
+      more_size = sizeof (more);
+      ret = zmq_getsockopt (zmqsock, ZMQ_RCVMORE, &more, &more_size);
+      zlog_info ("more data: (%d)%lld", ret, (long long)more);
+
+      if (more)
+        {
+          ret = zmq_msg_recv (msg, zmqsock, ZMQ_NOBLOCK);
+          if (ret < 0)
+            {
+              zlog_err ("zmq_msg_recv failed: %s (%d)", strerror (errno), errno);
+              return;
+            }
+        }
+    }
+  while (more);
+
+  if (arg == (void *)&dummy_rep)
+    {
+      zlog_info ("REP socket, sending reply");
+      zmq_send_const (zmqsock, "ACK\0", 4, 0);
+    }
+}
+
+DEFUN (zmq_open_rep,
+       zmq_open_rep_cmd,
+       "zmq open rep URL",
+       "ZeroMQ\n"
+       "open a socket\n"
+       "ZeroMQ REPly type\n"
+       "socket URL")
+{
+  void *rep = zmq_socket (qzmq_context, ZMQ_REP);
+
+  if (!rep)
+    {
+      vty_out (vty, "zmq_socket failed: %s (%d)%s", strerror (errno), errno,
+                    VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  if (zmq_bind (rep, argv[0]))
+    {
+      vty_out (vty, "zmq_bind failed: %s (%d)%s", strerror (errno), errno,
+                    VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  qzmq_thread_read_msg (master, callback, &dummy_rep, rep);
+
+  vty_out (vty, "zmq socket %p%s", rep, VTY_NEWLINE);
+  return CMD_SUCCESS;
+}
+
+void *last_req_opened = NULL;
+
+DEFUN (zmq_open_req,
+       zmq_open_req_cmd,
+       "zmq open req URL",
+       "ZeroMQ\n"
+       "open a socket\n"
+       "ZeroMQ REQuest type\n"
+       "socket URL")
+{
+  void *req = zmq_socket (qzmq_context, ZMQ_REQ);
+  if (!req)
+    {
+      vty_out (vty, "zmq_socket failed: %s (%d)%s", strerror (errno), errno,
+                    VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  if (zmq_connect (req, argv[0]))
+    {
+      vty_out (vty, "zmq_connect failed: %s (%d)%s", strerror (errno), errno,
+                    VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  qzmq_thread_read_msg (master, callback, &dummy_req, req);
+
+  last_req_opened = req;
+
+  vty_out (vty, "zmq socket %p%s", req, VTY_NEWLINE);
+  return CMD_SUCCESS;
+}
+
+DEFUN (zmq_do_send,
+       zmq_do_send_cmd,
+       "zmq send .LINE",
+       "ZeroMQ\n"
+       "send operation\n"
+       "data to send")
+{
+  if (!last_req_opened)
+    {
+      vty_out (vty, "open a REQ socket first please%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  if (zmq_send (last_req_opened, argv[0], strlen (argv[0]) + 1, 0) < 0)
+    {
+      vty_out (vty, "zmq_send failed: %s (%d)%s", strerror (errno), errno,
+                    VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  return CMD_SUCCESS;
+}
+
+DEFUN (zmq_exit,
+       zmq_exit_cmd,
+       "zmq exit",
+       "ZeroMQ\n"
+       "clean exit")
+{
+  qzmq_finish ();
+  return CMD_SUCCESS;
+}
+
+void test_init (void)
+{
+  qzmq_init ();
+  install_element (ENABLE_NODE, &zmq_open_req_cmd);
+  install_element (ENABLE_NODE, &zmq_open_rep_cmd);
+  install_element (ENABLE_NODE, &zmq_do_send_cmd);
+  install_element (ENABLE_NODE, &zmq_exit_cmd);
+
+  return;
+}
-- 
2.1.4





More information about the Quagga-dev mailing list