2013-07-05 15:20:42 +02:00
|
|
|
/*
|
2017-05-01 13:11:11 +02:00
|
|
|
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2013-07-05 15:20:42 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
2013-07-05 15:20:42 +02:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testutil.hpp"
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
const char *bind_address = 0;
|
2017-05-01 13:11:11 +02:00
|
|
|
char connect_address[MAX_SOCKET_STRING];
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void test_push_round_robin_out (void *ctx_)
|
2013-07-05 15:20:42 +02:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
void *push = zmq_socket (ctx_, ZMQ_PUSH);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (push);
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
int rc = zmq_bind (push, bind_address);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
2017-05-01 13:11:11 +02:00
|
|
|
size_t len = MAX_SOCKET_STRING;
|
|
|
|
rc = zmq_getsockopt (push, ZMQ_LAST_ENDPOINT, connect_address, &len);
|
|
|
|
assert (rc == 0);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
const size_t services = 5;
|
2018-02-01 11:46:09 +01:00
|
|
|
void *pulls[services];
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer) {
|
2018-05-27 13:01:36 +02:00
|
|
|
pulls[peer] = zmq_socket (ctx_, ZMQ_PULL);
|
2018-02-01 11:46:09 +01:00
|
|
|
assert (pulls[peer]);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2015-01-30 11:57:31 +01:00
|
|
|
int timeout = 250;
|
2018-02-01 11:46:09 +01:00
|
|
|
rc = zmq_setsockopt (pulls[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
rc = zmq_connect (pulls[peer], connect_address);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// Wait for connections.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2013-07-05 15:20:42 +02:00
|
|
|
// Send 2N messages
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer)
|
2013-07-05 15:20:42 +02:00
|
|
|
s_send_seq (push, "ABC", SEQ_END);
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer)
|
2013-07-05 15:20:42 +02:00
|
|
|
s_send_seq (push, "DEF", SEQ_END);
|
|
|
|
|
|
|
|
// Expect every PULL got one of each
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer) {
|
2018-02-01 11:46:09 +01:00
|
|
|
s_recv_seq (pulls[peer], "ABC", SEQ_END);
|
|
|
|
s_recv_seq (pulls[peer], "DEF", SEQ_END);
|
2013-07-05 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
close_zero_linger (push);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer)
|
2018-02-01 11:46:09 +01:00
|
|
|
close_zero_linger (pulls[peer]);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
|
|
|
// Wait for disconnects.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-05 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void test_pull_fair_queue_in (void *ctx_)
|
2013-07-05 15:20:42 +02:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
void *pull = zmq_socket (ctx_, ZMQ_PULL);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (pull);
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
int rc = zmq_bind (pull, bind_address);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
2017-05-01 13:11:11 +02:00
|
|
|
size_t len = MAX_SOCKET_STRING;
|
|
|
|
rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, connect_address, &len);
|
|
|
|
assert (rc == 0);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2018-05-14 20:53:19 +02:00
|
|
|
const unsigned char services = 5;
|
2018-02-01 11:46:09 +01:00
|
|
|
void *pushs[services];
|
2018-05-14 20:53:19 +02:00
|
|
|
for (unsigned char peer = 0; peer < services; ++peer) {
|
2018-05-27 13:01:36 +02:00
|
|
|
pushs[peer] = zmq_socket (ctx_, ZMQ_PUSH);
|
2018-02-01 11:46:09 +01:00
|
|
|
assert (pushs[peer]);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
rc = zmq_connect (pushs[peer], connect_address);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// Wait for connections.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
|
|
|
int first_half = 0;
|
|
|
|
int second_half = 0;
|
|
|
|
|
2013-07-05 15:20:42 +02:00
|
|
|
// Send 2N messages
|
2018-05-14 20:53:19 +02:00
|
|
|
for (unsigned char peer = 0; peer < services; ++peer) {
|
2018-02-01 11:46:09 +01:00
|
|
|
char *str = strdup ("A");
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
str[0] += peer;
|
|
|
|
s_send_seq (pushs[peer], str, SEQ_END);
|
|
|
|
first_half += str[0];
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
str[0] += services;
|
|
|
|
s_send_seq (pushs[peer], str, SEQ_END);
|
|
|
|
second_half += str[0];
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2013-07-05 15:20:42 +02:00
|
|
|
free (str);
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// Wait for data.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
|
|
|
zmq_msg_t msg;
|
|
|
|
rc = zmq_msg_init (&msg);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Expect to pull one from each first
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer) {
|
2013-07-05 17:58:01 +02:00
|
|
|
rc = zmq_msg_recv (&msg, pull, 0);
|
|
|
|
assert (rc == 2);
|
2018-02-01 11:46:09 +01:00
|
|
|
const char *str = (const char *) zmq_msg_data (&msg);
|
|
|
|
first_half -= str[0];
|
2013-07-05 15:20:42 +02:00
|
|
|
}
|
2013-07-05 17:58:01 +02:00
|
|
|
assert (first_half == 0);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// And then get the second batch
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer) {
|
2013-07-05 17:58:01 +02:00
|
|
|
rc = zmq_msg_recv (&msg, pull, 0);
|
|
|
|
assert (rc == 2);
|
2018-02-01 11:46:09 +01:00
|
|
|
const char *str = (const char *) zmq_msg_data (&msg);
|
|
|
|
second_half -= str[0];
|
2013-07-05 17:58:01 +02:00
|
|
|
}
|
|
|
|
assert (second_half == 0);
|
|
|
|
|
|
|
|
rc = zmq_msg_close (&msg);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
close_zero_linger (pull);
|
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer)
|
2018-02-01 11:46:09 +01:00
|
|
|
close_zero_linger (pushs[peer]);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
|
|
|
// Wait for disconnects.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-05 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void test_push_block_on_send_no_peers (void *ctx_)
|
2013-07-05 15:20:42 +02:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
void *sc = zmq_socket (ctx_, ZMQ_PUSH);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (sc);
|
|
|
|
|
2015-01-30 11:57:31 +01:00
|
|
|
int timeout = 250;
|
2013-07-14 08:32:35 +02:00
|
|
|
int rc = zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (timeout));
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
rc = zmq_send (sc, 0, 0, ZMQ_DONTWAIT);
|
|
|
|
assert (rc == -1);
|
|
|
|
assert (errno == EAGAIN);
|
|
|
|
|
|
|
|
rc = zmq_send (sc, 0, 0, 0);
|
|
|
|
assert (rc == -1);
|
|
|
|
assert (errno == EAGAIN);
|
|
|
|
|
|
|
|
rc = zmq_close (sc);
|
|
|
|
assert (rc == 0);
|
|
|
|
}
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void test_destroy_queue_on_disconnect (void *ctx_)
|
2013-07-05 15:20:42 +02:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
void *a = zmq_socket (ctx_, ZMQ_PUSH);
|
|
|
|
assert (a);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
|
|
|
int hwm = 1;
|
2018-05-27 13:01:36 +02:00
|
|
|
int rc = zmq_setsockopt (a, ZMQ_SNDHWM, &hwm, sizeof (hwm));
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_bind (a, bind_address);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
2017-05-01 13:11:11 +02:00
|
|
|
size_t len = MAX_SOCKET_STRING;
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_getsockopt (a, ZMQ_LAST_ENDPOINT, connect_address, &len);
|
2017-05-01 13:11:11 +02:00
|
|
|
assert (rc == 0);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void *b = zmq_socket (ctx_, ZMQ_PULL);
|
|
|
|
assert (b);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_setsockopt (b, ZMQ_RCVHWM, &hwm, sizeof (hwm));
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_connect (b, connect_address);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Send two messages, one should be stuck in A's outgoing queue, the other
|
|
|
|
// arrives at B.
|
2018-05-27 13:01:36 +02:00
|
|
|
s_send_seq (a, "ABC", SEQ_END);
|
|
|
|
s_send_seq (a, "DEF", SEQ_END);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
|
|
|
// Both queues should now be full, indicated by A blocking on send.
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (a, 0, 0, ZMQ_DONTWAIT);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == -1);
|
|
|
|
assert (errno == EAGAIN);
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_disconnect (b, connect_address);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Disconnect may take time and need command processing.
|
2018-05-27 13:01:36 +02:00
|
|
|
zmq_pollitem_t poller[2] = {{a, 0, 0, 0}, {b, 0, 0, 0}};
|
2013-07-05 15:20:42 +02:00
|
|
|
rc = zmq_poll (poller, 2, 100);
|
|
|
|
assert (rc == 0);
|
2013-07-05 17:58:01 +02:00
|
|
|
rc = zmq_poll (poller, 2, 100);
|
|
|
|
assert (rc == 0);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
|
|
|
zmq_msg_t msg;
|
|
|
|
rc = zmq_msg_init (&msg);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Can't receive old data on B.
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_msg_recv (&msg, b, ZMQ_DONTWAIT);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == -1);
|
|
|
|
assert (errno == EAGAIN);
|
|
|
|
|
|
|
|
// Sending fails.
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (a, 0, 0, ZMQ_DONTWAIT);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == -1);
|
|
|
|
assert (errno == EAGAIN);
|
|
|
|
|
|
|
|
// Reconnect B
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_connect (b, connect_address);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// Still can't receive old data on B.
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_msg_recv (&msg, b, ZMQ_DONTWAIT);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == -1);
|
|
|
|
assert (errno == EAGAIN);
|
|
|
|
|
|
|
|
// two messages should be sendable before the queues are filled up.
|
2018-05-27 13:01:36 +02:00
|
|
|
s_send_seq (a, "ABC", SEQ_END);
|
|
|
|
s_send_seq (a, "DEF", SEQ_END);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
rc = zmq_send (a, 0, 0, ZMQ_DONTWAIT);
|
2013-07-05 15:20:42 +02:00
|
|
|
assert (rc == -1);
|
|
|
|
assert (errno == EAGAIN);
|
|
|
|
|
|
|
|
rc = zmq_msg_close (&msg);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
close_zero_linger (a);
|
|
|
|
close_zero_linger (b);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// Wait for disconnects.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-05 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
int main (void)
|
2013-07-05 15:20:42 +02:00
|
|
|
{
|
2018-02-01 11:46:09 +01:00
|
|
|
setup_test_environment ();
|
2013-07-05 15:20:42 +02:00
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
assert (ctx);
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
const char *binds[] = {"inproc://a", "tcp://127.0.0.1:*"};
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
for (int transport = 0; transport < 2; ++transport) {
|
2018-02-01 11:46:09 +01:00
|
|
|
bind_address = binds[transport];
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// PUSH: SHALL route outgoing messages to connected peers using a
|
|
|
|
// round-robin strategy.
|
|
|
|
test_push_round_robin_out (ctx);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// PULL: SHALL receive incoming messages from its peers using a fair-queuing
|
|
|
|
// strategy.
|
|
|
|
test_pull_fair_queue_in (ctx);
|
2013-07-05 15:20:42 +02:00
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// PUSH: SHALL block on sending, or return a suitable error, when it has no
|
|
|
|
// available peers.
|
|
|
|
test_push_block_on_send_no_peers (ctx);
|
|
|
|
|
|
|
|
// PUSH and PULL: SHALL create this queue when a peer connects to it. If
|
|
|
|
// this peer disconnects, the socket SHALL destroy its queue and SHALL
|
|
|
|
// discard any messages it contains.
|
2013-07-07 12:49:24 +02:00
|
|
|
// *** Test disabled until libzmq does this properly ***
|
|
|
|
// test_destroy_queue_on_disconnect (ctx);
|
2013-07-05 17:58:01 +02:00
|
|
|
}
|
2013-07-05 15:20:42 +02:00
|
|
|
|
|
|
|
int rc = zmq_ctx_term (ctx);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
return 0;
|
2013-07-05 15:20:42 +02:00
|
|
|
}
|