libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_spec_rep.cpp
Go to the documentation of this file.
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 
32 const char *bind_address = 0;
33 const char *connect_address = 0;
34 
35 void test_fair_queue_in (void *ctx)
36 {
37  void *rep = zmq_socket (ctx, ZMQ_REP);
38  assert (rep);
39 
40  int timeout = 250;
41  int rc = zmq_setsockopt (rep, ZMQ_RCVTIMEO, &timeout, sizeof (int));
42  assert (rc == 0);
43 
44  rc = zmq_bind (rep, bind_address);
45  assert (rc == 0);
46 
47  const size_t services = 5;
48  void *reqs [services];
49  for (size_t peer = 0; peer < services; ++peer) {
50  reqs [peer] = zmq_socket (ctx, ZMQ_REQ);
51  assert (reqs [peer]);
52 
53  rc = zmq_setsockopt (reqs [peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
54  assert (rc == 0);
55 
56  rc = zmq_connect (reqs [peer], connect_address);
57  assert (rc == 0);
58  }
59 
61 
62  s_send_seq (reqs [0], "A", SEQ_END);
63  s_recv_seq (rep, "A", SEQ_END);
64  s_send_seq (rep, "A", SEQ_END);
65  s_recv_seq (reqs [0], "A", SEQ_END);
66 
67  s_send_seq (reqs [0], "A", SEQ_END);
68  s_recv_seq (rep, "A", SEQ_END);
69  s_send_seq (rep, "A", SEQ_END);
70  s_recv_seq (reqs [0], "A", SEQ_END);
71 
72  // TODO: following test fails randomly on some boxes
73 #ifdef SOMEONE_FIXES_THIS
74  // send N requests
75  for (size_t peer = 0; peer < services; ++peer) {
76  char * str = strdup("A");
77  str [0] += peer;
78  s_send_seq (reqs [peer], str, SEQ_END);
79  free (str);
80  }
81 
82  // handle N requests
83  for (size_t peer = 0; peer < services; ++peer) {
84  char * str = strdup("A");
85  str [0] += peer;
86  // Test fails here
87  s_recv_seq (rep, str, SEQ_END);
88  s_send_seq (rep, str, SEQ_END);
89  s_recv_seq (reqs [peer], str, SEQ_END);
90  free (str);
91  }
92 #endif
93  close_zero_linger (rep);
94 
95  for (size_t peer = 0; peer < services; ++peer)
96  close_zero_linger (reqs [peer]);
97 
98  // Wait for disconnects.
100 }
101 
102 void test_envelope (void *ctx)
103 {
104  void *rep = zmq_socket (ctx, ZMQ_REP);
105  assert (rep);
106 
107  int rc = zmq_bind (rep, bind_address);
108  assert (rc == 0);
109 
110  void *dealer = zmq_socket (ctx, ZMQ_DEALER);
111  assert (dealer);
112 
113  rc = zmq_connect (dealer, connect_address);
114  assert (rc == 0);
115 
116  // minimal envelope
117  s_send_seq (dealer, 0, "A", SEQ_END);
118  s_recv_seq (rep, "A", SEQ_END);
119  s_send_seq (rep, "A", SEQ_END);
120  s_recv_seq (dealer, 0, "A", SEQ_END);
121 
122  // big envelope
123  s_send_seq (dealer, "X", "Y", 0, "A", SEQ_END);
124  s_recv_seq (rep, "A", SEQ_END);
125  s_send_seq (rep, "A", SEQ_END);
126  s_recv_seq (dealer, "X", "Y", 0, "A", SEQ_END);
127 
128  close_zero_linger (rep);
129  close_zero_linger (dealer);
130 
131  // Wait for disconnects.
133 }
134 
135 int main (void)
136 {
138  void *ctx = zmq_ctx_new ();
139  assert (ctx);
140 
141  const char *binds [] = { "inproc://a", "tcp://127.0.0.1:5555" };
142  const char *connects [] = { "inproc://a", "tcp://localhost:5555" };
143 
144  for (int transport = 0; transport < 2; ++transport) {
145  bind_address = binds [transport];
146  connect_address = connects [transport];
147 
148  // SHALL receive incoming messages from its peers using a fair-queuing
149  // strategy.
150  test_fair_queue_in (ctx);
151 
152  // For an incoming message:
153  // SHALL remove and store the address envelope, including the delimiter.
154  // SHALL pass the remaining data frames to its calling application.
155  // SHALL wait for a single reply message from its calling application.
156  // SHALL prepend the address envelope and delimiter.
157  // SHALL deliver this message back to the originating peer.
158  test_envelope (ctx);
159  }
160 
161  int rc = zmq_ctx_term (ctx);
162  assert (rc == 0);
163 
164  return 0 ;
165 }
void msleep(int milliseconds)
Definition: testutil.hpp:316
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
void test_envelope(void *ctx)
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_DEALER
Definition: zmq.h:251
#define ZMQ_REP
Definition: zmq.h:250
void setup_test_environment(void)
Definition: testutil.hpp:285
#define SETTLE_TIME
Definition: testutil.hpp:44
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
void test_fair_queue_in(void *ctx)
int main(void)
#define ZMQ_REQ
Definition: zmq.h:249
const char * bind_address
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
void close_zero_linger(void *socket)
Definition: testutil.hpp:275
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
void s_recv_seq(void *socket,...)
Definition: testutil.hpp:236
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
const char * SEQ_END
Definition: testutil.hpp:198
const char * connect_address
void s_send_seq(void *socket,...)
Definition: testutil.hpp:205
#define ZMQ_RCVTIMEO
Definition: zmq.h:284