2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
#include "testutil.hpp"
|
2018-05-22 10:43:27 +02:00
|
|
|
#include "testutil_unity.hpp"
|
2015-01-26 15:59:19 +01:00
|
|
|
|
2019-03-23 08:04:57 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-24 12:51:28 -04:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2018-05-22 10:43:27 +02:00
|
|
|
|
|
|
|
void test ()
|
|
|
|
{
|
2015-01-26 15:59:19 +01:00
|
|
|
// Create a publisher
|
2018-05-22 10:43:27 +02:00
|
|
|
void *pub = test_context_socket (ZMQ_PUB);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pub, "inproc://soname"));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// Create two subscribers
|
2018-05-22 10:43:27 +02:00
|
|
|
void *sub1 = test_context_socket (ZMQ_SUB);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub1, "inproc://soname"));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
2018-05-22 10:43:27 +02:00
|
|
|
void *sub2 = test_context_socket (ZMQ_SUB);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub2, "inproc://soname"));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// Subscribe pub1 to one prefix
|
|
|
|
// and pub2 to another prefix.
|
2018-05-27 07:01:36 -04:00
|
|
|
const char prefi_x1[] = "prefix1";
|
|
|
|
const char prefi_x2[] = "p2";
|
2015-01-26 15:59:19 +01:00
|
|
|
|
2018-05-22 10:43:27 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_setsockopt (sub1, ZMQ_SUBSCRIBE, prefi_x1, strlen (prefi_x1)));
|
2018-05-22 10:43:27 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_setsockopt (sub2, ZMQ_SUBSCRIBE, prefi_x2, strlen (prefi_x2)));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// Send a message with the first prefix
|
2018-05-27 07:01:36 -04:00
|
|
|
send_string_expect_success (pub, prefi_x1, 0);
|
2018-05-22 10:45:05 +02:00
|
|
|
msleep (SETTLE_TIME);
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// sub1 should receive it, but not sub2
|
2018-05-27 07:01:36 -04:00
|
|
|
recv_string_expect_success (sub1, prefi_x1, ZMQ_DONTWAIT);
|
2015-01-26 15:59:19 +01:00
|
|
|
|
2018-05-22 10:43:27 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// Send a message with the second prefix
|
2018-05-27 07:01:36 -04:00
|
|
|
send_string_expect_success (pub, prefi_x2, 0);
|
2018-05-22 10:45:05 +02:00
|
|
|
msleep (SETTLE_TIME);
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// sub2 should receive it, but not sub1
|
2018-05-27 07:01:36 -04:00
|
|
|
recv_string_expect_success (sub2, prefi_x2, ZMQ_DONTWAIT);
|
2015-01-26 15:59:19 +01:00
|
|
|
|
2018-05-22 10:43:27 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// Now invert the matching
|
|
|
|
int invert = 1;
|
2018-05-22 10:43:27 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (pub, ZMQ_INVERT_MATCHING, &invert, sizeof (invert)));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// ... on both sides, otherwise the SUB socket will filter the messages out
|
2018-05-22 10:43:27 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sub1, ZMQ_INVERT_MATCHING, &invert, sizeof (invert)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sub2, ZMQ_INVERT_MATCHING, &invert, sizeof (invert)));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// Send a message with the first prefix
|
2018-05-27 07:01:36 -04:00
|
|
|
send_string_expect_success (pub, prefi_x1, 0);
|
2018-05-22 10:45:05 +02:00
|
|
|
msleep (SETTLE_TIME);
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// sub2 should receive it, but not sub1
|
2018-05-27 07:01:36 -04:00
|
|
|
recv_string_expect_success (sub2, prefi_x1, ZMQ_DONTWAIT);
|
2015-01-26 15:59:19 +01:00
|
|
|
|
2018-05-22 10:43:27 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// Send a message with the second prefix
|
2018-05-27 07:01:36 -04:00
|
|
|
send_string_expect_success (pub, prefi_x2, 0);
|
2018-05-22 10:45:05 +02:00
|
|
|
msleep (SETTLE_TIME);
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// sub1 should receive it, but not sub2
|
2018-05-27 07:01:36 -04:00
|
|
|
recv_string_expect_success (sub1, prefi_x2, ZMQ_DONTWAIT);
|
2015-01-26 15:59:19 +01:00
|
|
|
|
2018-05-22 10:43:27 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT));
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// Clean up.
|
2018-05-22 10:43:27 +02:00
|
|
|
test_context_socket_close (pub);
|
|
|
|
test_context_socket_close (sub1);
|
|
|
|
test_context_socket_close (sub2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
|
|
|
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test);
|
|
|
|
return UNITY_END ();
|
2015-01-26 15:59:19 +01:00
|
|
|
}
|