libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_many_sockets.cpp
Go to the documentation of this file.
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 <zmq.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <vector>
35 
37 {
38  // Keep allocating sockets until we run out of system resources
39  const int no_of_sockets = 2 * 65536;
40  void *ctx = zmq_ctx_new ();
41  zmq_ctx_set (ctx, ZMQ_MAX_SOCKETS, no_of_sockets);
42  std::vector <void*> sockets;
43 
44  while (true) {
45  void *socket = zmq_socket (ctx, ZMQ_PAIR);
46  if (!socket)
47  break;
48  sockets.push_back (socket);
49  }
50  assert ((int) sockets.size () <= no_of_sockets);
51 
52  // System is out of resources, further calls to zmq_socket should return NULL
53  for (unsigned int i = 0; i < 10; ++i) {
54  void *socket = zmq_socket (ctx, ZMQ_PAIR);
55  assert (socket == NULL);
56  }
57  // Clean up.
58  for (unsigned int i = 0; i < sockets.size (); ++i)
59  zmq_close (sockets [i]);
60 
61  zmq_ctx_destroy (ctx);
62 }
63 
65 {
66  // Keep allocating sockets until we hit the default limit
67  void *ctx = zmq_ctx_new ();
68  std::vector<void*> sockets;
69 
70  while (true) {
71  void *socket = zmq_socket (ctx, ZMQ_PAIR);
72  if (!socket)
73  break;
74  sockets.push_back (socket);
75  }
76  // We may stop sooner if system has fewer available sockets
77  assert (sockets.size () <= ZMQ_MAX_SOCKETS_DFLT);
78 
79  // Further calls to zmq_socket should return NULL
80  for (unsigned int i = 0; i < 10; ++i) {
81  void *socket = zmq_socket (ctx, ZMQ_PAIR);
82  assert (socket == NULL);
83  }
84 
85  // Clean up
86  for (unsigned int i = 0; i < sockets.size (); ++i)
87  zmq_close (sockets [i]);
88 
89  zmq_ctx_destroy (ctx);
90 }
91 
92 int main (void)
93 {
95 
96  test_system_max ();
98 
99  return 0;
100 }
#define ZMQ_MAX_SOCKETS_DFLT
Definition: zmq.h:198
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_MAX_SOCKETS
Definition: zmq.h:190
void test_system_max()
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
void test_zmq_default_max()
ZMQ_EXPORT int zmq_ctx_set(void *context, int option, int optval)
Definition: zmq.cpp:200
int main(void)
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
ZMQ_EXPORT int zmq_ctx_destroy(void *context)
Definition: zmq.cpp:236
#define ZMQ_PAIR
Definition: zmq.h:246