2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2013-03-12 17:04:51 +01:00
|
|
|
|
2013-08-17 14:43:45 +02:00
|
|
|
#include "testutil.hpp"
|
2019-03-22 15:01:04 +01:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-23 13:04:57 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-24 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2012-12-04 15:14:21 +01:00
|
|
|
|
|
|
|
/// Initialize a zeromq message with a given null-terminated string
|
|
|
|
#define ZMQ_PREPARE_STRING(msg, data, size) \
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg)); \
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, size + 1)); \
|
2012-12-04 15:14:21 +01:00
|
|
|
memcpy (zmq_msg_data (&msg), data, size + 1);
|
|
|
|
|
2014-12-23 15:21:22 +01:00
|
|
|
static int publicationsReceived = 0;
|
|
|
|
static bool isSubscribed = false;
|
2012-12-04 15:14:21 +01:00
|
|
|
|
2019-03-22 15:01:04 +01:00
|
|
|
void test_disconnect_inproc ()
|
2014-01-18 22:08:06 +01:00
|
|
|
{
|
2019-03-22 15:01:04 +01:00
|
|
|
void *pub_socket = test_context_socket (ZMQ_XPUB);
|
|
|
|
void *sub_socket = test_context_socket (ZMQ_SUB);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, "foo", 3));
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_bind (pub_socket, "inproc://someInProcDescriptor"));
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2013-06-22 13:00:33 +02:00
|
|
|
int more;
|
2012-12-04 15:14:21 +01:00
|
|
|
size_t more_size = sizeof (more);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2019-03-22 15:01:04 +01:00
|
|
|
for (int iteration = 0;; ++iteration) {
|
2012-12-04 15:14:21 +01:00
|
|
|
zmq_pollitem_t items[] = {
|
2018-05-27 13:01:36 +02:00
|
|
|
{sub_socket, 0, ZMQ_POLLIN, 0}, // read publications
|
|
|
|
{pub_socket, 0, ZMQ_POLLIN, 0}, // read subscriptions
|
2012-12-04 15:14:21 +01:00
|
|
|
};
|
2013-01-31 19:46:22 +01:00
|
|
|
int rc = zmq_poll (items, 2, 100);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
if (items[1].revents & ZMQ_POLLIN) {
|
2019-03-22 15:01:04 +01:00
|
|
|
for (more = 1; more;) {
|
2012-12-04 15:14:21 +01:00
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq_msg_init (&msg);
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, pub_socket, 0));
|
2020-02-04 12:37:31 +01:00
|
|
|
const char *const buffer =
|
|
|
|
static_cast<const char *> (zmq_msg_data (&msg));
|
2012-12-04 15:14:21 +01:00
|
|
|
|
|
|
|
if (buffer[0] == 0) {
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_TRUE (isSubscribed);
|
2012-12-04 15:14:21 +01:00
|
|
|
isSubscribed = false;
|
2013-01-30 23:53:23 +01:00
|
|
|
} else {
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_FALSE (isSubscribed);
|
2012-12-04 15:14:21 +01:00
|
|
|
isSubscribed = true;
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (pub_socket, ZMQ_RCVMORE, &more, &more_size));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
2012-12-04 15:14:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (items[0].revents & ZMQ_POLLIN) {
|
2019-03-22 15:01:04 +01:00
|
|
|
more = 1;
|
|
|
|
for (more = 1; more;) {
|
2012-12-04 15:14:21 +01:00
|
|
|
zmq_msg_t msg;
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sub_socket, 0));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (sub_socket, ZMQ_RCVMORE, &more, &more_size));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
2012-12-04 15:14:21 +01:00
|
|
|
}
|
2019-03-22 15:01:04 +01:00
|
|
|
publicationsReceived++;
|
2012-12-04 15:14:21 +01:00
|
|
|
}
|
|
|
|
if (iteration == 1) {
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_connect (sub_socket, "inproc://someInProcDescriptor"));
|
2015-02-06 09:36:47 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2012-12-04 15:14:21 +01:00
|
|
|
}
|
|
|
|
if (iteration == 4) {
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_disconnect (sub_socket, "inproc://someInProcDescriptor"));
|
2012-12-04 15:14:21 +01:00
|
|
|
}
|
2013-01-31 19:46:22 +01:00
|
|
|
if (iteration > 4 && rc == 0)
|
2012-12-04 15:14:21 +01:00
|
|
|
break;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
zmq_msg_t channel_envlp;
|
|
|
|
ZMQ_PREPARE_STRING (channel_envlp, "foo", 3);
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_msg_send (&channel_envlp, pub_socket, ZMQ_SNDMORE));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&channel_envlp));
|
2012-12-04 15:14:21 +01:00
|
|
|
|
|
|
|
zmq_msg_t message;
|
|
|
|
ZMQ_PREPARE_STRING (message, "this is foo!", 12);
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&message, pub_socket, 0));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&message));
|
2012-12-04 15:14:21 +01:00
|
|
|
}
|
2019-03-22 15:01:04 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (3, publicationsReceived);
|
|
|
|
TEST_ASSERT_FALSE (isSubscribed);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2019-03-22 15:01:04 +01:00
|
|
|
test_context_socket_close (pub_socket);
|
|
|
|
test_context_socket_close (sub_socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int, char **)
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
2012-12-04 15:14:21 +01:00
|
|
|
|
2019-03-22 15:01:04 +01:00
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_disconnect_inproc);
|
|
|
|
return UNITY_END ();
|
2013-01-11 21:51:08 +01:00
|
|
|
}
|