libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_xpub_nodrop.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 int main (void)
33 {
35  void *ctx = zmq_ctx_new ();
36  assert (ctx);
37 
38  // Create a publisher
39  void *pub = zmq_socket (ctx, ZMQ_PUB);
40  assert (pub);
41 
42  int hwm = 2000;
43  int rc = zmq_setsockopt(pub, ZMQ_SNDHWM, &hwm, 4);
44  assert(rc == 0);
45 
46  rc = zmq_bind (pub, "inproc://soname");
47  assert (rc == 0);
48 
49  // set pub socket options
50  int wait = 1;
51  rc = zmq_setsockopt (pub, ZMQ_XPUB_NODROP, &wait, 4);
52  assert (rc == 0);
53 
54 
55  // Create a subscriber
56  void *sub = zmq_socket (ctx, ZMQ_SUB);
57  assert (sub);
58  rc = zmq_connect (sub, "inproc://soname");
59  assert (rc == 0);
60 
61  // Subscribe for all messages.
62  rc = zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0);
63  assert (rc == 0);
64 
65  int hwmlimit = hwm - 1;
66  int send_count = 0;
67 
68  // Send an empty message
69  for (int i = 0; i < hwmlimit; i++) {
70  rc = zmq_send (pub, NULL, 0, 0);
71  assert (rc == 0);
72  send_count++;
73  }
74 
75  int recv_count = 0;
76  do {
77  // Receive the message in the subscriber
78  rc = zmq_recv (sub, NULL, 0, ZMQ_DONTWAIT);
79  if (rc == -1)
80  assert (errno == EAGAIN);
81  else {
82  assert (rc == 0);
83  recv_count++;
84  }
85  }
86  while (rc == 0);
87 
88  assert (send_count == recv_count);
89 
90  // Now test real blocking behavior
91  // Set a timeout, default is infinite
92  int timeout = 0;
93  rc = zmq_setsockopt (pub, ZMQ_SNDTIMEO, &timeout, 4);
94  assert (rc == 0);
95 
96  send_count = 0;
97  recv_count = 0;
98  hwmlimit = hwm;
99 
100  // Send an empty message until we get an error, which must be EAGAIN
101  while (zmq_send (pub, "", 0, 0) == 0)
102  send_count++;
103  assert (errno == EAGAIN);
104 
105  while (zmq_recv (sub, NULL, 0, ZMQ_DONTWAIT) == 0)
106  recv_count++;
107  assert (send_count == recv_count);
108 
109  // Clean up.
110  rc = zmq_close (pub);
111  assert (rc == 0);
112  rc = zmq_close (sub);
113  assert (rc == 0);
114  rc = zmq_ctx_term (ctx);
115  assert (rc == 0);
116 
117  return 0 ;
118 }
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_SUB
Definition: zmq.h:248
#define ZMQ_SUBSCRIBE
Definition: zmq.h:266
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_PUB
Definition: zmq.h:247
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_SNDHWM
Definition: zmq.h:281
#define ZMQ_SNDTIMEO
Definition: zmq.h:285
int main(void)
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 ZMQ_DONTWAIT
Definition: zmq.h:345
#define ZMQ_XPUB_NODROP
Definition: zmq.h:318