1/*
2 Copyright (c) 2007-2017 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
35void test_roundtrip ()
36{
37 char endpoint1[MAX_SOCKET_STRING];
38 char endpoint2[MAX_SOCKET_STRING];
39
40 // Create a req/rep device.
41 void *dealer = test_context_socket (ZMQ_DEALER);
42 bind_loopback_ipv4 (dealer, endpoint1, sizeof (endpoint1));
43
44 void *router = test_context_socket (ZMQ_ROUTER);
45 bind_loopback_ipv4 (router, endpoint2, sizeof (endpoint2));
46
47 // Create a worker.
48 void *rep = test_context_socket (ZMQ_REP);
49 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rep, endpoint1));
50
51 // Create a client.
52 void *req = test_context_socket (ZMQ_REQ);
53 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (req, endpoint2));
54
55 // Send a request.
56 send_string_expect_success (req, "ABC", ZMQ_SNDMORE);
57 send_string_expect_success (req, "DEF", 0);
58
59 // Pass the request through the device.
60 for (int i = 0; i != 4; i++) {
61 zmq_msg_t msg;
62 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
63 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, router, 0));
64 int rcvmore;
65 size_t sz = sizeof (rcvmore);
66 TEST_ASSERT_SUCCESS_ERRNO (
67 zmq_getsockopt (router, ZMQ_RCVMORE, &rcvmore, &sz));
68 TEST_ASSERT_SUCCESS_ERRNO (
69 zmq_msg_send (&msg, dealer, rcvmore ? ZMQ_SNDMORE : 0));
70 }
71
72 // Receive the request.
73 recv_string_expect_success (rep, "ABC", 0);
74 int rcvmore;
75 size_t sz = sizeof (rcvmore);
76 TEST_ASSERT_SUCCESS_ERRNO (
77 zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz));
78 TEST_ASSERT_TRUE (rcvmore);
79 recv_string_expect_success (rep, "DEF", 0);
80 TEST_ASSERT_SUCCESS_ERRNO (
81 zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz));
82 TEST_ASSERT_FALSE (rcvmore);
83
84 // Send the reply.
85 send_string_expect_success (rep, "GHI", ZMQ_SNDMORE);
86 send_string_expect_success (rep, "JKL", 0);
87
88 // Pass the reply through the device.
89 for (int i = 0; i != 4; i++) {
90 zmq_msg_t msg;
91 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
92 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, dealer, 0));
93 int rcvmore;
94 size_t sz = sizeof (rcvmore);
95 TEST_ASSERT_SUCCESS_ERRNO (
96 zmq_getsockopt (dealer, ZMQ_RCVMORE, &rcvmore, &sz));
97 TEST_ASSERT_SUCCESS_ERRNO (
98 zmq_msg_send (&msg, router, rcvmore ? ZMQ_SNDMORE : 0));
99 }
100
101 // Receive the reply.
102 recv_string_expect_success (req, "GHI", 0);
103 TEST_ASSERT_SUCCESS_ERRNO (
104 zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz));
105 TEST_ASSERT_TRUE (rcvmore);
106 recv_string_expect_success (req, "JKL", 0);
107 TEST_ASSERT_SUCCESS_ERRNO (
108 zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz));
109 TEST_ASSERT_FALSE (rcvmore);
110
111 // Clean up.
112 test_context_socket_close (req);
113 test_context_socket_close (rep);
114 test_context_socket_close (router);
115 test_context_socket_close (dealer);
116}
117
118int main ()
119{
120 setup_test_environment ();
121
122 UNITY_BEGIN ();
123 RUN_TEST (test_roundtrip);
124 return UNITY_END ();
125}
126