libzmq
tests
test_system.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
#if defined(ZMQ_HAVE_WINDOWS)
7
#include <winsock2.h>
8
#include <stdexcept>
9
#else
10
#include <sys/socket.h>
11
#include <netinet/in.h>
12
#include <unistd.h>
13
#endif
14
15
SETUP_TEARDOWN_TESTCONTEXT
16
17
// Solaris has a default of 256 max files per process
18
#ifdef ZMQ_HAVE_SOLARIS
19
#define MAX_SOCKETS 200
20
#else
21
#define MAX_SOCKETS 1000
22
#endif
23
24
#if defined(ZMQ_HAVE_WINDOWS)
25
26
void
initialise_network
(
void
)
27
{
28
WSADATA info;
29
if
(WSAStartup (MAKEWORD (2, 0), &info) != 0)
30
throw
std::runtime_error (
"Could not start WSA"
);
31
}
32
33
#else
34
35
void
initialise_network
(
void
)
36
{
37
}
38
39
#endif
40
41
void
test_localhost
()
42
{
43
// Check that we have local networking via ZeroMQ
44
void
*dealer =
test_context_socket
(
ZMQ_DEALER
);
45
if
(
zmq_bind
(dealer,
"tcp://127.0.0.1:*"
) == -1) {
46
TEST_FAIL_MESSAGE
(
47
"E: Cannot find 127.0.0.1 -- your system does not have local\n"
48
"E: networking. Please fix this before running libzmq checks.\n"
);
49
}
50
51
test_context_socket_close
(dealer);
52
}
53
54
void
test_max_sockets
()
55
{
56
// Check that we can create 1,000 sockets
57
fd_t
handle[
MAX_SOCKETS
];
58
int
count
;
59
for
(
count
= 0;
count
<
MAX_SOCKETS
;
count
++) {
60
handle[
count
] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
61
if
(handle[
count
] ==
retired_fd
) {
62
printf (
"W: Only able to create %d sockets on this box\n"
,
count
);
63
const
char
msg[] =
64
"I: Tune your system to increase maximum allowed file handles\n"
65
#if !defined(ZMQ_HAVE_WINDOWS)
66
"I: Run 'ulimit -n 1200' in bash\n"
67
#endif
68
;
69
TEST_FAIL_MESSAGE
(msg);
70
}
71
}
72
// Release the socket handles
73
for
(
count
= 0;
count
<
MAX_SOCKETS
;
count
++) {
74
close (handle[
count
]);
75
}
76
}
77
78
// This test case stresses the system to shake out known configuration
79
// problems. We're direct system calls when necessary. Some code may
80
// need wrapping to be properly portable.
81
82
int
main
(
void
)
83
{
84
initialise_network
();
85
86
UNITY_BEGIN
();
87
RUN_TEST
(
test_localhost
);
88
RUN_TEST
(
test_max_sockets
);
89
return
UNITY_END
();
90
}
initialise_network
void initialise_network(void)
Definition:
test_system.cpp:35
UNITY_END
return UNITY_END()
main
int main(void)
Definition:
test_system.cpp:82
RUN_TEST
#define RUN_TEST(func)
Definition:
unity_internals.h:615
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition:
testutil_unity.hpp:172
TEST_FAIL_MESSAGE
#define TEST_FAIL_MESSAGE(message)
Definition:
unity.h:101
retired_fd
@ retired_fd
Definition:
libzmq/tests/testutil.hpp:117
testutil_unity.hpp
ZMQ_DEALER
#define ZMQ_DEALER
Definition:
zmq.h:263
testutil.hpp
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition:
zmq.cpp:299
test_context_socket
void * test_context_socket(int type_)
Definition:
testutil_unity.cpp:200
test_localhost
void test_localhost()
Definition:
test_system.cpp:41
UNITY_BEGIN
UNITY_BEGIN()
fd_t
zmq_fd_t fd_t
Definition:
libzmq/tests/testutil.hpp:98
test_max_sockets
void test_max_sockets()
Definition:
test_system.cpp:54
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition:
testutil_unity.cpp:208
count
GLint GLsizei count
Definition:
glcorearb.h:2830
MAX_SOCKETS
#define MAX_SOCKETS
Definition:
test_system.cpp:21
libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59