test_many_sockets.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include "testutil_unity.hpp"
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <vector>
9 
11 
13 {
14  // Keep allocating sockets until we run out of system resources
15  const int no_of_sockets = 2 * 65536;
16  zmq_ctx_set (get_test_context (), ZMQ_MAX_SOCKETS, no_of_sockets);
17  std::vector<void *> sockets;
18 
19  while (true) {
20  void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
21  if (!socket)
22  break;
23  sockets.push_back (socket);
24  }
25  TEST_ASSERT_LESS_OR_EQUAL (no_of_sockets,
26  static_cast<int> (sockets.size ()));
27  printf ("Socket creation failed after %i sockets\n",
28  static_cast<int> (sockets.size ()));
29 
30  // System is out of resources, further calls to zmq_socket should return NULL
31  for (unsigned int i = 0; i < 10; ++i) {
33  }
34  // Clean up.
35  for (unsigned int i = 0; i < sockets.size (); ++i)
37 }
38 
40 {
41  // Keep allocating sockets until we hit the default limit
42  std::vector<void *> sockets;
43 
44  while (true) {
45  void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
46  if (!socket)
47  break;
48  sockets.push_back (socket);
49  }
50  // We may stop sooner if system has fewer available sockets
52 
53  // Further calls to zmq_socket should return NULL
54  for (unsigned int i = 0; i < 10; ++i) {
56  }
57 
58  // Clean up
59  for (unsigned int i = 0; i < sockets.size (); ++i)
61 }
62 
63 int main (void)
64 {
66 
67  UNITY_BEGIN ();
70  return UNITY_END ();
71 }
ZMQ_MAX_SOCKETS_DFLT
#define ZMQ_MAX_SOCKETS_DFLT
Definition: zmq.h:194
zmq_ctx_set
ZMQ_EXPORT int zmq_ctx_set(void *context_, int option_, int optval_)
Definition: zmq.cpp:156
main
int main(void)
Definition: test_many_sockets.cpp:63
ZMQ_MAX_SOCKETS
#define ZMQ_MAX_SOCKETS
Definition: zmq.h:182
UNITY_END
return UNITY_END()
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
get_test_context
void * get_test_context()
Definition: testutil_unity.cpp:184
test_zmq_default_max
void test_zmq_default_max()
Definition: test_many_sockets.cpp:39
test_system_max
SETUP_TEARDOWN_TESTCONTEXT void test_system_max()
Definition: test_many_sockets.cpp:12
testutil_unity.hpp
testutil.hpp
TEST_ASSERT_LESS_OR_EQUAL
#define TEST_ASSERT_LESS_OR_EQUAL(threshold, actual)
Definition: unity.h:200
zmq_socket
ZMQ_EXPORT void * zmq_socket(void *, int type_)
Definition: zmq.cpp:230
zmq_close
ZMQ_EXPORT int zmq_close(void *s_)
Definition: zmq.cpp:241
i
int i
Definition: gmock-matchers_test.cc:764
ZMQ_PAIR
#define ZMQ_PAIR
Definition: zmq.h:258
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
TEST_ASSERT_NULL
#define TEST_ASSERT_NULL(pointer)
Definition: unity.h:124


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59