Skip to content

Commit

Permalink
Fixed test_many_sockets
Browse files Browse the repository at this point in the history
* failed on systems where process could not allocate 1024 sockets by default
* include path for platform.hpp was wrong; fixed it
  • Loading branch information
hintjens committed Nov 24, 2013
1 parent ba7c065 commit c022c04
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 43 deletions.
73 changes: 31 additions & 42 deletions tests/test_many_sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,76 +23,65 @@
#include <stdlib.h>
#include <vector>


void test_system_max ()
{
// Keep allocating sockets until we run out of system resources

const int no_of_sockets = 2 * 65536;
void *ctx = zmq_ctx_new();
zmq_ctx_set(ctx, ZMQ_MAX_SOCKETS, no_of_sockets);
std::vector<void*> sockets;
void *ctx = zmq_ctx_new ();
zmq_ctx_set (ctx, ZMQ_MAX_SOCKETS, no_of_sockets);
std::vector <void*> sockets;

while (true)
{
void *socket = zmq_socket(ctx, ZMQ_PAIR);
while (true) {
void *socket = zmq_socket (ctx, ZMQ_PAIR);
if (!socket)
break;

sockets.push_back(socket);
sockets.push_back (socket);
}
assert ((int) sockets.size () < no_of_sockets);

assert((int)sockets.size() < no_of_sockets);

// System is out of resources, further calls to zmq_socket should return NULL.
for (unsigned int i = 0; i < 10; ++i)
{
void *socket = zmq_socket(ctx, ZMQ_PAIR);
assert(socket == NULL);
// System is out of resources, further calls to zmq_socket should return NULL
for (unsigned int i = 0; i < 10; ++i) {
void *socket = zmq_socket (ctx, ZMQ_PAIR);
assert (socket == NULL);
}

// Clean up.
for (unsigned int i = 0; i < sockets.size(); ++i)
zmq_close(sockets[i]);
for (unsigned int i = 0; i < sockets.size (); ++i)
zmq_close (sockets [i]);

zmq_ctx_destroy(ctx);
zmq_ctx_destroy (ctx);
}

void test_zmq_default_max ()
{
// Keep allocating sockets until we hit the default zeromq limit

void *ctx = zmq_ctx_new();
// Keep allocating sockets until we hit the default limit
void *ctx = zmq_ctx_new ();
std::vector<void*> sockets;

while (true)
{
void *socket = zmq_socket(ctx, ZMQ_PAIR);
while (true) {
void *socket = zmq_socket (ctx, ZMQ_PAIR);
if (!socket)
break;

sockets.push_back(socket);
sockets.push_back (socket);
}
// We may stop sooner if system has fewer available sockets
assert (sockets.size () <= ZMQ_MAX_SOCKETS_DFLT);

assert(sockets.size() == ZMQ_MAX_SOCKETS_DFLT);

// At zeromq max, further calls to zmq_socket should return NULL.
for (unsigned int i = 0; i < 10; ++i)
{
void *socket = zmq_socket(ctx, ZMQ_PAIR);
assert(socket == NULL);
// Further calls to zmq_socket should return NULL
for (unsigned int i = 0; i < 10; ++i) {
void *socket = zmq_socket (ctx, ZMQ_PAIR);
assert (socket == NULL);
}

// Clean up.
for (unsigned int i = 0; i < sockets.size(); ++i)
zmq_close(sockets[i]);
// Clean up
for (unsigned int i = 0; i < sockets.size (); ++i)
zmq_close (sockets [i]);

zmq_ctx_destroy(ctx);
zmq_ctx_destroy (ctx);
}

int main(void)
int main (void)
{
setup_test_environment();
setup_test_environment ();

test_system_max ();
test_zmq_default_max ();
Expand Down
2 changes: 1 addition & 1 deletion tests/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "../include/zmq.h"
#include "../include/zmq_utils.h"
#include "platform.hpp"
#include "../src/platform.hpp"

// This defines the settle time used in tests; raise this if we
// get test failures on slower systems due to binds/connects not
Expand Down

0 comments on commit c022c04

Please sign in to comment.