Hi all!

Here is a little patch for the newly emerged tests.

The code is released under the MIT/X11 License.


>From f6309ec19cc8c1ed1d52baa62cb07e450583a80e Mon Sep 17 00:00:00 2001
From: guidog <[email protected]>
Date: Fri, 27 Aug 2010 19:36:37 +0200
Subject: [PATCH] Added tests for PAIR and REQ/REP.

---
 tests/Makefile.am     |    4 ++-
 tests/test_pair.cpp   |   55 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_reqrep.cpp |   55 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/testutil.hpp    |   38 +++++++++++++++++++++++++++++++++
 4 files changed, 151 insertions(+), 1 deletions(-)
 create mode 100644 tests/test_pair.cpp
 create mode 100644 tests/test_reqrep.cpp
 create mode 100644 tests/testutil.hpp

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 50ba0f3..bad1538 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,8 +1,10 @@
 INCLUDES = -I$(top_builddir)/include
 LDADD = $(top_builddir)/src/libzmq.la
 
-noinst_PROGRAMS = simple
+noinst_PROGRAMS = simple test_pair test_reqrep
 
 simple_SOURCES = simple.cpp
+test_pair_SOURCES = test_pair.cpp testutil.hpp
+test_reqrep_SOURCES = test_reqrep.cpp testutil.hpp
 
 TESTS = $(noinst_PROGRAMS)
diff --git a/tests/test_pair.cpp b/tests/test_pair.cpp
new file mode 100644
index 0000000..a9c9802
--- /dev/null
+++ b/tests/test_pair.cpp
@@ -0,0 +1,55 @@
+#include <iostream>
+
+#include <zmq.hpp>
+#include "testutil.hpp"
+
+using namespace std ;
+using namespace zmqtestutil ;
+
+// zmq context is global
+static zmq::context_t context(1);
+
+int main(int argc, char* argv[]) {
+    zmq::pollitem_t items[2];
+    socket_pair p = create_bound_pair(&context, ZMQ_PAIR, ZMQ_PAIR, "tcp://127.0.0.1:2000");
+
+    // first test simple ping pong
+    const string expect("XXX");
+
+    {
+        const string returned = zmqtestutil::ping_pong(p, expect);
+        assert ( expect == returned );
+    }
+
+    {
+        zmq::message_t m1(expect.size());
+        memcpy(m1.data(), expect.c_str(), expect.size() );
+        items [0].socket = *p.first ;
+        items [0].fd = 0;
+        items [0].events = ZMQ_POLLIN;
+        items [0].revents = 0;
+        items [1].socket = *p.second;
+        items [1].fd = 0;
+        items [1].events = ZMQ_POLLIN;
+        items [1].revents = 0;
+
+        p.first->send(m1, 0);
+
+        // now use polling
+        // timout makes test abort on error
+        int rc = zmq::poll(&items[0], 2, 1000);
+        assert( rc == 1 );
+        assert( (items[1].revents & ZMQ_POLLIN) != 0);
+
+        zmq::message_t m2 ;
+        p.second->recv(&m2, 0);
+        const string ret((char *)m2.data(), m2.size());
+        assert ( expect == ret );
+    }
+
+    // delete sockets
+    delete(p.first);
+    delete(p.second);
+
+    return 0 ;
+}
diff --git a/tests/test_reqrep.cpp b/tests/test_reqrep.cpp
new file mode 100644
index 0000000..02cfd71
--- /dev/null
+++ b/tests/test_reqrep.cpp
@@ -0,0 +1,55 @@
+#include <iostream>
+
+#include <zmq.hpp>
+#include "testutil.hpp"
+
+using namespace std ;
+using namespace zmqtestutil ;
+
+// zmq context is global
+static zmq::context_t context(1);
+
+int main() {
+    zmq::pollitem_t items[2];
+    socket_pair p = create_bound_pair(&context, ZMQ_REQ, ZMQ_REP, "tcp://127.0.0.1:2000");
+
+    // first test simple ping pong
+    const string expect("XXX");
+
+    {
+        const string returned = zmqtestutil::ping_pong(p, expect);
+        assert ( expect == returned );
+    }
+
+    {
+        zmq::message_t m1(expect.size());
+        memcpy(m1.data(), expect.c_str(), expect.size() );
+        items [0].socket = *p.first ;
+        items [0].fd = 0;
+        items [0].events = ZMQ_POLLIN;
+        items [0].revents = 0;
+        items [1].socket = *p.second;
+        items [1].fd = 0;
+        items [1].events = ZMQ_POLLIN;
+        items [1].revents = 0;
+
+        p.first->send(m1, 0);
+
+        // now use polling
+        // timout makes test abort on error
+        int rc = zmq::poll(&items[0], 2, 1000);
+        assert( rc == 1 );
+        assert( (items[1].revents & ZMQ_POLLIN) != 0);
+
+        zmq::message_t m2 ;
+        p.second->recv(&m2, 0);
+        const string ret((char *)m2.data(), m2.size());
+        assert ( expect == ret );
+    }
+
+    // delete sockets
+    delete(p.first);
+    delete(p.second);
+
+    return 0 ;
+}
diff --git a/tests/testutil.hpp b/tests/testutil.hpp
new file mode 100644
index 0000000..5de7892
--- /dev/null
+++ b/tests/testutil.hpp
@@ -0,0 +1,38 @@
+#ifndef ZMQTESTUTIL_
+#define ZMQTESTUTIL_
+
+#include <utility>
+#include <zmq.hpp>
+
+namespace zmqtestutil {
+
+    typedef std::pair<zmq::socket_t*, zmq::socket_t*> socket_pair ;
+
+    socket_pair create_bound_pair(zmq::context_t* context,
+                                  int t1, int t2,
+                                  const char * transport) {
+        zmq::socket_t* s1 = new zmq::socket_t(*context, t1);
+        zmq::socket_t* s2 = new zmq::socket_t(*context, t2);
+        s1->bind(transport);
+        s2->connect(transport);
+        return socket_pair(s1, s2);
+    }
+
+    std::string ping_pong(const socket_pair& sp, const std::string& orig_msg) {
+        zmq::socket_t& s1 = *sp.first ;
+        zmq::socket_t& s2 = *sp.second ;
+        // construct message to send
+        zmq::message_t ping(orig_msg.size());
+        memcpy(ping.data(), orig_msg.c_str(), orig_msg.size() );
+        // send ping out
+        s1.send(ping, 0);
+        // get pong from connected socket
+        zmq::message_t pong ;
+        s2.recv(&pong, 0);
+        // return received data as std::string
+        return std::string((char *)pong.data(), pong.size());
+    }
+
+} ;
+
+#endif
-- 
1.7.1

_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to