mirror of
https://github.com/zeromq/libzmq.git
synced 2025-10-27 19:10:22 +01:00
Problem: tests without test framework
Solution: migrate to unity
This commit is contained in:
@@ -28,33 +28,40 @@
|
||||
*/
|
||||
|
||||
#include "testutil.hpp"
|
||||
#include "testutil_unity.hpp"
|
||||
|
||||
#include <unity.h>
|
||||
|
||||
void setUp ()
|
||||
{
|
||||
setup_test_context ();
|
||||
}
|
||||
|
||||
void tearDown ()
|
||||
{
|
||||
teardown_test_context ();
|
||||
}
|
||||
|
||||
const char *bind_address = 0;
|
||||
char connect_address[MAX_SOCKET_STRING];
|
||||
|
||||
void test_round_robin_out (void *ctx_)
|
||||
void test_round_robin_out (const char *bind_address_)
|
||||
{
|
||||
void *req = zmq_socket (ctx_, ZMQ_REQ);
|
||||
assert (req);
|
||||
void *req = test_context_socket (ZMQ_REQ);
|
||||
|
||||
int rc = zmq_bind (req, bind_address);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, bind_address_));
|
||||
size_t len = MAX_SOCKET_STRING;
|
||||
rc = zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, connect_address, &len);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, connect_address, &len));
|
||||
|
||||
const size_t services = 5;
|
||||
void *rep[services];
|
||||
for (size_t peer = 0; peer < services; peer++) {
|
||||
rep[peer] = zmq_socket (ctx_, ZMQ_REP);
|
||||
assert (rep[peer]);
|
||||
rep[peer] = test_context_socket (ZMQ_REP);
|
||||
|
||||
int timeout = 250;
|
||||
rc = zmq_setsockopt (rep[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
|
||||
assert (rc == 0);
|
||||
|
||||
rc = zmq_connect (rep[peer], connect_address);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_setsockopt (rep[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rep[peer], connect_address));
|
||||
}
|
||||
// We have to give the connects time to finish otherwise the requests
|
||||
// will not properly round-robin. We could alternatively connect the
|
||||
@@ -69,47 +76,37 @@ void test_round_robin_out (void *ctx_)
|
||||
s_recv_seq (req, "DEF", SEQ_END);
|
||||
}
|
||||
|
||||
close_zero_linger (req);
|
||||
test_context_socket_close_zero_linger (req);
|
||||
for (size_t peer = 0; peer < services; peer++)
|
||||
close_zero_linger (rep[peer]);
|
||||
|
||||
// Wait for disconnects.
|
||||
msleep (SETTLE_TIME);
|
||||
test_context_socket_close_zero_linger (rep[peer]);
|
||||
}
|
||||
|
||||
void test_req_only_listens_to_current_peer (void *ctx_)
|
||||
void test_req_only_listens_to_current_peer (const char *bind_address_)
|
||||
{
|
||||
void *req = zmq_socket (ctx_, ZMQ_REQ);
|
||||
assert (req);
|
||||
void *req = test_context_socket (ZMQ_REQ);
|
||||
|
||||
int rc = zmq_setsockopt (req, ZMQ_ROUTING_ID, "A", 2);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (req, ZMQ_ROUTING_ID, "A", 2));
|
||||
|
||||
rc = zmq_bind (req, bind_address);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, bind_address_));
|
||||
size_t len = MAX_SOCKET_STRING;
|
||||
rc = zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, connect_address, &len);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, connect_address, &len));
|
||||
|
||||
const size_t services = 3;
|
||||
void *router[services];
|
||||
|
||||
for (size_t i = 0; i < services; ++i) {
|
||||
router[i] = zmq_socket (ctx_, ZMQ_ROUTER);
|
||||
assert (router[i]);
|
||||
router[i] = test_context_socket (ZMQ_ROUTER);
|
||||
|
||||
int timeout = 250;
|
||||
rc =
|
||||
zmq_setsockopt (router[i], ZMQ_RCVTIMEO, &timeout, sizeof (timeout));
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_setsockopt (router[i], ZMQ_RCVTIMEO, &timeout, sizeof (timeout)));
|
||||
|
||||
int enabled = 1;
|
||||
rc = zmq_setsockopt (router[i], ZMQ_ROUTER_MANDATORY, &enabled,
|
||||
sizeof (enabled));
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
||||
router[i], ZMQ_ROUTER_MANDATORY, &enabled, sizeof (enabled)));
|
||||
|
||||
rc = zmq_connect (router[i], connect_address);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (router[i], connect_address));
|
||||
}
|
||||
|
||||
// Wait for connects to finish.
|
||||
@@ -119,8 +116,8 @@ void test_req_only_listens_to_current_peer (void *ctx_)
|
||||
// There still is a race condition when a stale peer's message
|
||||
// arrives at the REQ just after a request was sent to that peer.
|
||||
// To avoid that happening in the test, sleep for a bit.
|
||||
rc = zmq_poll (0, 0, 10);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_EQUAL_INT (1,
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (0, 0, 10)));
|
||||
|
||||
s_send_seq (req, "ABC", SEQ_END);
|
||||
|
||||
@@ -138,30 +135,22 @@ void test_req_only_listens_to_current_peer (void *ctx_)
|
||||
s_recv_seq (req, "GOOD", SEQ_END);
|
||||
}
|
||||
|
||||
close_zero_linger (req);
|
||||
test_context_socket_close_zero_linger (req);
|
||||
for (size_t i = 0; i < services; ++i)
|
||||
close_zero_linger (router[i]);
|
||||
|
||||
// Wait for disconnects.
|
||||
msleep (SETTLE_TIME);
|
||||
test_context_socket_close_zero_linger (router[i]);
|
||||
}
|
||||
|
||||
void test_req_message_format (void *ctx_)
|
||||
void test_req_message_format (const char *bind_address_)
|
||||
{
|
||||
void *req = zmq_socket (ctx_, ZMQ_REQ);
|
||||
assert (req);
|
||||
void *req = test_context_socket (ZMQ_REQ);
|
||||
void *router = test_context_socket (ZMQ_ROUTER);
|
||||
|
||||
void *router = zmq_socket (ctx_, ZMQ_ROUTER);
|
||||
assert (router);
|
||||
|
||||
int rc = zmq_bind (req, bind_address);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, bind_address_));
|
||||
size_t len = MAX_SOCKET_STRING;
|
||||
rc = zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, connect_address, &len);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, connect_address, &len));
|
||||
|
||||
rc = zmq_connect (router, connect_address);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (router, connect_address));
|
||||
|
||||
// Send a multi-part request.
|
||||
s_send_seq (req, "ABC", "DEF", SEQ_END);
|
||||
@@ -170,99 +159,112 @@ void test_req_message_format (void *ctx_)
|
||||
zmq_msg_init (&msg);
|
||||
|
||||
// Receive peer routing id
|
||||
rc = zmq_msg_recv (&msg, router, 0);
|
||||
assert (rc != -1);
|
||||
assert (zmq_msg_size (&msg) > 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, router, 0));
|
||||
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&msg));
|
||||
zmq_msg_t peer_id_msg;
|
||||
zmq_msg_init (&peer_id_msg);
|
||||
zmq_msg_copy (&peer_id_msg, &msg);
|
||||
|
||||
int more = 0;
|
||||
size_t more_size = sizeof (more);
|
||||
rc = zmq_getsockopt (router, ZMQ_RCVMORE, &more, &more_size);
|
||||
assert (rc == 0);
|
||||
assert (more);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_getsockopt (router, ZMQ_RCVMORE, &more, &more_size));
|
||||
TEST_ASSERT_TRUE (more);
|
||||
|
||||
// Receive the rest.
|
||||
s_recv_seq (router, 0, "ABC", "DEF", SEQ_END);
|
||||
|
||||
// Send back a single-part reply.
|
||||
rc = zmq_msg_send (&peer_id_msg, router, ZMQ_SNDMORE);
|
||||
assert (rc != -1);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_msg_send (&peer_id_msg, router, ZMQ_SNDMORE));
|
||||
s_send_seq (router, 0, "GHI", SEQ_END);
|
||||
|
||||
// Receive reply.
|
||||
s_recv_seq (req, "GHI", SEQ_END);
|
||||
|
||||
rc = zmq_msg_close (&msg);
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_id_msg));
|
||||
|
||||
rc = zmq_msg_close (&peer_id_msg);
|
||||
assert (rc == 0);
|
||||
|
||||
close_zero_linger (req);
|
||||
close_zero_linger (router);
|
||||
|
||||
// Wait for disconnects.
|
||||
msleep (SETTLE_TIME);
|
||||
test_context_socket_close_zero_linger (req);
|
||||
test_context_socket_close_zero_linger (router);
|
||||
}
|
||||
|
||||
void test_block_on_send_no_peers (void *ctx_)
|
||||
void test_block_on_send_no_peers ()
|
||||
{
|
||||
void *sc = zmq_socket (ctx_, ZMQ_REQ);
|
||||
assert (sc);
|
||||
void *sc = test_context_socket (ZMQ_REQ);
|
||||
|
||||
int timeout = 250;
|
||||
int rc = zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (timeout));
|
||||
assert (rc == 0);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (timeout)));
|
||||
|
||||
rc = zmq_send (sc, 0, 0, ZMQ_DONTWAIT);
|
||||
assert (rc == -1);
|
||||
assert (errno == EAGAIN);
|
||||
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (sc, 0, 0, ZMQ_DONTWAIT));
|
||||
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (sc, 0, 0, 0));
|
||||
|
||||
rc = zmq_send (sc, 0, 0, 0);
|
||||
assert (rc == -1);
|
||||
assert (errno == EAGAIN);
|
||||
|
||||
rc = zmq_close (sc);
|
||||
assert (rc == 0);
|
||||
test_context_socket_close (sc);
|
||||
}
|
||||
|
||||
int main (void)
|
||||
const char bind_inproc[] = "inproc://a";
|
||||
const char bind_tcp[] = "tcp://127.0.0.1:*";
|
||||
|
||||
void test_round_robin_out_inproc ()
|
||||
{
|
||||
test_round_robin_out (bind_inproc);
|
||||
}
|
||||
|
||||
void test_round_robin_out_tcp ()
|
||||
{
|
||||
test_round_robin_out (bind_tcp);
|
||||
}
|
||||
|
||||
void test_req_message_format_inproc ()
|
||||
{
|
||||
test_req_message_format (bind_inproc);
|
||||
}
|
||||
|
||||
void test_req_message_format_tcp ()
|
||||
{
|
||||
test_req_message_format (bind_tcp);
|
||||
}
|
||||
|
||||
void test_req_only_listens_to_current_peer_inproc ()
|
||||
{
|
||||
test_req_only_listens_to_current_peer (bind_inproc);
|
||||
}
|
||||
|
||||
void test_req_only_listens_to_current_peer_tcp ()
|
||||
{
|
||||
test_req_only_listens_to_current_peer (bind_tcp);
|
||||
}
|
||||
|
||||
int main ()
|
||||
{
|
||||
setup_test_environment ();
|
||||
void *ctx = zmq_ctx_new ();
|
||||
assert (ctx);
|
||||
|
||||
const char *binds[] = {"inproc://a", "tcp://127.0.0.1:*"};
|
||||
UNITY_BEGIN ();
|
||||
|
||||
for (int transport = 0; transport < 2; transport++) {
|
||||
bind_address = binds[transport];
|
||||
// SHALL route outgoing messages to connected peers using a round-robin
|
||||
// strategy.
|
||||
RUN_TEST (test_round_robin_out_inproc);
|
||||
RUN_TEST (test_round_robin_out_tcp);
|
||||
|
||||
// SHALL route outgoing messages to connected peers using a round-robin
|
||||
// strategy.
|
||||
test_round_robin_out (ctx);
|
||||
// The request and reply messages SHALL have this format on the wire:
|
||||
// * A delimiter, consisting of an empty frame, added by the REQ socket.
|
||||
// * One or more data frames, comprising the message visible to the
|
||||
// application.
|
||||
RUN_TEST (test_req_message_format_inproc);
|
||||
RUN_TEST (test_req_message_format_tcp);
|
||||
|
||||
// The request and reply messages SHALL have this format on the wire:
|
||||
// * A delimiter, consisting of an empty frame, added by the REQ socket.
|
||||
// * One or more data frames, comprising the message visible to the
|
||||
// application.
|
||||
test_req_message_format (ctx);
|
||||
// SHALL block on sending, or return a suitable error, when it has no
|
||||
// connected peers.
|
||||
RUN_TEST (test_block_on_send_no_peers);
|
||||
|
||||
// SHALL block on sending, or return a suitable error, when it has no
|
||||
// connected peers.
|
||||
test_block_on_send_no_peers (ctx);
|
||||
// SHALL accept an incoming message only from the last peer that it sent a
|
||||
// request to.
|
||||
// SHALL discard silently any messages received from other peers.
|
||||
// TODO PH: this test is still failing; disabled for now to allow build to
|
||||
// complete.
|
||||
// RUN_TEST (test_req_only_listens_to_current_peer_inproc);
|
||||
// RUN_TEST (test_req_only_listens_to_current_peer_tcp);
|
||||
|
||||
// SHALL accept an incoming message only from the last peer that it sent a
|
||||
// request to.
|
||||
// SHALL discard silently any messages received from other peers.
|
||||
// PH: this test is still failing; disabled for now to allow build to
|
||||
// complete.
|
||||
// test_req_only_listens_to_current_peer (ctx);
|
||||
}
|
||||
|
||||
int rc = zmq_ctx_term (ctx);
|
||||
assert (rc == 0);
|
||||
|
||||
return 0;
|
||||
return UNITY_END ();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user