test_spec_req.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include "testutil_unity.hpp"
5 
7 
9 
10 void test_round_robin_out (const char *bind_address_)
11 {
13 
14  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, bind_address_));
15  size_t len = MAX_SOCKET_STRING;
18 
19  const size_t services = 5;
20  void *rep[services];
21  for (size_t peer = 0; peer < services; peer++) {
23 
25  }
26  // We have to give the connects time to finish otherwise the requests
27  // will not properly round-robin. We could alternatively connect the
28  // REQ sockets to the REP sockets.
30 
31  // Send our peer-replies, and expect every REP it used once in order
32  for (size_t peer = 0; peer < services; peer++) {
33  s_send_seq (req, "ABC", SEQ_END);
34  s_recv_seq (rep[peer], "ABC", SEQ_END);
35  s_send_seq (rep[peer], "DEF", SEQ_END);
36  s_recv_seq (req, "DEF", SEQ_END);
37  }
38 
40  for (size_t peer = 0; peer < services; peer++)
42 }
43 
44 void test_req_only_listens_to_current_peer (const char *bind_address_)
45 {
47 
49 
50  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, bind_address_));
51  size_t len = MAX_SOCKET_STRING;
54 
55  const size_t services = 3;
56  void *router[services];
57 
58  for (size_t i = 0; i < services; ++i) {
59  router[i] = test_context_socket (ZMQ_ROUTER);
60 
61  int enabled = 1;
63  router[i], ZMQ_ROUTER_MANDATORY, &enabled, sizeof (enabled)));
64 
66  }
67 
68  // Wait for connects to finish.
70 
71  for (size_t i = 0; i < services; ++i) {
72  // There still is a race condition when a stale peer's message
73  // arrives at the REQ just after a request was sent to that peer.
74  // To avoid that happening in the test, sleep for a bit.
76  TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (0, 0, 10)));
77 
78  s_send_seq (req, "ABC", SEQ_END);
79 
80  // Receive on router i
81  s_recv_seq (router[i], "A", 0, "ABC", SEQ_END);
82 
83  // Send back replies on all routers
84  for (size_t j = 0; j < services; ++j) {
85  const char *replies[] = {"WRONG", "GOOD"};
86  const char *reply = replies[i == j ? 1 : 0];
87  s_send_seq (router[j], "A", 0, reply, SEQ_END);
88  }
89 
90  // Receive only the good reply
91  s_recv_seq (req, "GOOD", SEQ_END);
92  }
93 
95  for (size_t i = 0; i < services; ++i)
97 }
98 
99 void test_req_message_format (const char *bind_address_)
100 {
101  void *req = test_context_socket (ZMQ_REQ);
102  void *router = test_context_socket (ZMQ_ROUTER);
103 
104  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, bind_address_));
105  size_t len = MAX_SOCKET_STRING;
108 
110 
111  // Send a multi-part request.
112  s_send_seq (req, "ABC", "DEF", SEQ_END);
113 
114  zmq_msg_t msg;
115  zmq_msg_init (&msg);
116 
117  // Receive peer routing id
118  TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, router, 0));
120  zmq_msg_t peer_id_msg;
121  zmq_msg_init (&peer_id_msg);
122  zmq_msg_copy (&peer_id_msg, &msg);
123 
124  int more = 0;
125  size_t more_size = sizeof (more);
127  zmq_getsockopt (router, ZMQ_RCVMORE, &more, &more_size));
128  TEST_ASSERT_TRUE (more);
129 
130  // Receive the rest.
131  s_recv_seq (router, 0, "ABC", "DEF", SEQ_END);
132 
133  // Send back a single-part reply.
135  zmq_msg_send (&peer_id_msg, router, ZMQ_SNDMORE));
136  s_send_seq (router, 0, "GHI", SEQ_END);
137 
138  // Receive reply.
139  s_recv_seq (req, "GHI", SEQ_END);
140 
142  TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_id_msg));
143 
146 }
147 
149 {
150  void *sc = test_context_socket (ZMQ_REQ);
151 
152  int timeout = 250;
154  zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (timeout)));
155 
158 
160 }
161 
162 const char bind_inproc[] = "inproc://a";
163 const char bind_tcp[] = "tcp://127.0.0.1:*";
164 
166 {
168 }
169 
171 {
173 }
174 
176 {
178 }
179 
181 {
183 }
184 
186 {
188 }
189 
191 {
193 }
194 
195 int main ()
196 {
198 
199  UNITY_BEGIN ();
200 
201  // SHALL route outgoing messages to connected peers using a round-robin
202  // strategy.
205 
206  // The request and reply messages SHALL have this format on the wire:
207  // * A delimiter, consisting of an empty frame, added by the REQ socket.
208  // * One or more data frames, comprising the message visible to the
209  // application.
212 
213  // SHALL block on sending, or return a suitable error, when it has no
214  // connected peers.
216 
217  // SHALL accept an incoming message only from the last peer that it sent a
218  // request to.
219  // SHALL discard silently any messages received from other peers.
220  // TODO PH: this test is still failing; disabled for now to allow build to
221  // complete.
222  // RUN_TEST (test_req_only_listens_to_current_peer_inproc);
223  // RUN_TEST (test_req_only_listens_to_current_peer_tcp);
224 
225  return UNITY_END ();
226 }
main
int main()
Definition: test_spec_req.cpp:195
UNITY_END
return UNITY_END()
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
TEST_ASSERT_TRUE
#define TEST_ASSERT_TRUE(condition)
Definition: unity.h:121
zmq_msg_send
ZMQ_EXPORT int zmq_msg_send(zmq_msg_t *msg_, void *s_, int flags_)
Definition: zmq.cpp:609
test_req_message_format_tcp
void test_req_message_format_tcp()
Definition: test_spec_req.cpp:180
zmq_poll
ZMQ_EXPORT int zmq_poll(zmq_pollitem_t *items_, int nitems_, long timeout_)
Definition: zmq.cpp:827
EAGAIN
#define EAGAIN
Definition: errno.hpp:14
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
test_req_message_format
void test_req_message_format(const char *bind_address_)
Definition: test_spec_req.cpp:99
ZMQ_LAST_ENDPOINT
#define ZMQ_LAST_ENDPOINT
Definition: zmq.h:298
test_req_only_listens_to_current_peer_inproc
void test_req_only_listens_to_current_peer_inproc()
Definition: test_spec_req.cpp:185
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
rep
void * rep[services]
Definition: test_req_relaxed.cpp:11
services
const size_t services
Definition: test_req_relaxed.cpp:8
test_context_socket_close_zero_linger
void * test_context_socket_close_zero_linger(void *socket_)
Definition: testutil_unity.cpp:215
ZMQ_REQ
#define ZMQ_REQ
Definition: zmq.h:261
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
zmq_msg_size
ZMQ_EXPORT size_t zmq_msg_size(const zmq_msg_t *msg_)
Definition: zmq.cpp:647
enabled
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: glcorearb.h:4174
TEST_ASSERT_GREATER_THAN_INT
#define TEST_ASSERT_GREATER_THAN_INT(threshold, actual)
Definition: unity.h:153
test_round_robin_out_tcp
void test_round_robin_out_tcp()
Definition: test_spec_req.cpp:170
testutil_unity.hpp
test_round_robin_out_inproc
void test_round_robin_out_inproc()
Definition: test_spec_req.cpp:165
test_block_on_send_no_peers
void test_block_on_send_no_peers()
Definition: test_spec_req.cpp:148
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
ZMQ_SNDTIMEO
#define ZMQ_SNDTIMEO
Definition: zmq.h:297
testutil.hpp
ZMQ_ROUTER
#define ZMQ_ROUTER
Definition: zmq.h:264
zmq_msg_t
Definition: zmq.h:218
ZMQ_REP
#define ZMQ_REP
Definition: zmq.h:262
connect_address
SETUP_TEARDOWN_TESTCONTEXT char connect_address[MAX_SOCKET_STRING]
Definition: test_spec_req.cpp:8
test_req_only_listens_to_current_peer_tcp
void test_req_only_listens_to_current_peer_tcp()
Definition: test_spec_req.cpp:190
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
zmq_msg_recv
ZMQ_EXPORT int zmq_msg_recv(zmq_msg_t *msg_, void *s_, int flags_)
Definition: zmq.cpp:617
test_req_only_listens_to_current_peer
void test_req_only_listens_to_current_peer(const char *bind_address_)
Definition: test_spec_req.cpp:44
timeout
GLbitfield GLuint64 timeout
Definition: glcorearb.h:3588
sc
void * sc
Definition: test_channel.cpp:9
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
zmq_msg_init
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg_)
Definition: zmq.cpp:587
i
int i
Definition: gmock-matchers_test.cc:764
ZMQ_ROUTER_MANDATORY
#define ZMQ_ROUTER_MANDATORY
Definition: zmq.h:299
SEQ_END
const char * SEQ_END
Definition: testutil.cpp:47
req
void * req
Definition: test_req_relaxed.cpp:10
len
int len
Definition: php/ext/google/protobuf/map.c:206
s_send_seq
void s_send_seq(void *socket_,...)
Definition: testutil.cpp:135
test_req_message_format_inproc
void test_req_message_format_inproc()
Definition: test_spec_req.cpp:175
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
s_recv_seq
void s_recv_seq(void *socket_,...)
Definition: testutil.cpp:158
zmq_msg_copy
ZMQ_EXPORT int zmq_msg_copy(zmq_msg_t *dest_, zmq_msg_t *src_)
Definition: zmq.cpp:636
ZMQ_SNDMORE
#define ZMQ_SNDMORE
Definition: zmq.h:359
TEST_ASSERT_FAILURE_ERRNO
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:95
ZMQ_ROUTING_ID
#define ZMQ_ROUTING_ID
Definition: zmq.h:277
bind_inproc
const char bind_inproc[]
Definition: test_spec_req.cpp:162
test_round_robin_out
void test_round_robin_out(const char *bind_address_)
Definition: test_spec_req.cpp:10
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
bind_tcp
const char bind_tcp[]
Definition: test_spec_req.cpp:163
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
ZMQ_RCVMORE
#define ZMQ_RCVMORE
Definition: zmq.h:284
zmq_msg_close
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg_)
Definition: zmq.cpp:625
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
zmq_getsockopt
ZMQ_EXPORT int zmq_getsockopt(void *s_, int option_, void *optval_, size_t *optvallen_)
Definition: zmq.cpp:261


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59