mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-13 10:52:56 +01:00
Problem: duplicated code in tests related to monitoring
Solution: extract new receive_monitor_address function
This commit is contained in:
parent
495fb00b7e
commit
21b8d5cff7
@ -28,6 +28,7 @@
|
||||
*/
|
||||
|
||||
#include "testutil.hpp"
|
||||
#include "testutil_monitoring.hpp"
|
||||
#include "testutil_unity.hpp"
|
||||
#if defined(ZMQ_HAVE_WINDOWS)
|
||||
#include <winsock2.h>
|
||||
@ -178,43 +179,6 @@ void tearDown ()
|
||||
zmq_threadclose (zap_thread);
|
||||
}
|
||||
|
||||
// Read one event off the monitor socket; return value and address
|
||||
// by reference, if not null, and event number by value. Returns -1
|
||||
// in case of error.
|
||||
static int get_monitor_event (void *monitor_, int *value_, char **address_)
|
||||
{
|
||||
// First frame in message contains event number and value
|
||||
zmq_msg_t msg;
|
||||
zmq_msg_init (&msg);
|
||||
if (zmq_msg_recv (&msg, monitor_, 0) == -1)
|
||||
return -1; // Interruped, presumably
|
||||
TEST_ASSERT_TRUE (zmq_msg_more (&msg));
|
||||
|
||||
uint8_t *data = static_cast<uint8_t *> (zmq_msg_data (&msg));
|
||||
uint16_t event = *reinterpret_cast<uint16_t *> (data);
|
||||
if (value_)
|
||||
*value_ = *reinterpret_cast<uint32_t *> (data + 2);
|
||||
zmq_msg_close (&msg);
|
||||
|
||||
// Second frame in message contains event address
|
||||
zmq_msg_init (&msg);
|
||||
if (zmq_msg_recv (&msg, monitor_, 0) == -1)
|
||||
return -1; // Interruped, presumably
|
||||
TEST_ASSERT_FALSE (zmq_msg_more (&msg));
|
||||
|
||||
if (address_) {
|
||||
const uint8_t *const data =
|
||||
static_cast<const uint8_t *> (zmq_msg_data (&msg));
|
||||
const size_t size = zmq_msg_size (&msg);
|
||||
*address_ = static_cast<char *> (malloc (size + 1));
|
||||
memcpy (*address_, data, size);
|
||||
*address_[size] = 0;
|
||||
}
|
||||
zmq_msg_close (&msg);
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
void test_valid_creds ()
|
||||
{
|
||||
void *client = test_context_socket (ZMQ_DEALER);
|
||||
|
@ -28,6 +28,7 @@
|
||||
*/
|
||||
|
||||
#include "testutil.hpp"
|
||||
#include "testutil_monitoring.hpp"
|
||||
#include "testutil_unity.hpp"
|
||||
|
||||
#include <stdlib.h>
|
||||
@ -35,41 +36,6 @@
|
||||
|
||||
SETUP_TEARDOWN_TESTCONTEXT
|
||||
|
||||
// Read one event off the monitor socket; return value and address
|
||||
// by reference, if not null, and event number by value. Returns -1
|
||||
// in case of error.
|
||||
|
||||
static int get_monitor_event (void *monitor_, int *value_, char **address_)
|
||||
{
|
||||
// First frame in message contains event number and value
|
||||
zmq_msg_t msg;
|
||||
zmq_msg_init (&msg);
|
||||
if (zmq_msg_recv (&msg, monitor_, 0) == -1)
|
||||
return -1; // Interruped, presumably
|
||||
TEST_ASSERT_TRUE (zmq_msg_more (&msg));
|
||||
|
||||
uint8_t *data = static_cast<uint8_t *> (zmq_msg_data (&msg));
|
||||
uint16_t event = *reinterpret_cast<uint16_t *> (data);
|
||||
if (value_)
|
||||
*value_ = *reinterpret_cast<uint32_t *> (data + 2);
|
||||
|
||||
// Second frame in message contains event address
|
||||
zmq_msg_init (&msg);
|
||||
if (zmq_msg_recv (&msg, monitor_, 0) == -1)
|
||||
return -1; // Interruped, presumably
|
||||
TEST_ASSERT_TRUE (!zmq_msg_more (&msg));
|
||||
|
||||
if (address_) {
|
||||
const uint8_t *const data =
|
||||
static_cast<const uint8_t *> (zmq_msg_data (&msg));
|
||||
const size_t size = zmq_msg_size (&msg);
|
||||
*address_ = static_cast<char *> (malloc (size + 1));
|
||||
memcpy (*address_, data, size);
|
||||
*address_[size] = 0;
|
||||
}
|
||||
return event;
|
||||
}
|
||||
|
||||
static void test_stream_handshake_timeout_accept ()
|
||||
{
|
||||
char my_endpoint[MAX_SOCKET_STRING];
|
||||
|
@ -32,10 +32,32 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int
|
||||
receive_monitor_address (void *monitor_, char **address_, bool expect_more_)
|
||||
{
|
||||
zmq_msg_t msg;
|
||||
|
||||
zmq_msg_init (&msg);
|
||||
if (zmq_msg_recv (&msg, monitor_, 0) == -1)
|
||||
return -1; // Interrupted, presumably
|
||||
TEST_ASSERT_EQUAL (expect_more_, zmq_msg_more (&msg));
|
||||
|
||||
if (address_) {
|
||||
const uint8_t *const data =
|
||||
static_cast<const uint8_t *> (zmq_msg_data (&msg));
|
||||
const size_t size = zmq_msg_size (&msg);
|
||||
*address_ = static_cast<char *> (malloc (size + 1));
|
||||
memcpy (*address_, data, size);
|
||||
(*address_)[size] = 0;
|
||||
}
|
||||
zmq_msg_close (&msg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read one event off the monitor socket; return value and address
|
||||
// by reference, if not null, and event number by value. Returns -1
|
||||
// in case of error.
|
||||
|
||||
static int get_monitor_event_internal (void *monitor_,
|
||||
int *value_,
|
||||
char **address_,
|
||||
@ -56,18 +78,9 @@ static int get_monitor_event_internal (void *monitor_,
|
||||
memcpy (value_, data + 2, sizeof (uint32_t));
|
||||
|
||||
// Second frame in message contains event address
|
||||
zmq_msg_init (&msg);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, monitor_, recv_flag_));
|
||||
TEST_ASSERT_FALSE (zmq_msg_more (&msg));
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
receive_monitor_address (monitor_, address_, false));
|
||||
|
||||
if (address_) {
|
||||
const uint8_t *const data =
|
||||
static_cast<const uint8_t *> (zmq_msg_data (&msg));
|
||||
const size_t size = zmq_msg_size (&msg);
|
||||
*address_ = static_cast<char *> (malloc (size + 1));
|
||||
memcpy (*address_, data, size);
|
||||
(*address_)[size] = 0;
|
||||
}
|
||||
return event;
|
||||
}
|
||||
|
||||
@ -242,34 +255,13 @@ static int64_t get_monitor_event_internal_v2 (void *monitor_,
|
||||
}
|
||||
|
||||
// Second-to-last frame in message contains local address
|
||||
zmq_msg_init (&msg);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, monitor_, recv_flag_));
|
||||
TEST_ASSERT_TRUE (zmq_msg_more (&msg));
|
||||
|
||||
if (local_address_) {
|
||||
const uint8_t *const data =
|
||||
static_cast<const uint8_t *> (zmq_msg_data (&msg));
|
||||
const size_t size = zmq_msg_size (&msg);
|
||||
*local_address_ = static_cast<char *> (malloc (size + 1));
|
||||
memcpy (*local_address_, data, size);
|
||||
(*local_address_)[size] = 0;
|
||||
}
|
||||
zmq_msg_close (&msg);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
receive_monitor_address (monitor_, local_address_, true));
|
||||
|
||||
// Last frame in message contains remote address
|
||||
zmq_msg_init (&msg);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, monitor_, recv_flag_));
|
||||
TEST_ASSERT_TRUE (!zmq_msg_more (&msg));
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
receive_monitor_address (monitor_, remote_address_, false));
|
||||
|
||||
if (remote_address_) {
|
||||
const uint8_t *data =
|
||||
static_cast<const uint8_t *> (zmq_msg_data (&msg));
|
||||
const size_t size = zmq_msg_size (&msg);
|
||||
*remote_address_ = static_cast<char *> (malloc (size + 1));
|
||||
memcpy (*remote_address_, data, size);
|
||||
(*remote_address_)[size] = 0;
|
||||
}
|
||||
zmq_msg_close (&msg);
|
||||
return event;
|
||||
}
|
||||
|
||||
|
@ -42,6 +42,9 @@ int get_monitor_event_with_timeout (void *monitor_,
|
||||
char **address_,
|
||||
int timeout_);
|
||||
|
||||
// Read one event off the monitor socket; return value and address
|
||||
// by reference, if not null, and event number by value. Returns -1
|
||||
// in case of error.
|
||||
int get_monitor_event (void *monitor_, int *value_, char **address_);
|
||||
|
||||
void expect_monitor_event (void *monitor_, int expected_event_);
|
||||
@ -72,4 +75,5 @@ void expect_monitor_event_v2 (void *monitor_,
|
||||
int64_t expected_event_,
|
||||
const char *expected_local_address_ = NULL,
|
||||
const char *expected_remote_address_ = NULL);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user