2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2013-11-07 17:47:55 +01:00
|
|
|
|
|
|
|
#include "testutil.hpp"
|
2019-03-22 15:17:10 +01:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2013-11-07 17:47:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-11-11 14:41:42 +01:00
|
|
|
#include <vector>
|
2013-11-07 17:47:55 +01:00
|
|
|
|
2019-03-24 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2019-03-22 15:17:10 +01:00
|
|
|
|
2013-11-11 14:41:42 +01:00
|
|
|
void test_system_max ()
|
2013-11-07 17:47:55 +01:00
|
|
|
{
|
2013-11-11 14:41:42 +01:00
|
|
|
// Keep allocating sockets until we run out of system resources
|
2013-11-11 14:54:58 +01:00
|
|
|
const int no_of_sockets = 2 * 65536;
|
2019-03-22 15:17:10 +01:00
|
|
|
zmq_ctx_set (get_test_context (), ZMQ_MAX_SOCKETS, no_of_sockets);
|
2013-11-19 16:51:06 +01:00
|
|
|
std::vector<void *> sockets;
|
2013-11-11 14:41:42 +01:00
|
|
|
|
2013-11-19 16:51:06 +01:00
|
|
|
while (true) {
|
2019-03-22 15:17:10 +01:00
|
|
|
void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
|
2013-11-11 14:41:42 +01:00
|
|
|
if (!socket)
|
|
|
|
break;
|
2013-11-19 16:51:06 +01:00
|
|
|
sockets.push_back (socket);
|
2013-11-11 14:41:42 +01:00
|
|
|
}
|
2019-03-22 15:17:10 +01:00
|
|
|
TEST_ASSERT_LESS_OR_EQUAL (no_of_sockets,
|
|
|
|
static_cast<int> (sockets.size ()));
|
2018-05-23 13:20:06 +02:00
|
|
|
printf ("Socket creation failed after %i sockets\n",
|
|
|
|
static_cast<int> (sockets.size ()));
|
2013-11-11 14:41:42 +01:00
|
|
|
|
2013-11-19 16:51:06 +01:00
|
|
|
// System is out of resources, further calls to zmq_socket should return NULL
|
|
|
|
for (unsigned int i = 0; i < 10; ++i) {
|
2020-02-04 12:37:31 +01:00
|
|
|
TEST_ASSERT_NULL (zmq_socket (get_test_context (), ZMQ_PAIR));
|
2013-11-11 14:41:42 +01:00
|
|
|
}
|
|
|
|
// Clean up.
|
2013-11-19 16:51:06 +01:00
|
|
|
for (unsigned int i = 0; i < sockets.size (); ++i)
|
2019-03-22 15:17:10 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (sockets[i]));
|
2013-11-11 14:41:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_zmq_default_max ()
|
|
|
|
{
|
2013-11-19 16:51:06 +01:00
|
|
|
// Keep allocating sockets until we hit the default limit
|
2013-11-11 14:41:42 +01:00
|
|
|
std::vector<void *> sockets;
|
|
|
|
|
2013-11-19 16:51:06 +01:00
|
|
|
while (true) {
|
2019-03-22 15:17:10 +01:00
|
|
|
void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
|
2013-11-11 14:41:42 +01:00
|
|
|
if (!socket)
|
|
|
|
break;
|
2013-11-19 16:51:06 +01:00
|
|
|
sockets.push_back (socket);
|
2013-11-11 14:41:42 +01:00
|
|
|
}
|
2013-11-19 16:51:06 +01:00
|
|
|
// We may stop sooner if system has fewer available sockets
|
2019-03-22 15:17:10 +01:00
|
|
|
TEST_ASSERT_LESS_OR_EQUAL (ZMQ_MAX_SOCKETS_DFLT, sockets.size ());
|
2013-11-07 17:47:55 +01:00
|
|
|
|
2013-11-19 16:51:06 +01:00
|
|
|
// Further calls to zmq_socket should return NULL
|
|
|
|
for (unsigned int i = 0; i < 10; ++i) {
|
2020-02-04 12:37:31 +01:00
|
|
|
TEST_ASSERT_NULL (zmq_socket (get_test_context (), ZMQ_PAIR));
|
2013-11-07 17:47:55 +01:00
|
|
|
}
|
|
|
|
|
2013-11-19 16:51:06 +01:00
|
|
|
// Clean up
|
|
|
|
for (unsigned int i = 0; i < sockets.size (); ++i)
|
2019-03-22 15:17:10 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (sockets[i]));
|
2013-11-11 14:41:42 +01:00
|
|
|
}
|
|
|
|
|
2013-11-19 16:51:06 +01:00
|
|
|
int main (void)
|
2013-11-11 14:41:42 +01:00
|
|
|
{
|
2013-11-19 16:51:06 +01:00
|
|
|
setup_test_environment ();
|
2013-11-07 17:47:55 +01:00
|
|
|
|
2019-03-22 15:17:10 +01:00
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_system_max);
|
|
|
|
RUN_TEST (test_zmq_default_max);
|
|
|
|
return UNITY_END ();
|
2013-11-07 17:47:55 +01:00
|
|
|
}
|