test_system.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil.hpp"
  25. #include "testutil_unity.hpp"
  26. #if defined(ZMQ_HAVE_WINDOWS)
  27. #include <winsock2.h>
  28. #include <stdexcept>
  29. #else
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <unistd.h>
  33. #endif
  34. SETUP_TEARDOWN_TESTCONTEXT
  35. // Solaris has a default of 256 max files per process
  36. #ifdef ZMQ_HAVE_SOLARIS
  37. #define MAX_SOCKETS 200
  38. #else
  39. #define MAX_SOCKETS 1000
  40. #endif
  41. #if defined(ZMQ_HAVE_WINDOWS)
  42. void initialise_network (void)
  43. {
  44. WSADATA info;
  45. if (WSAStartup (MAKEWORD (2, 0), &info) != 0)
  46. throw std::runtime_error ("Could not start WSA");
  47. }
  48. #else
  49. void initialise_network (void)
  50. {
  51. }
  52. #endif
  53. void test_localhost ()
  54. {
  55. // Check that we have local networking via ZeroMQ
  56. void *dealer = test_context_socket (ZMQ_DEALER);
  57. if (zmq_bind (dealer, "tcp://127.0.0.1:*") == -1) {
  58. TEST_FAIL_MESSAGE (
  59. "E: Cannot find 127.0.0.1 -- your system does not have local\n"
  60. "E: networking. Please fix this before running libzmq checks.\n");
  61. }
  62. test_context_socket_close (dealer);
  63. }
  64. void test_max_sockets ()
  65. {
  66. // Check that we can create 1,000 sockets
  67. fd_t handle[MAX_SOCKETS];
  68. int count;
  69. for (count = 0; count < MAX_SOCKETS; count++) {
  70. handle[count] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  71. if (handle[count] == retired_fd) {
  72. printf ("W: Only able to create %d sockets on this box\n", count);
  73. const char msg[] =
  74. "I: Tune your system to increase maximum allowed file handles\n"
  75. #if !defined(ZMQ_HAVE_WINDOWS)
  76. "I: Run 'ulimit -n 1200' in bash\n"
  77. #endif
  78. ;
  79. TEST_FAIL_MESSAGE (msg);
  80. }
  81. }
  82. // Release the socket handles
  83. for (count = 0; count < MAX_SOCKETS; count++) {
  84. close (handle[count]);
  85. }
  86. }
  87. // This test case stresses the system to shake out known configuration
  88. // problems. We're direct system calls when necessary. Some code may
  89. // need wrapping to be properly portable.
  90. int main (void)
  91. {
  92. initialise_network ();
  93. UNITY_BEGIN ();
  94. RUN_TEST (test_localhost);
  95. RUN_TEST (test_max_sockets);
  96. return UNITY_END ();
  97. }