Commit be66eacf authored by Simon Giesecke's avatar Simon Giesecke

Problem: test_heartbeats still using plain assertions

Solution: migrate to unity assertions
parent b331caad
...@@ -29,16 +29,16 @@ typedef SOCKET raw_socket; ...@@ -29,16 +29,16 @@ typedef SOCKET raw_socket;
typedef int raw_socket; typedef int raw_socket;
#endif #endif
#include <unity.h> #include "testutil_unity.hpp"
void setUp () void setUp ()
{ {
// setup_test_context (); setup_test_context ();
} }
void tearDown () void tearDown ()
{ {
// teardown_test_context (); teardown_test_context ();
} }
...@@ -51,24 +51,22 @@ static int get_monitor_event (void *monitor) ...@@ -51,24 +51,22 @@ static int get_monitor_event (void *monitor)
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
// First frame in message contains event number and value // First frame in message contains event number and value
zmq_msg_t msg; zmq_msg_t msg;
int rc = zmq_msg_init (&msg); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
assert (rc == 0);
if (zmq_msg_recv (&msg, monitor, ZMQ_DONTWAIT) == -1) { if (zmq_msg_recv (&msg, monitor, ZMQ_DONTWAIT) == -1) {
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
continue; // Interruped, presumably continue; // Interrupted, presumably
} }
assert (zmq_msg_more (&msg)); TEST_ASSERT_TRUE (zmq_msg_more (&msg));
uint8_t *data = (uint8_t *) zmq_msg_data (&msg); uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
uint16_t event = *(uint16_t *) (data); uint16_t event = *(uint16_t *) (data);
// Second frame in message contains event address // Second frame in message contains event address
rc = zmq_msg_init (&msg); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
assert (rc == 0);
if (zmq_msg_recv (&msg, monitor, 0) == -1) { if (zmq_msg_recv (&msg, monitor, 0) == -1) {
return -1; // Interruped, presumably return -1; // Interrupted, presumably
} }
assert (!zmq_msg_more (&msg)); TEST_ASSERT_FALSE (zmq_msg_more (&msg));
return event; return event;
} }
...@@ -79,10 +77,11 @@ static void recv_with_retry (raw_socket fd, char *buffer, int bytes) ...@@ -79,10 +77,11 @@ static void recv_with_retry (raw_socket fd, char *buffer, int bytes)
{ {
int received = 0; int received = 0;
while (true) { while (true) {
int rc = recv (fd, buffer + received, bytes - received, 0); int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
assert (rc > 0); recv (fd, buffer + received, bytes - received, 0));
TEST_ASSERT_GREATER_THAN_INT (0, rc);
received += rc; received += rc;
assert (received <= bytes); TEST_ASSERT_LESS_OR_EQUAL_INT (bytes, received);
if (received == bytes) if (received == bytes)
break; break;
} }
...@@ -96,8 +95,8 @@ static void mock_handshake (raw_socket fd, int mock_ping) ...@@ -96,8 +95,8 @@ static void mock_handshake (raw_socket fd, int mock_ping)
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
memcpy (buffer, zmtp_greeting, sizeof (zmtp_greeting)); memcpy (buffer, zmtp_greeting, sizeof (zmtp_greeting));
int rc = send (fd, buffer, 64, 0); int rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd, buffer, 64, 0));
assert (rc == 64); TEST_ASSERT_EQUAL_INT (64, rc);
recv_with_retry (fd, buffer, 64); recv_with_retry (fd, buffer, 64);
...@@ -108,8 +107,8 @@ static void mock_handshake (raw_socket fd, int mock_ping) ...@@ -108,8 +107,8 @@ static void mock_handshake (raw_socket fd, int mock_ping)
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
memcpy (buffer, zmtp_ready, 43); memcpy (buffer, zmtp_ready, 43);
rc = send (fd, buffer, 43, 0); rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd, buffer, 43, 0));
assert (rc == 43); TEST_ASSERT_EQUAL_INT (43, rc);
// greeting // greeting
recv_with_retry (fd, buffer, 43); recv_with_retry (fd, buffer, 43);
...@@ -122,25 +121,25 @@ static void mock_handshake (raw_socket fd, int mock_ping) ...@@ -122,25 +121,25 @@ static void mock_handshake (raw_socket fd, int mock_ping)
uint8_t zmtp_pong[10] = {4, 8, 4, 'P', 'O', 'N', 'G', 'L', 'O', 'L'}; uint8_t zmtp_pong[10] = {4, 8, 4, 'P', 'O', 'N', 'G', 'L', 'O', 'L'};
memset (buffer, 0, sizeof (buffer)); memset (buffer, 0, sizeof (buffer));
memcpy (buffer, zmtp_ping, 12); memcpy (buffer, zmtp_ping, 12);
rc = send (fd, buffer, 12, 0); rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd, buffer, 12, 0));
assert (rc == 12); TEST_ASSERT_EQUAL_INT (12, rc);
// test a larger body that won't fit in a small message and should get // test a larger body that won't fit in a small message and should get
// truncated // truncated
memset (buffer, 'z', sizeof (buffer)); memset (buffer, 'z', sizeof (buffer));
memcpy (buffer, zmtp_ping, 12); memcpy (buffer, zmtp_ping, 12);
buffer[1] = 65; buffer[1] = 65;
rc = send (fd, buffer, 67, 0); rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (send (fd, buffer, 67, 0));
assert (rc == 67); TEST_ASSERT_EQUAL_INT (67, rc);
// small pong // small pong
recv_with_retry (fd, buffer, 10); recv_with_retry (fd, buffer, 10);
assert (memcmp (zmtp_pong, buffer, 10) == 0); TEST_ASSERT_EQUAL_INT (0, memcmp (zmtp_pong, buffer, 10));
// large pong // large pong
recv_with_retry (fd, buffer, 23); recv_with_retry (fd, buffer, 23);
uint8_t zmtp_pooong[65] = {4, 21, 4, 'P', 'O', 'N', 'G', 'L', 'O', 'L'}; uint8_t zmtp_pooong[65] = {4, 21, 4, 'P', 'O', 'N', 'G', 'L', 'O', 'L'};
memset (zmtp_pooong + 10, 'z', 55); memset (zmtp_pooong + 10, 'z', 55);
assert (memcmp (zmtp_pooong, buffer, 23) == 0); TEST_ASSERT_EQUAL_INT (0, memcmp (zmtp_pooong, buffer, 23));
} }
} }
...@@ -172,8 +171,7 @@ static void setup_curve (void *socket, int is_server) ...@@ -172,8 +171,7 @@ static void setup_curve (void *socket, int is_server)
strlen (server_key)); strlen (server_key));
} }
static void prep_server_socket (void *ctx, static void prep_server_socket (int set_heartbeats,
int set_heartbeats,
int is_curve, int is_curve,
void **server_out, void **server_out,
void **mon_out, void **mon_out,
...@@ -181,41 +179,34 @@ static void prep_server_socket (void *ctx, ...@@ -181,41 +179,34 @@ static void prep_server_socket (void *ctx,
size_t ep_length, size_t ep_length,
int socket_type) int socket_type)
{ {
int rc;
// We'll be using this socket in raw mode // We'll be using this socket in raw mode
void *server = zmq_socket (ctx, socket_type); void *server = test_context_socket (socket_type);
assert (server);
int value = 0; int value = 0;
rc = zmq_setsockopt (server, ZMQ_LINGER, &value, sizeof (value)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (server, ZMQ_LINGER, &value, sizeof (value)));
if (set_heartbeats) { if (set_heartbeats) {
value = 50; value = 50;
rc = zmq_setsockopt (server, ZMQ_HEARTBEAT_IVL, &value, sizeof (value)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (server, ZMQ_HEARTBEAT_IVL, &value, sizeof (value)));
} }
if (is_curve) if (is_curve)
setup_curve (server, 1); setup_curve (server, 1);
rc = zmq_bind (server, "tcp://127.0.0.1:*"); bind_loopback_ipv4 (server, endpoint, ep_length);
assert (rc == 0);
rc = zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, endpoint, &ep_length);
assert (rc == 0);
// Create and connect a socket for collecting monitor events on dealer // Create and connect a socket for collecting monitor events on dealer
void *server_mon = zmq_socket (ctx, ZMQ_PAIR); void *server_mon = test_context_socket (ZMQ_PAIR);
assert (server_mon);
rc = zmq_socket_monitor (server, "inproc://monitor-dealer", TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED server, "inproc://monitor-dealer",
| ZMQ_EVENT_ACCEPTED); ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED));
assert (rc == 0);
// Connect to the inproc endpoint so we'll get events // Connect to the inproc endpoint so we'll get events
rc = zmq_connect (server_mon, "inproc://monitor-dealer"); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_connect (server_mon, "inproc://monitor-dealer"));
*server_out = server; *server_out = server;
*mon_out = server_mon; *mon_out = server_mon;
...@@ -229,12 +220,8 @@ static void test_heartbeat_timeout (int server_type, int mock_ping) ...@@ -229,12 +220,8 @@ static void test_heartbeat_timeout (int server_type, int mock_ping)
int rc; int rc;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);
void *server, *server_mon; void *server, *server_mon;
prep_server_socket (ctx, !mock_ping, 0, &server, &server_mon, my_endpoint, prep_server_socket (!mock_ping, 0, &server, &server_mon, my_endpoint,
MAX_SOCKET_STRING, server_type); MAX_SOCKET_STRING, server_type);
struct sockaddr_in ip4addr; struct sockaddr_in ip4addr;
...@@ -249,32 +236,27 @@ static void test_heartbeat_timeout (int server_type, int mock_ping) ...@@ -249,32 +236,27 @@ static void test_heartbeat_timeout (int server_type, int mock_ping)
#endif #endif
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
rc = connect (s, (struct sockaddr *) &ip4addr, sizeof ip4addr); rc = TEST_ASSERT_SUCCESS_RAW_ERRNO (
assert (rc > -1); connect (s, (struct sockaddr *) &ip4addr, sizeof ip4addr));
TEST_ASSERT_GREATER_THAN_INT (-1, rc);
// Mock a ZMTP 3 client so we can forcibly time out a connection // Mock a ZMTP 3 client so we can forcibly time out a connection
mock_handshake (s, mock_ping); mock_handshake (s, mock_ping);
// By now everything should report as connected // By now everything should report as connected
rc = get_monitor_event (server_mon); rc = get_monitor_event (server_mon);
assert (rc == ZMQ_EVENT_ACCEPTED); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc);
if (!mock_ping) { if (!mock_ping) {
// We should have been disconnected // We should have been disconnected
rc = get_monitor_event (server_mon); rc = get_monitor_event (server_mon);
assert (rc == ZMQ_EVENT_DISCONNECTED); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, rc);
} }
close (s); close (s);
rc = zmq_close (server); test_context_socket_close (server);
assert (rc == 0); test_context_socket_close (server_mon);
rc = zmq_close (server_mon);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
} }
// This checks that peers respect the TTL value in ping messages // This checks that peers respect the TTL value in ping messages
...@@ -287,52 +269,38 @@ static void test_heartbeat_ttl (int client_type, int server_type) ...@@ -287,52 +269,38 @@ static void test_heartbeat_ttl (int client_type, int server_type)
int rc, value; int rc, value;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);
void *server, *server_mon, *client; void *server, *server_mon, *client;
prep_server_socket (ctx, 0, 0, &server, &server_mon, my_endpoint, prep_server_socket (0, 0, &server, &server_mon, my_endpoint,
MAX_SOCKET_STRING, server_type); MAX_SOCKET_STRING, server_type);
client = zmq_socket (ctx, client_type); client = test_context_socket (client_type);
assert (client != NULL);
// Set the heartbeat TTL to 0.1 seconds // Set the heartbeat TTL to 0.1 seconds
value = 100; value = 100;
rc = zmq_setsockopt (client, ZMQ_HEARTBEAT_TTL, &value, sizeof (value)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (client, ZMQ_HEARTBEAT_TTL, &value, sizeof (value)));
// Set the heartbeat interval to much longer than the TTL so that // Set the heartbeat interval to much longer than the TTL so that
// the socket times out oon the remote side. // the socket times out oon the remote side.
value = 250; value = 250;
rc = zmq_setsockopt (client, ZMQ_HEARTBEAT_IVL, &value, sizeof (value)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (client, ZMQ_HEARTBEAT_IVL, &value, sizeof (value)));
rc = zmq_connect (client, my_endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
assert (rc == 0);
// By now everything should report as connected // By now everything should report as connected
rc = get_monitor_event (server_mon); rc = get_monitor_event (server_mon);
assert (rc == ZMQ_EVENT_ACCEPTED); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc);
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
// We should have been disconnected // We should have been disconnected
rc = get_monitor_event (server_mon); rc = get_monitor_event (server_mon);
assert (rc == ZMQ_EVENT_DISCONNECTED); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, rc);
rc = zmq_close (server);
assert (rc == 0);
rc = zmq_close (server_mon); test_context_socket_close (server);
assert (rc == 0); test_context_socket_close (server_mon);
test_context_socket_close (client);
rc = zmq_close (client);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
} }
// This checks for normal operation - that is pings and pongs being // This checks for normal operation - that is pings and pongs being
...@@ -344,15 +312,11 @@ test_heartbeat_notimeout (int is_curve, int client_type, int server_type) ...@@ -344,15 +312,11 @@ test_heartbeat_notimeout (int is_curve, int client_type, int server_type)
int rc; int rc;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);
void *server, *server_mon; void *server, *server_mon;
prep_server_socket (ctx, 1, is_curve, &server, &server_mon, my_endpoint, prep_server_socket (1, is_curve, &server, &server_mon, my_endpoint,
MAX_SOCKET_STRING, server_type); MAX_SOCKET_STRING, server_type);
void *client = zmq_socket (ctx, client_type); void *client = test_context_socket (client_type);
if (is_curve) if (is_curve)
setup_curve (client, 0); setup_curve (client, 0);
rc = zmq_connect (client, my_endpoint); rc = zmq_connect (client, my_endpoint);
...@@ -362,23 +326,14 @@ test_heartbeat_notimeout (int is_curve, int client_type, int server_type) ...@@ -362,23 +326,14 @@ test_heartbeat_notimeout (int is_curve, int client_type, int server_type)
// By now everything should report as connected // By now everything should report as connected
rc = get_monitor_event (server_mon); rc = get_monitor_event (server_mon);
assert (rc == ZMQ_EVENT_ACCEPTED); TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, rc);
// We should still be connected because pings and pongs are happenin' // We should still be connected because pings and pongs are happenin'
rc = get_monitor_event (server_mon); TEST_ASSERT_EQUAL_INT (-1, get_monitor_event (server_mon));
assert (rc == -1);
rc = zmq_close (client);
assert (rc == 0);
rc = zmq_close (server);
assert (rc == 0);
rc = zmq_close (server_mon);
assert (rc == 0);
rc = zmq_ctx_term (ctx); test_context_socket_close (client);
assert (rc == 0); test_context_socket_close (server);
test_context_socket_close (server_mon);
} }
void test_heartbeat_timeout_router () void test_heartbeat_timeout_router ()
......
...@@ -57,12 +57,37 @@ int test_assert_success_message_errno_helper (int rc, ...@@ -57,12 +57,37 @@ int test_assert_success_message_errno_helper (int rc,
return rc; return rc;
} }
int test_assert_success_message_raw_errno_helper (int rc,
const char *msg,
const char *expr)
{
if (rc == -1) {
#if defined ZMQ_HAVE_WINDOWS
int current_errno = WSAGetLastError ();
#else
int current_errno = errno;
#endif
char buffer[512];
buffer[sizeof (buffer) - 1] =
0; // to ensure defined behavior with VC++ <= 2013
snprintf (buffer, sizeof (buffer) - 1, "%s failed%s%s%s, errno = %i",
expr, msg ? " (additional info: " : "", msg ? msg : "",
msg ? ")" : "", current_errno);
TEST_FAIL_MESSAGE (buffer);
}
return rc;
}
#define TEST_ASSERT_SUCCESS_MESSAGE_ERRNO(expr, msg) \ #define TEST_ASSERT_SUCCESS_MESSAGE_ERRNO(expr, msg) \
test_assert_success_message_errno_helper (expr, msg, #expr) test_assert_success_message_errno_helper (expr, msg, #expr)
#define TEST_ASSERT_SUCCESS_ERRNO(expr) \ #define TEST_ASSERT_SUCCESS_ERRNO(expr) \
test_assert_success_message_errno_helper (expr, NULL, #expr) test_assert_success_message_errno_helper (expr, NULL, #expr)
#define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr) \
test_assert_success_message_raw_errno_helper (expr, NULL, #expr)
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr) \ #define TEST_ASSERT_FAILURE_ERRNO(error_code, expr) \
{ \ { \
int rc = (expr); \ int rc = (expr); \
......
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