libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_issue_566.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 // Issue 566 describes a problem in libzmq v4.0.0 where a dealer to router
33 // connection would fail randomly. The test works when the two sockets are
34 // on the same context, and failed when they were on separate contexts.
35 // Fixed by https://github.com/zeromq/libzmq/commit/be25cf.
36 
37 int main (void)
38 {
40 
41  void *ctx1 = zmq_ctx_new ();
42  assert (ctx1);
43 
44  void *ctx2 = zmq_ctx_new ();
45  assert (ctx2);
46 
47  void *router = zmq_socket (ctx1, ZMQ_ROUTER);
48  int on = 1;
49  int rc = zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &on, sizeof (on));
50  assert (rc == 0);
51  rc = zmq_bind (router, "tcp://127.0.0.1:5555");
52  assert (rc != -1);
53 
54  // Repeat often enough to be sure this works as it should
55  for (int cycle = 0; cycle < 100; cycle++) {
56  // Create dealer with unique explicit identity
57  // We assume the router learns this out-of-band
58  void *dealer = zmq_socket (ctx2, ZMQ_DEALER);
59  char identity [10];
60  sprintf (identity, "%09d", cycle);
61  rc = zmq_setsockopt (dealer, ZMQ_IDENTITY, identity, 10);
62  assert (rc == 0);
63  int rcvtimeo = 1000;
64  rc = zmq_setsockopt (dealer, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int));
65  assert (rc == 0);
66  rc = zmq_connect (dealer, "tcp://127.0.0.1:5555");
67  assert (rc == 0);
68 
69  // Router will try to send to dealer, at short intervals.
70  // It typically takes 2-5 msec for the connection to establish
71  // on a loopback interface, but we'll allow up to one second
72  // before failing the test (e.g. for running on a debugger or
73  // a very slow system).
74  for (int attempt = 0; attempt < 500; attempt++) {
75  zmq_poll (0, 0, 2);
76  rc = zmq_send (router, identity, 10, ZMQ_SNDMORE);
77  if (rc == -1 && errno == EHOSTUNREACH)
78  continue;
79  assert (rc == 10);
80  rc = zmq_send (router, "HELLO", 5, 0);
81  assert (rc == 5);
82  break;
83  }
84  uint8_t buffer [5];
85  rc = zmq_recv (dealer, buffer, 5, 0);
86  assert (rc == 5);
87  assert (memcmp (buffer, "HELLO", 5) == 0);
88  close_zero_linger (dealer);
89  }
90  zmq_close (router);
91  zmq_ctx_destroy (ctx1);
92  zmq_ctx_destroy (ctx2);
93 
94  return 0;
95 }
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
#define ZMQ_DEALER
Definition: zmq.h:251
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT int zmq_recv(void *s, void *buf, size_t len, int flags)
Definition: zmq.cpp:507
#define ZMQ_ROUTER
Definition: zmq.h:252
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_ROUTER_MANDATORY
Definition: zmq.h:287
int main(void)
#define ZMQ_IDENTITY
Definition: zmq.h:265
#define EHOSTUNREACH
Definition: zmq.h:160
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
ZMQ_EXPORT int zmq_ctx_destroy(void *context)
Definition: zmq.cpp:236
ZMQ_EXPORT int zmq_poll(zmq_pollitem_t *items, int nitems, long timeout)
Definition: zmq.cpp:748
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
void close_zero_linger(void *socket)
Definition: testutil.hpp:275
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
#define ZMQ_RCVTIMEO
Definition: zmq.h:284