0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-27 15:41:05 +08:00

Problem: no assertions for local and remote addresses in monitor tests

Solution: added such assertions and extended test utilities
This commit is contained in:
Simon Giesecke 2019-02-02 16:34:19 +01:00
parent f884fa725c
commit 8f90f579f5
2 changed files with 55 additions and 14 deletions

View File

@ -117,7 +117,7 @@ void test_monitor_basic ()
#ifdef ZMQ_BUILD_DRAFT_API
void test_monitor_versioned_basic ()
{
char my_endpoint[MAX_SOCKET_STRING];
char server_endpoint[MAX_SOCKET_STRING];
// We'll monitor these two sockets
void *client = test_context_socket (ZMQ_DEALER);
@ -140,9 +140,9 @@ void test_monitor_versioned_basic ()
zmq_connect (server_mon, "inproc://monitor-server"));
// Now do a basic ping test
bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);
bind_loopback_ipv4 (server, server_endpoint, sizeof server_endpoint);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, server_endpoint));
bounce (server, client);
// Close client and server
@ -150,18 +150,36 @@ void test_monitor_versioned_basic ()
test_context_socket_close_zero_linger (client);
test_context_socket_close_zero_linger (server);
char *client_local_address = NULL;
char *client_remote_address = NULL;
// Now collect and check events from both sockets
int64_t event = get_monitor_event_v2 (client_mon, NULL, NULL, NULL);
if (event == ZMQ_EVENT_CONNECT_DELAYED)
event = get_monitor_event_v2 (client_mon, NULL, NULL, NULL);
assert (event == ZMQ_EVENT_CONNECTED);
expect_monitor_event_v2 (client_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
int64_t event = get_monitor_event_v2 (
client_mon, NULL, &client_local_address, &client_remote_address);
if (event == ZMQ_EVENT_CONNECT_DELAYED) {
free (client_local_address);
free (client_remote_address);
event = get_monitor_event_v2 (client_mon, NULL, &client_local_address,
&client_remote_address);
}
TEST_ASSERT_EQUAL (ZMQ_EVENT_CONNECTED, event);
TEST_ASSERT_EQUAL_STRING (server_endpoint, client_remote_address);
static const char prefix[] = "tcp://127.0.0.1:";
TEST_ASSERT_EQUAL_STRING_LEN (prefix, client_local_address,
strlen (prefix));
TEST_ASSERT_NOT_EQUAL (
0, strcmp (client_local_address, client_remote_address));
expect_monitor_event_v2 (client_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED,
client_local_address, client_remote_address);
expect_monitor_event_v2 (client_mon, ZMQ_EVENT_MONITOR_STOPPED);
// This is the flow of server events
expect_monitor_event_v2 (server_mon, ZMQ_EVENT_LISTENING);
expect_monitor_event_v2 (server_mon, ZMQ_EVENT_ACCEPTED);
expect_monitor_event_v2 (server_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
expect_monitor_event_v2 (server_mon, ZMQ_EVENT_ACCEPTED,
client_remote_address, client_local_address);
expect_monitor_event_v2 (server_mon, ZMQ_EVENT_HANDSHAKE_SUCCEEDED,
client_remote_address, client_local_address);
event = get_monitor_event_v2 (server_mon, NULL, NULL, NULL);
// Sometimes the server sees the client closing before it gets closed.
if (event != ZMQ_EVENT_DISCONNECTED) {
@ -171,6 +189,8 @@ void test_monitor_versioned_basic ()
if (event != ZMQ_EVENT_DISCONNECTED) {
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_MONITOR_STOPPED, event);
}
free (client_local_address);
free (client_remote_address);
// Close down the sockets
// TODO why does this use zero_linger?

View File

@ -297,16 +297,37 @@ int64_t get_monitor_event_v2 (void *monitor_,
remote_address_, -1);
}
void expect_monitor_event_v2 (void *monitor_, int64_t expected_event_)
void expect_monitor_event_v2 (void *monitor_,
int64_t expected_event_,
const char *expected_local_address_ = NULL,
const char *expected_remote_address_ = NULL)
{
int64_t event = get_monitor_event_v2 (monitor_, NULL, NULL, NULL);
char *local_address = NULL;
char *remote_address = NULL;
int64_t event = get_monitor_event_v2 (
monitor_, NULL, expected_local_address_ ? &local_address : NULL,
expected_remote_address_ ? &remote_address : NULL);
bool failed = false;
if (event != expected_event_) {
fprintf (stderr,
"Expected monitor event %" PRIx64 " but received %" PRIx64
"Expected monitor event %" PRIx64 ", but received %" PRIx64
"\n",
expected_event_, event);
assert (event == expected_event_);
failed = true;
}
if (expected_local_address_
&& 0 != strcmp (local_address, expected_local_address_)) {
fprintf (stderr, "Expected local address %s, but received %s\n",
expected_local_address_, local_address);
}
if (expected_remote_address_
&& 0 != strcmp (remote_address, expected_remote_address_)) {
fprintf (stderr, "Expected remote address %s, but received %s\n",
expected_remote_address_, remote_address);
}
free (local_address);
free (remote_address);
assert (!failed);
}
#endif