Skip to content

Commit

Permalink
Problem: tests bind to hardcoded TCP ports
Browse files Browse the repository at this point in the history
Solution: use ZMQ_LAST_ENDPOINT in most places. This alllows running
tests in paralle, and on over-booked shared machines where many of
the ports would be already in use.
Keep 3 tests with an hardcoded port, as there are some code paths that
require it (eg: connect before bind), but list those ports in
tests/testutil.hpp as macros so that they do not overlap and still
allow parallel runs.

These changes were inspired by a patch uploaded to Ubuntu by the
package maintainer, Steve Langasek <[email protected]>.
Thank you Steve!
  • Loading branch information
bluca committed May 1, 2017
1 parent ae461dc commit 5934919
Show file tree
Hide file tree
Showing 51 changed files with 791 additions and 599 deletions.
4 changes: 2 additions & 2 deletions tests/test_bind_after_connect_tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main (void)
void *sc = zmq_socket (ctx, ZMQ_DEALER);
assert (sc);

int rc = zmq_connect (sc, "tcp://127.0.0.1:7722");
int rc = zmq_connect (sc, ENDPOINT_3);
assert (rc == 0);

rc = zmq_send_const (sc, "foobar", 6, 0);
Expand All @@ -53,7 +53,7 @@ int main (void)
rc = zmq_send_const (sc, "buzz", 4, 0);
assert (rc == 4);

rc = zmq_bind (sb, "tcp://127.0.0.1:7722");
rc = zmq_bind (sb, ENDPOINT_3);
assert (rc == 0);

zmq_msg_t msg;
Expand Down
10 changes: 7 additions & 3 deletions tests/test_conflate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
This file is part of libzmq, the ZeroMQ core engine in C++.
Expand Down Expand Up @@ -31,7 +31,9 @@

int main (int, char *[])
{
const char *bind_to = "tcp://127.0.0.1:5555";
const char *bind_to = "tcp://127.0.0.1:*";
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];

int rc;

Expand All @@ -47,11 +49,13 @@ int main (int, char *[])

rc = zmq_bind (s_in, bind_to);
assert (rc == 0);
rc = zmq_getsockopt (s_in, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);

void* s_out = zmq_socket (ctx, ZMQ_PUSH);
assert (s_out);

rc = zmq_connect (s_out, bind_to);
rc = zmq_connect (s_out, my_endpoint);
assert (rc == 0);

int message_count = 20;
Expand Down
22 changes: 15 additions & 7 deletions tests/test_connect_rid.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
This file is part of libzmq, the ZeroMQ core engine in C++.
Expand Down Expand Up @@ -35,9 +35,11 @@ void test_stream_2_stream(){
int ret;
char buff[256];
char msg[] = "hi 1";
const char *bindip = "tcp://127.0.0.1:5556";
const char *bindip = "tcp://127.0.0.1:*";
int disabled = 0;
int zero = 0;
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *ctx = zmq_ctx_new ();

// Set up listener STREAM.
Expand All @@ -49,6 +51,8 @@ void test_stream_2_stream(){
assert (0 == ret);
ret = zmq_bind (rbind, bindip);
assert(0 == ret);
ret = zmq_getsockopt (rbind, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (0 == ret);

// Set up connection stream.
rconn1 = zmq_socket (ctx, ZMQ_STREAM);
Expand All @@ -59,7 +63,7 @@ void test_stream_2_stream(){
// Do the connection.
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
assert (0 == ret);
ret = zmq_connect (rconn1, bindip);
ret = zmq_connect (rconn1, my_endpoint);

/* Uncomment to test assert on duplicate rid.
// Test duplicate connect attempt.
Expand All @@ -83,7 +87,7 @@ void test_stream_2_stream(){
assert ('h' == buff[128]);

// Handle close of the socket.
ret = zmq_unbind (rbind, bindip);
ret = zmq_unbind (rbind, my_endpoint);
assert(0 == ret);
ret = zmq_close (rbind);
assert(0 == ret);
Expand All @@ -98,8 +102,10 @@ void test_router_2_router(bool named){
int ret;
char buff[256];
char msg[] = "hi 1";
const char *bindip = "tcp://127.0.0.1:5556";
const char *bindip = "tcp://127.0.0.1:*";
int zero = 0;
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *ctx = zmq_ctx_new ();

// Create bind socket.
Expand All @@ -109,6 +115,8 @@ void test_router_2_router(bool named){
assert (0 == ret);
ret = zmq_bind (rbind, bindip);
assert (0 == ret);
ret = zmq_getsockopt (rbind, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (0 == ret);

// Create connection socket.
rconn1 = zmq_socket (ctx, ZMQ_ROUTER);
Expand All @@ -125,7 +133,7 @@ void test_router_2_router(bool named){
// Make call to connect using a connect_rid.
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
assert (0 == ret);
ret = zmq_connect (rconn1, bindip);
ret = zmq_connect (rconn1, my_endpoint);
assert (0 == ret);
/* Uncomment to test assert on duplicate rid
// Test duplicate connect attempt.
Expand Down Expand Up @@ -169,7 +177,7 @@ void test_router_2_router(bool named){
ret = zmq_recv (rconn1, buff+128, 128, 0);
assert (3 == ret && 'o' == buff[128]);

ret = zmq_unbind (rbind, bindip);
ret = zmq_unbind (rbind, my_endpoint);
assert(0 == ret);
ret = zmq_close (rbind);
assert(0 == ret);
Expand Down
12 changes: 6 additions & 6 deletions tests/test_dgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ int main (void)
void *listener = zmq_socket (ctx, ZMQ_DGRAM);

// Connecting dgram shoudl fail
int rc = zmq_connect (listener, "udp://127.0.0.1:5556");
int rc = zmq_connect (listener, ENDPOINT_4);
assert (rc == -1);

rc = zmq_bind (listener, "udp://*:5556");
rc = zmq_bind (listener, ENDPOINT_4);
assert (rc == 0);

rc = zmq_bind (sender, "udp://*:5557");
rc = zmq_bind (sender, ENDPOINT_5);
assert (rc == 0);

str_send_to (sender, "Is someone there ?", "127.0.0.1:5556");
str_send_to (sender, "Is someone there ?", strrchr (ENDPOINT_4, '/') + 1);

str_recv_from (listener, &message_string, &address);
assert (strcmp(message_string, "Is someone there ?") == 0);
assert (strcmp(address, "127.0.0.1:5557") == 0);
assert (strcmp(address, strrchr (ENDPOINT_5, '/') + 1) == 0);
free (message_string);

str_send_to (listener, "Yes, there is !", address);
free (address);

str_recv_from (sender, &message_string, &address);
assert (strcmp(message_string, "Yes, there is !") == 0);
assert (strcmp(address, "127.0.0.1:5556") == 0);
assert (strcmp(address, strrchr (ENDPOINT_4, '/') + 1) == 0);
free (message_string);
free (address);

Expand Down
11 changes: 8 additions & 3 deletions tests/test_diffserv.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
This file is part of libzmq, the ZeroMQ core engine in C++.
Expand Down Expand Up @@ -35,6 +35,8 @@ int main (void)
int tos = 0x28;
int o_tos;
size_t tos_size = sizeof(tos);
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];

setup_test_environment();
void *ctx = zmq_ctx_new ();
Expand All @@ -44,8 +46,11 @@ int main (void)
assert (sb);
rc = zmq_setsockopt (sb, ZMQ_TOS, &tos, tos_size);
assert (rc == 0);
rc = zmq_bind (sb, "tcp://127.0.0.1:5560");
rc = zmq_bind (sb, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);

rc = zmq_getsockopt (sb, ZMQ_TOS, &o_tos, &tos_size);
assert (rc == 0);
assert (o_tos == tos);
Expand All @@ -55,7 +60,7 @@ int main (void)
tos = 0x58;
rc = zmq_setsockopt (sc, ZMQ_TOS, &tos, tos_size);
assert (rc == 0);
rc = zmq_connect (sc, "tcp://127.0.0.1:5560");
rc = zmq_connect (sc, my_endpoint);
assert (rc == 0);
rc = zmq_getsockopt (sc, ZMQ_TOS, &o_tos, &tos_size);
assert (rc == 0);
Expand Down
10 changes: 7 additions & 3 deletions tests/test_fork.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
This file is part of libzmq, the ZeroMQ core engine in C++.
Expand Down Expand Up @@ -29,7 +29,8 @@

#include "testutil.hpp"

const char *address = "tcp://127.0.0.1:6571";
const char *address = "tcp://127.0.0.1:*";
char connect_address[MAX_SOCKET_STRING];

#define NUM_MESSAGES 5

Expand All @@ -45,6 +46,9 @@ int main (void)
assert (pull);
int rc = zmq_bind (pull, address);
assert (rc == 0);
size_t len = MAX_SOCKET_STRING;
rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, connect_address, &len);
assert (rc == 0);

int pid = fork ();
if (pid == 0) {
Expand All @@ -58,7 +62,7 @@ int main (void)
assert (child_ctx);
void *push = zmq_socket (child_ctx, ZMQ_PUSH);
assert (push);
rc = zmq_connect (push, address);
rc = zmq_connect (push, connect_address);
assert (rc == 0);
int count;
for (count = 0; count < NUM_MESSAGES; count++)
Expand Down
27 changes: 18 additions & 9 deletions tests/test_heartbeats.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
This file is part of 0MQ.
Expand Down Expand Up @@ -102,7 +102,8 @@ mock_handshake (raw_socket fd) {
}

static void
prep_server_socket(void * ctx, int set_heartbeats, void ** server_out, void ** mon_out)
prep_server_socket(void * ctx, int set_heartbeats, void ** server_out, void ** mon_out,
char *endpoint, size_t ep_length)
{
int rc;
// We'll be using this socket in raw mode
Expand All @@ -119,7 +120,9 @@ prep_server_socket(void * ctx, int set_heartbeats, void ** server_out, void ** m
assert (rc == 0);
}

rc = zmq_bind (server, "tcp://127.0.0.1:5556");
rc = zmq_bind (server, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, endpoint, &ep_length);
assert (rc == 0);

// Create and connect a socket for collecting monitor events on dealer
Expand All @@ -145,19 +148,21 @@ static void
test_heartbeat_timeout (void)
{
int rc;
char my_endpoint[MAX_SOCKET_STRING];

// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);

void * server, * server_mon;
prep_server_socket (ctx, 1, &server, &server_mon);
prep_server_socket (ctx, 1, &server, &server_mon, my_endpoint,
MAX_SOCKET_STRING);

struct sockaddr_in ip4addr;
raw_socket s;

ip4addr.sin_family = AF_INET;
ip4addr.sin_port = htons (5556);
ip4addr.sin_port = htons (atoi (strrchr (my_endpoint, ':') + 1));
#if defined (ZMQ_HAVE_WINDOWS) && (_WIN32_WINNT < 0x0600)
ip4addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
#else
Expand Down Expand Up @@ -200,13 +205,15 @@ static void
test_heartbeat_ttl (void)
{
int rc, value;
char my_endpoint[MAX_SOCKET_STRING];

// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);

void * server, * server_mon, *client;
prep_server_socket (ctx, 0, &server, &server_mon);
prep_server_socket (ctx, 0, &server, &server_mon, my_endpoint,
MAX_SOCKET_STRING);

client = zmq_socket (ctx, ZMQ_DEALER);
assert (client != NULL);
Expand All @@ -222,7 +229,7 @@ test_heartbeat_ttl (void)
rc = zmq_setsockopt (client, ZMQ_HEARTBEAT_IVL, &value, sizeof (value));
assert (rc == 0);

rc = zmq_connect (client, "tcp://localhost:5556");
rc = zmq_connect (client, my_endpoint);
assert (rc == 0);

// By now everything should report as connected
Expand Down Expand Up @@ -255,16 +262,18 @@ static void
test_heartbeat_notimeout (void)
{
int rc;
char my_endpoint[MAX_SOCKET_STRING];

// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);

void * server, * server_mon;
prep_server_socket(ctx, 1, &server, &server_mon);
prep_server_socket(ctx, 1, &server, &server_mon, my_endpoint,
MAX_SOCKET_STRING);

void * client = zmq_socket (ctx, ZMQ_DEALER);
rc = zmq_connect (client, "tcp://127.0.0.1:5556");
rc = zmq_connect (client, my_endpoint);

// Give it a sec to connect and handshake
msleep (SETTLE_TIME);
Expand Down
10 changes: 7 additions & 3 deletions tests/test_hwm_pubsub.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
This file is part of libzmq, the ZeroMQ core engine in C++.
Expand Down Expand Up @@ -157,6 +157,8 @@ void test_reset_hwm ()
int first_count = 9999;
int second_count = 1100;
int hwm = 11024;
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];

void *ctx = zmq_ctx_new ();
assert (ctx);
Expand All @@ -167,15 +169,17 @@ void test_reset_hwm ()
assert (pub_socket);
rc = zmq_setsockopt (pub_socket, ZMQ_SNDHWM, &hwm, sizeof (hwm));
assert (rc == 0);
rc = zmq_bind (pub_socket, "tcp://127.0.0.1:1234");
rc = zmq_bind (pub_socket, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (pub_socket, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);

// Set up connect socket
void *sub_socket = zmq_socket (ctx, ZMQ_SUB);
assert (sub_socket);
rc = zmq_setsockopt (sub_socket, ZMQ_RCVHWM, &hwm, sizeof (hwm));
assert (rc == 0);
rc = zmq_connect (sub_socket, "tcp://127.0.0.1:1234");
rc = zmq_connect (sub_socket, my_endpoint);
assert (rc == 0);
rc = zmq_setsockopt( sub_socket, ZMQ_SUBSCRIBE, 0, 0);
assert (rc == 0);
Expand Down
Loading

0 comments on commit 5934919

Please sign in to comment.