Commit fc334bc7 authored by sigiesec's avatar sigiesec

Problem: unclean and duplicated test code

Solution: refactoring
parent f70097c1
...@@ -29,6 +29,26 @@ ...@@ -29,6 +29,26 @@
#include "testutil.hpp" #include "testutil.hpp"
#ifdef ZMQ_BUILD_DRAFT_API
bool send_msg_to_peer_if_ready (void *router, const char *peer_identity)
{
int rc = zmq_socket_get_peer_state (router, peer_identity, 1);
if (rc == -1)
printf ("zmq_socket_get_peer_state failed for %s: %i\n", peer_identity,
errno);
assert (rc != -1);
if (rc & ZMQ_POLLOUT) {
rc = zmq_send (router, peer_identity, 1, ZMQ_SNDMORE | ZMQ_DONTWAIT);
assert (rc == 1);
rc = zmq_send (router, "Hello", 5, ZMQ_DONTWAIT);
assert (rc == 5);
return true;
}
return false;
}
#endif
void test_get_peer_state () void test_get_peer_state ()
{ {
#ifdef ZMQ_BUILD_DRAFT_API #ifdef ZMQ_BUILD_DRAFT_API
...@@ -59,18 +79,24 @@ void test_get_peer_state () ...@@ -59,18 +79,24 @@ void test_get_peer_state ()
// Lower HWMs to allow doing the test with fewer messages // Lower HWMs to allow doing the test with fewer messages
int hwm = 100; int hwm = 100;
zmq_setsockopt (router, ZMQ_SNDHWM, &hwm, sizeof (int)); rc = zmq_setsockopt (router, ZMQ_SNDHWM, &hwm, sizeof (int));
zmq_setsockopt (dealer1, ZMQ_RCVHWM, &hwm, sizeof (int)); assert (rc == 0);
zmq_setsockopt (dealer2, ZMQ_RCVHWM, &hwm, sizeof (int)); rc = zmq_setsockopt (dealer1, ZMQ_RCVHWM, &hwm, sizeof (int));
assert (rc == 0);
rc = zmq_setsockopt (dealer2, ZMQ_RCVHWM, &hwm, sizeof (int));
assert (rc == 0);
const char *dealer1_identity = "X";
const char *dealer2_identity = "Y";
// Name dealer1 "X" and connect it to our router // Name dealer1 "X" and connect it to our router
rc = zmq_setsockopt (dealer1, ZMQ_IDENTITY, "X", 1); rc = zmq_setsockopt (dealer1, ZMQ_IDENTITY, dealer1_identity, 1);
assert (rc == 0); assert (rc == 0);
rc = zmq_connect (dealer1, my_endpoint); rc = zmq_connect (dealer1, my_endpoint);
assert (rc == 0); assert (rc == 0);
// Name dealer2 "Y" and connect it to our router // Name dealer2 "Y" and connect it to our router
rc = zmq_setsockopt (dealer2, ZMQ_IDENTITY, "Y", 1); rc = zmq_setsockopt (dealer2, ZMQ_IDENTITY, dealer2_identity, 1);
assert (rc == 0); assert (rc == 0);
rc = zmq_connect (dealer2, my_endpoint); rc = zmq_connect (dealer2, my_endpoint);
assert (rc == 0); assert (rc == 0);
...@@ -81,7 +107,7 @@ void test_get_peer_state () ...@@ -81,7 +107,7 @@ void test_get_peer_state ()
assert (rc == 5); assert (rc == 5);
rc = zmq_recv (router, buffer, 255, 0); rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 1); assert (rc == 1);
assert (buffer[0] == 'X'); assert (0 == memcmp (buffer, dealer1_identity, rc));
rc = zmq_recv (router, buffer, 255, 0); rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 5); assert (rc == 5);
...@@ -89,7 +115,7 @@ void test_get_peer_state () ...@@ -89,7 +115,7 @@ void test_get_peer_state ()
assert (rc == 5); assert (rc == 5);
rc = zmq_recv (router, buffer, 255, 0); rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 1); assert (rc == 1);
assert (buffer[0] == 'Y'); assert (0 == memcmp (buffer, dealer2_identity, rc));
rc = zmq_recv (router, buffer, 255, 0); rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 5); assert (rc == 5);
...@@ -107,41 +133,30 @@ void test_get_peer_state () ...@@ -107,41 +133,30 @@ void test_get_peer_state ()
bool dealer2_blocked = false; bool dealer2_blocked = false;
size_t dealer1_sent = 0, dealer2_sent = 0, dealer1_received = 0; size_t dealer1_sent = 0, dealer2_sent = 0, dealer1_received = 0;
zmq_poller_event_t events[event_size]; zmq_poller_event_t events[event_size];
for (size_t iterations = 0; iterations < count; ++iterations) { for (size_t iteration = 0; iteration < count; ++iteration) {
rc = zmq_poller_wait_all (poller, events, event_size, -1); rc = zmq_poller_wait_all (poller, events, event_size, -1);
assert (rc != -1); assert (rc != -1);
for (size_t i = 0; i < event_size; ++i) { for (size_t event_no = 0; event_no < event_size; ++event_no) {
if (events[i].socket == router && events[i].events & ZMQ_POLLOUT) { const zmq_poller_event_t &current_event = events[event_no];
rc = zmq_socket_get_peer_state (router, "X", 1); if (current_event.socket == router
if (rc == -1) && current_event.events & ZMQ_POLLOUT) {
printf ("zmq_socket_get_peer_state failed: %i\n", errno); if (send_msg_to_peer_if_ready (router, dealer1_identity))
assert (rc != -1);
if (rc & ZMQ_POLLOUT) {
rc = zmq_send (router, "X", 1, ZMQ_SNDMORE | ZMQ_DONTWAIT);
assert (rc == 1);
rc = zmq_send (router, "Hello", 5, ZMQ_DONTWAIT);
assert (rc == 5);
++dealer1_sent; ++dealer1_sent;
}
rc = zmq_socket_get_peer_state (router, "Y", 1); if (send_msg_to_peer_if_ready (router, dealer2_identity))
if (rc == -1)
printf ("zmq_socket_get_peer_state failed: %i\n", errno);
assert (rc != -1);
if (rc & ZMQ_POLLOUT) {
rc = zmq_send (router, "Y", 1, ZMQ_SNDMORE | ZMQ_DONTWAIT);
assert (rc == 1);
rc = zmq_send (router, "Hello", 5, ZMQ_DONTWAIT);
assert (rc == 5);
++dealer2_sent; ++dealer2_sent;
} else { else
dealer2_blocked = true; dealer2_blocked = true;
} }
} if (current_event.socket == dealer1
if (events[i].socket == dealer1 && events[i].events & ZMQ_POLLIN) { && current_event.events & ZMQ_POLLIN) {
rc = zmq_recv (dealer1, buffer, 255, ZMQ_DONTWAIT); rc = zmq_recv (dealer1, buffer, 255, ZMQ_DONTWAIT);
assert (rc == 5); assert (rc == 5);
int more;
size_t more_size = sizeof (more);
rc = zmq_getsockopt (dealer1, ZMQ_RCVMORE, &more, &more_size);
assert (rc == 0);
assert (!more);
++dealer1_received; ++dealer1_received;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment