Skip to content

Commit

Permalink
shifted tests to use last_endpoint to avoid port collisions (zeromq#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamg authored and bluca committed Nov 3, 2017
1 parent f8ff127 commit 818f9c3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
34 changes: 20 additions & 14 deletions src/tests/test_auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ BOOST_AUTO_TEST_CASE(grasslands)

// create and bind a server socket
zmqpp::socket server(context, zmqpp::socket_type::push);
server.bind("tcp://*:9000");
server.bind("tcp://*:0");
const std::string endpoint = server.get<std::string>(zmqpp::socket_option::last_endpoint);

// create and connect a client socket
zmqpp::socket client(context, zmqpp::socket_type::pull);
client.connect("tcp://127.0.0.1:9000");
client.connect(endpoint);

// Send a single message from server to client
zmqpp::message request;
Expand Down Expand Up @@ -74,11 +75,12 @@ BOOST_AUTO_TEST_CASE(strawhouse)
// create and bind a server socket
zmqpp::socket server(context, zmqpp::socket_type::push);
//server.set(zmqpp::socket_option::zap_domain, "global");
server.bind("tcp://*:9001");
server.bind("tcp://*:0");
const std::string endpoint = server.get<std::string>(zmqpp::socket_option::last_endpoint);

// create and connect a client socket
zmqpp::socket client(context, zmqpp::socket_type::pull);
client.connect("tcp://127.0.0.1:9001");
client.connect(endpoint);

// Send a single message from server to client
zmqpp::message request;
Expand Down Expand Up @@ -115,13 +117,14 @@ BOOST_AUTO_TEST_CASE(woodhouse)
// create and bind a server socket
zmqpp::socket server(context, zmqpp::socket_type::pull);
server.set(zmqpp::socket_option::plain_server, 1);
server.bind("tcp://*:9002");
server.bind("tcp://*:0");
const std::string endpoint = server.get<std::string>(zmqpp::socket_option::last_endpoint);

// create and connect a client socket
zmqpp::socket client(context, zmqpp::socket_type::push);
client.set(zmqpp::socket_option::plain_username, "admin");
client.set(zmqpp::socket_option::plain_password, "password");
client.connect("tcp://127.0.0.1:9002");
client.connect(endpoint);

// Send a single message from client to server
zmqpp::message request;
Expand Down Expand Up @@ -182,14 +185,15 @@ BOOST_AUTO_TEST_CASE(stonehouse)
int as_server = 1;
server.set(zmqpp::socket_option::curve_server, as_server);
server.set(zmqpp::socket_option::curve_secret_key, server_keypair.secret_key);
server.bind("tcp://*:9003");
server.bind("tcp://*:0");
const std::string endpoint = server.get<std::string>(zmqpp::socket_option::last_endpoint);

// create and connect a client socket
zmqpp::socket client(context, zmqpp::socket_type::push);
client.set(zmqpp::socket_option::curve_server_key, server_keypair.public_key);
client.set(zmqpp::socket_option::curve_public_key, client_keypair.public_key);
client.set(zmqpp::socket_option::curve_secret_key, client_keypair.secret_key);
client.connect("tcp://127.0.0.1:9003");
client.connect(endpoint);

// Send a single message from client to server
zmqpp::message request;
Expand All @@ -208,11 +212,12 @@ BOOST_AUTO_TEST_CASE(stonehouse)
BOOST_AUTO_TEST_CASE(custom_metadata)
{
zmqpp::context context;
zmqpp::socket client(context, zmqpp::socket_type::req);
client.connect("tcp://127.0.0.1:9004");

zmqpp::socket server(context, zmqpp::socket_type::rep);
server.bind("tcp://*:9004");
server.bind("tcp://*:0");
const std::string endpoint = server.get<std::string>(zmqpp::socket_option::last_endpoint);

zmqpp::socket client(context, zmqpp::socket_type::req);
client.connect(endpoint);

zmqpp::message request;
request << "1.0" << "0001" << "test" << "192.168.55.1" << "BOB" << "PLAIN" << "admin" << "secret";
Expand Down Expand Up @@ -308,14 +313,15 @@ BOOST_AUTO_TEST_CASE(ironhouse)
int as_server = 1;
server.set(zmqpp::socket_option::curve_server, as_server);
server.set(zmqpp::socket_option::curve_secret_key, server_keypair.secret_key);
server.bind("tcp://*:9005");
server.bind("tcp://*:0");
const std::string endpoint = server.get<std::string>(zmqpp::socket_option::last_endpoint);

// create and connect a client socket
zmqpp::socket client(context, zmqpp::socket_type::pull);
client.set(zmqpp::socket_option::curve_server_key, server_keypair.public_key);
client.set(zmqpp::socket_option::curve_public_key, client_keypair.public_key);
client.set(zmqpp::socket_option::curve_secret_key, client_keypair.secret_key);
client.connect("tcp://127.0.0.1:9005");
client.connect(endpoint);

// Send a single message from server to client
zmqpp::message request;
Expand Down
21 changes: 13 additions & 8 deletions src/tests/test_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ BOOST_AUTO_TEST_CASE( push_messages_baseline )
boost::timer t;

void* context = zmq_init(1);
void* pusher = zmq_socket(context, ZMQ_PUSH);
BOOST_REQUIRE_MESSAGE(0 == zmq_connect(pusher, "tcp://localhost:5555"), "connect: " << zmq_strerror(zmq_errno()));

void* puller = zmq_socket(context, ZMQ_PULL);
BOOST_REQUIRE_MESSAGE(0 == zmq_bind(puller, "tcp://*:5555"), "bind: " << zmq_strerror(zmq_errno()));
BOOST_REQUIRE_MESSAGE(0 == zmq_bind(puller, "tcp://*:0"), "bind: " << zmq_strerror(zmq_errno()));

std::array<char, 512> endpoint;
size_t length = endpoint.size();
BOOST_REQUIRE_MESSAGE(0 == zmq_getsockopt(puller, ZMQ_LAST_ENDPOINT, endpoint.data(), &length), "getsockopt: " << zmq_strerror(zmq_errno()));

void* pusher = zmq_socket(context, ZMQ_PUSH);
BOOST_REQUIRE_MESSAGE(0 == zmq_connect(pusher, endpoint.data()), "connect: " << zmq_strerror(zmq_errno()));

auto pusher_func = [&pusher](void) {
auto remaining = messages;
Expand Down Expand Up @@ -107,11 +111,12 @@ BOOST_AUTO_TEST_CASE( push_messages )
boost::timer t;

zmqpp::context context;
zmqpp::socket pusher(context, zmqpp::socket_type::push);
pusher.connect("tcp://localhost:55555");

zmqpp::socket puller(context, zmqpp::socket_type::pull);
puller.bind("tcp://*:55555");
puller.bind("tcp://*:0");
const std::string endpoint = puller.get<std::string>(zmqpp::socket_option::last_endpoint);

zmqpp::socket pusher(context, zmqpp::socket_type::push);
pusher.connect(endpoint);

auto pusher_func = [&pusher](void) {
auto remaining = messages;
Expand Down
11 changes: 6 additions & 5 deletions src/tests/test_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,15 +550,16 @@ BOOST_AUTO_TEST_CASE( test_simple_monitor )
{
zmqpp::context ctx;
zmqpp::socket server(ctx, zmqpp::socket_type::push);
server.bind("tcp://*:55443");
server.bind("tcp://*:0");
const std::string endpoint = server.get<std::string>(zmqpp::socket_option::last_endpoint);

server.monitor("inproc://test_monitor", zmqpp::event::all);

zmqpp::socket monitor(ctx, zmqpp::socket_type::pair);
monitor.connect("inproc://test_monitor");

zmqpp::socket client(ctx, zmqpp::socket_type::pull);
client.connect("tcp://localhost:55443");
client.connect(endpoint);

// Receive event accepted
{
Expand All @@ -571,20 +572,20 @@ BOOST_AUTO_TEST_CASE( test_simple_monitor )
uint16_t ev = *(reinterpret_cast<const uint16_t *>(ptr));
// uint32_t value = *(reinterpret_cast<const uint32_t *>(ptr + 2));
BOOST_CHECK_EQUAL( zmqpp::event::accepted, ev );
BOOST_CHECK_EQUAL("tcp://0.0.0.0:55443", message.get(1));
BOOST_CHECK_EQUAL("tcp://0.0.0.0:0", message.get(1));
// value is the underlying file descriptor. we cannot check its value against anything meaningful
#else
zmq_event_t const* event = static_cast<zmq_event_t const*>( message.raw_data(0) );
BOOST_CHECK_EQUAL( zmqpp::event::accepted, event->event );
BOOST_CHECK_EQUAL( 0, event->value );
BOOST_CHECK_EQUAL("tcp://0.0.0.0:55443", message.get(1));
BOOST_CHECK_EQUAL("tcp://0.0.0.0:0", message.get(1));
#endif
}

server.unmonitor();

zmqpp::socket client2(ctx, zmqpp::socket_type::pull);
client2.connect("tcp://localhost:55443");
client2.connect(endpoint);

// Receive event monitor_stopped
{
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_socket_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ BOOST_AUTO_TEST_CASE( set_socket_options_tcp_only )
{
zmqpp::context context;
zmqpp::socket socket(context, zmqpp::socket_type::subscribe);
socket.bind("tcp://*:54321");
socket.bind("tcp://*:0");

// TODO: reenable once I have curve key generation working to test against
// CHECK_SET(socket, std::string, curve_public_key);
Expand Down Expand Up @@ -358,7 +358,7 @@ BOOST_AUTO_TEST_CASE( get_socket_options_tcp_only )
{
zmqpp::context context;
zmqpp::socket socket(context, zmqpp::socket_type::subscribe);
socket.bind("tcp://*:54322");
socket.bind("tcp://*:0");

// TODO: reenable once I have curve key generation working to test against
// CHECK_GET(socket, std::string, curve_public_key);
Expand Down

0 comments on commit 818f9c3

Please sign in to comment.