1/*
2 Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "testutil.hpp"
31#include "testutil_unity.hpp"
32
33SETUP_TEARDOWN_TESTCONTEXT
34
35// TODO this is heavily duplicated with test_reqrep_device.cpp
36void test_roundtrip ()
37{
38 // Create a req/rep device.
39 void *dealer = test_context_socket (ZMQ_DEALER);
40 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (dealer, "tipc://{5560,0,0}"));
41 void *router = test_context_socket (ZMQ_ROUTER);
42 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, "tipc://{5561,0,0}"));
43
44 // Create a worker.
45 void *rep = test_context_socket (ZMQ_REP);
46 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rep, "tipc://{5560,0}@0.0.0"));
47
48 // Create a client.
49 void *req = test_context_socket (ZMQ_REQ);
50 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (req, "tipc://{5561,0}@0.0.0"));
51
52 // Send a request.
53 send_string_expect_success (req, "ABC", ZMQ_SNDMORE);
54 send_string_expect_success (req, "DEF", 0);
55
56 // Pass the request through the device.
57 for (int i = 0; i != 4; i++) {
58 zmq_msg_t msg;
59 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
60 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, router, 0));
61 int rcvmore;
62 size_t sz = sizeof (rcvmore);
63 TEST_ASSERT_SUCCESS_ERRNO (
64 zmq_getsockopt (router, ZMQ_RCVMORE, &rcvmore, &sz));
65 TEST_ASSERT_SUCCESS_ERRNO (
66 zmq_msg_send (&msg, dealer, rcvmore ? ZMQ_SNDMORE : 0));
67 }
68
69 // Receive the request.
70 recv_string_expect_success (rep, "ABC", 0);
71 int rcvmore;
72 size_t sz = sizeof (rcvmore);
73 TEST_ASSERT_SUCCESS_ERRNO (
74 zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz));
75 TEST_ASSERT_TRUE (rcvmore);
76 recv_string_expect_success (rep, "DEF", 0);
77 TEST_ASSERT_SUCCESS_ERRNO (
78 zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz));
79 TEST_ASSERT_FALSE (rcvmore);
80
81 // Send the reply.
82 send_string_expect_success (rep, "GHI", ZMQ_SNDMORE);
83 send_string_expect_success (rep, "JKL", 0);
84
85 // Pass the reply through the device.
86 for (int i = 0; i != 4; i++) {
87 zmq_msg_t msg;
88 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
89 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, dealer, 0));
90 int rcvmore;
91 size_t sz = sizeof (rcvmore);
92 TEST_ASSERT_SUCCESS_ERRNO (
93 zmq_getsockopt (dealer, ZMQ_RCVMORE, &rcvmore, &sz));
94 TEST_ASSERT_SUCCESS_ERRNO (
95 zmq_msg_send (&msg, router, rcvmore ? ZMQ_SNDMORE : 0));
96 }
97
98 // Receive the reply.
99 recv_string_expect_success (req, "GHI", 0);
100 TEST_ASSERT_SUCCESS_ERRNO (
101 zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz));
102 TEST_ASSERT_TRUE (rcvmore);
103 recv_string_expect_success (req, "JKL", 0);
104 TEST_ASSERT_SUCCESS_ERRNO (
105 zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz));
106 TEST_ASSERT_FALSE (rcvmore);
107
108 // Clean up.
109 test_context_socket_close (req);
110 test_context_socket_close (rep);
111 test_context_socket_close (router);
112 test_context_socket_close (dealer);
113}
114
115int main ()
116{
117 if (!is_tipc_available ()) {
118 printf ("TIPC environment unavailable, skipping test\n");
119 return 77;
120 }
121
122 UNITY_BEGIN ();
123 RUN_TEST (test_roundtrip);
124 return UNITY_END ();
125}
126