1/*
2 Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "testutil.hpp"
31#include "testutil_unity.hpp"
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <vector>
36
37SETUP_TEARDOWN_TESTCONTEXT
38
39void test_system_max ()
40{
41 // Keep allocating sockets until we run out of system resources
42 const int no_of_sockets = 2 * 65536;
43 zmq_ctx_set (get_test_context (), ZMQ_MAX_SOCKETS, no_of_sockets);
44 std::vector<void *> sockets;
45
46 while (true) {
47 void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
48 if (!socket)
49 break;
50 sockets.push_back (socket);
51 }
52 TEST_ASSERT_LESS_OR_EQUAL (no_of_sockets,
53 static_cast<int> (sockets.size ()));
54 printf ("Socket creation failed after %i sockets\n",
55 static_cast<int> (sockets.size ()));
56
57 // System is out of resources, further calls to zmq_socket should return NULL
58 for (unsigned int i = 0; i < 10; ++i) {
59 void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
60 TEST_ASSERT_NULL (socket);
61 }
62 // Clean up.
63 for (unsigned int i = 0; i < sockets.size (); ++i)
64 TEST_ASSERT_SUCCESS_ERRNO (zmq_close (sockets[i]));
65}
66
67void test_zmq_default_max ()
68{
69 // Keep allocating sockets until we hit the default limit
70 std::vector<void *> sockets;
71
72 while (true) {
73 void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
74 if (!socket)
75 break;
76 sockets.push_back (socket);
77 }
78 // We may stop sooner if system has fewer available sockets
79 TEST_ASSERT_LESS_OR_EQUAL (ZMQ_MAX_SOCKETS_DFLT, sockets.size ());
80
81 // Further calls to zmq_socket should return NULL
82 for (unsigned int i = 0; i < 10; ++i) {
83 void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
84 TEST_ASSERT_NULL (socket);
85 }
86
87 // Clean up
88 for (unsigned int i = 0; i < sockets.size (); ++i)
89 TEST_ASSERT_SUCCESS_ERRNO (zmq_close (sockets[i]));
90}
91
92int main (void)
93{
94 setup_test_environment ();
95
96 UNITY_BEGIN ();
97 RUN_TEST (test_system_max);
98 RUN_TEST (test_zmq_default_max);
99 return UNITY_END ();
100}
101