libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_router_mandatory_hwm.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 // DEBUG shouldn't be defined in sources as it will cause a redefined symbol
33 // error when it is defined in the build configuration. It appears that the
34 // intent here is to semi-permanently disable DEBUG tracing statements, so the
35 // implementation is changed to accomodate that intent.
36 //#define DEBUG 0
37 #define TRACE_ENABLED 0
38 
39 int main (void)
40 {
41  int rc;
42  if (TRACE_ENABLED) fprintf(stderr, "Staring router mandatory HWM test ...\n");
44  void *ctx = zmq_ctx_new ();
45  assert (ctx);
46  void *router = zmq_socket (ctx, ZMQ_ROUTER);
47  assert (router);
48 
49  // Configure router socket to mandatory routing and set HWM and linger
50  int mandatory = 1;
51  rc = zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &mandatory, sizeof (mandatory));
52  assert (rc == 0);
53  int sndhwm = 1;
54  rc = zmq_setsockopt (router, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
55  assert (rc == 0);
56  int linger = 1;
57  rc = zmq_setsockopt (router, ZMQ_LINGER, &linger, sizeof (linger));
58  assert (rc == 0);
59 
60  rc = zmq_bind (router, "tcp://127.0.0.1:5560");
61  assert (rc == 0);
62 
63  // Create dealer called "X" and connect it to our router, configure HWM
64  void *dealer = zmq_socket (ctx, ZMQ_DEALER);
65  assert (dealer);
66  rc = zmq_setsockopt (dealer, ZMQ_IDENTITY, "X", 1);
67  assert (rc == 0);
68  int rcvhwm = 1;
69  rc = zmq_setsockopt (dealer, ZMQ_RCVHWM, &rcvhwm, sizeof (rcvhwm));
70  assert (rc == 0);
71 
72  rc = zmq_connect (dealer, "tcp://127.0.0.1:5560");
73  assert (rc == 0);
74 
75  // Get message from dealer to know when connection is ready
76  char buffer [255];
77  rc = zmq_send (dealer, "Hello", 5, 0);
78  assert (rc == 5);
79  rc = zmq_recv (router, buffer, 255, 0);
80  assert (rc == 1);
81  assert (buffer [0] == 'X');
82 
83  int i;
84  const int BUF_SIZE = 65536;
85  char buf[BUF_SIZE];
86  memset(buf, 0, BUF_SIZE);
87  // Send first batch of messages
88  for(i = 0; i < 100000; ++i) {
89  if (TRACE_ENABLED) fprintf(stderr, "Sending message %d ...\n", i);
90  rc = zmq_send (router, "X", 1, ZMQ_DONTWAIT | ZMQ_SNDMORE);
91  if (rc == -1 && zmq_errno() == EAGAIN) break;
92  assert (rc == 1);
93  rc = zmq_send (router, buf, BUF_SIZE, ZMQ_DONTWAIT);
94  assert (rc == BUF_SIZE);
95  }
96  // This should fail after one message but kernel buffering could
97  // skew results
98  assert (i < 10);
99  msleep (1000);
100  // Send second batch of messages
101  for(; i < 100000; ++i) {
102  if (TRACE_ENABLED) fprintf(stderr, "Sending message %d (part 2) ...\n", i);
103  rc = zmq_send (router, "X", 1, ZMQ_DONTWAIT | ZMQ_SNDMORE);
104  if (rc == -1 && zmq_errno() == EAGAIN) break;
105  assert (rc == 1);
106  rc = zmq_send (router, buf, BUF_SIZE, ZMQ_DONTWAIT);
107  assert (rc == BUF_SIZE);
108  }
109  // This should fail after two messages but kernel buffering could
110  // skew results
111  assert (i < 20);
112 
113  if (TRACE_ENABLED) fprintf(stderr, "Done sending messages.\n");
114 
115  rc = zmq_close (router);
116  assert (rc == 0);
117 
118  rc = zmq_close (dealer);
119  assert (rc == 0);
120 
121  rc = zmq_ctx_term (ctx);
122  assert (rc == 0);
123 
124  return 0 ;
125 }
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
#define ZMQ_RCVHWM
Definition: zmq.h:282
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
ZMQ_EXPORT int zmq_errno(void)
Definition: zmq.cpp:107
#define ZMQ_ROUTER
Definition: zmq.h:252
int main(void)
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_SNDHWM
Definition: zmq.h:281
#define ZMQ_LINGER
Definition: zmq.h:276
#define ZMQ_ROUTER_MANDATORY
Definition: zmq.h:287
#define ZMQ_IDENTITY
Definition: zmq.h:265
#define TRACE_ENABLED
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_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
#define BUF_SIZE
#define ZMQ_DONTWAIT
Definition: zmq.h:345