libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_iov.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 // XSI vector I/O
33 #if defined ZMQ_HAVE_UIO
34 #include <sys/uio.h>
35 #else
36 struct iovec {
37  void *iov_base;
38  size_t iov_len;
39 };
40 #endif
41 
42 void do_check(void* sb, void* sc, size_t msg_size)
43 {
44  assert (sb && sc && msg_size > 0);
45 
46  int rc = 0;
47  const char msg_val = '1';
48  const int num_messages = 10;
49  size_t send_count, recv_count;
50 
51  send_count = recv_count = num_messages;
52 
53  char *ref_msg = (char *) malloc (msg_size);
54  assert (ref_msg);
55  memset (ref_msg, msg_val, msg_size);
56 
57  // zmq_sendiov(3) as a single multi-part send
58  struct iovec send_iov[num_messages];
59  char *buf = (char *) malloc (msg_size * num_messages);
60 
61  for (int i = 0; i < num_messages; i++)
62  {
63  send_iov[i].iov_base = &buf[i * msg_size];
64  send_iov[i].iov_len = msg_size;
65  memcpy (send_iov[i].iov_base, ref_msg, msg_size);
66  assert (memcmp (ref_msg, send_iov[i].iov_base, msg_size) == 0);
67  }
68 
69  // Test errors - zmq_recviov - null socket
70  rc = zmq_sendiov (NULL, send_iov, send_count, ZMQ_SNDMORE);
71  assert (rc == -1 && errno == ENOTSOCK);
72  // Test errors - zmq_recviov - invalid send count
73  rc = zmq_sendiov (sc, send_iov, 0, 0);
74  assert (rc == -1 && errno == EINVAL);
75  // Test errors - zmq_recviov - null iovec
76  rc = zmq_sendiov (sc, NULL, send_count, 0);
77  assert (rc == -1 && errno == EINVAL);
78 
79  // Test success
80  rc = zmq_sendiov (sc, send_iov, send_count, ZMQ_SNDMORE);
81  // The zmq_sendiov(3) API method does not follow the same semantics as
82  // zmq_recviov(3); the latter returns the count of messages sent, rightly
83  // so, whilst the former sends the number of bytes successfully sent from
84  // the last message, which does not hold much sense from a batch send
85  // perspective; hence the assert checks if rc is same as msg_size.
86  assert ((size_t)rc == msg_size);
87 
88  // zmq_recviov(3) single-shot
89  struct iovec recv_iov[num_messages];
90 
91  // Test errors - zmq_recviov - null socket
92  rc = zmq_recviov (NULL, recv_iov, &recv_count, 0);
93  assert (rc == -1 && errno == ENOTSOCK);
94  // Test error - zmq_recviov - invalid receive count
95  rc = zmq_recviov (sb, recv_iov, NULL, 0);
96  assert (rc == -1 && errno == EINVAL);
97  size_t invalid_recv_count = 0;
98  rc = zmq_recviov (sb, recv_iov, &invalid_recv_count, 0);
99  assert (rc == -1 && errno == EINVAL);
100  // Test error - zmq_recviov - null iovec
101  rc = zmq_recviov (sb, NULL, &recv_count, 0);
102  assert (rc == -1 && errno == EINVAL);
103 
104  // Test success
105  rc = zmq_recviov (sb, recv_iov, &recv_count, 0);
106  assert (rc == num_messages);
107 
108  for (int i = 0; i < num_messages; i++)
109  {
110  assert (recv_iov[i].iov_base);
111  assert (memcmp (ref_msg, recv_iov[i].iov_base, msg_size) == 0);
112  free(recv_iov[i].iov_base);
113  }
114 
115  assert (send_count == recv_count);
116  free (ref_msg);
117  free (buf);
118 }
119 
120 int main (void)
121 {
123 
124  void *ctx = zmq_ctx_new ();
125  assert (ctx);
126  int rc;
127 
128  void *sb = zmq_socket (ctx, ZMQ_PULL);
129  assert (sb);
130 
131  rc = zmq_bind (sb, "inproc://a");
132  assert (rc == 0);
133 
135  void *sc = zmq_socket (ctx, ZMQ_PUSH);
136 
137  rc = zmq_connect (sc, "inproc://a");
138  assert (rc == 0);
139 
140 
141  // message bigger than VSM max
142  do_check (sb, sc, 100);
143 
144  // message smaller than VSM max
145  do_check (sb, sc, 10);
146 
147  rc = zmq_close (sc);
148  assert (rc == 0);
149 
150  rc = zmq_close (sb);
151  assert (rc == 0);
152 
153  rc = zmq_ctx_term (ctx);
154  assert (rc == 0);
155 
156  return 0;
157 }
size_t iov_len
Definition: zmq.cpp:59
ZMQ_EXPORT int zmq_recviov(void *s, struct iovec *iov, size_t *count, int flags)
Definition: zmq.cpp:557
void msleep(int milliseconds)
Definition: testutil.hpp:316
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
int main(void)
Definition: test_iov.cpp:120
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
#define ZMQ_PUSH
Definition: zmq.h:254
void * iov_base
Definition: zmq.cpp:58
#define ENOTSOCK
Definition: zmq.h:136
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_sendiov(void *s, struct iovec *iov, size_t count, int flags)
Definition: zmq.cpp:449
Definition: zmq.cpp:57
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
void do_check(void *sb, void *sc, size_t msg_size)
Definition: test_iov.cpp:42
#define ZMQ_PULL
Definition: zmq.h:253