test_disconnect_msg.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright (c) 2007-2016 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. SETUP_TEARDOWN_TESTCONTEXT
  27. void test (const char *address)
  28. {
  29. // Create a server
  30. void *server = test_context_socket (ZMQ_SERVER);
  31. // set server socket options
  32. TEST_ASSERT_SUCCESS_ERRNO (
  33. zmq_setsockopt (server, ZMQ_DISCONNECT_MSG, "D", 1));
  34. // bind server
  35. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, address));
  36. // Create a client
  37. void *client = test_context_socket (ZMQ_CLIENT);
  38. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (client, ZMQ_HELLO_MSG, "H", 1));
  39. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, address));
  40. // Receive the hello message from client
  41. recv_string_expect_success (server, "H", 0);
  42. // Kill the client
  43. test_context_socket_close (client);
  44. // Receive the disconnect message
  45. recv_string_expect_success (server, "D", 0);
  46. // Clean up.
  47. test_context_socket_close (server);
  48. }
  49. void test_tcp ()
  50. {
  51. test ("tcp://127.0.0.1:5569");
  52. }
  53. void test_inproc ()
  54. {
  55. test ("inproc://disconnect-msg");
  56. }
  57. void test_inproc_disconnect ()
  58. {
  59. const char *address = "inproc://disconnect-msg";
  60. // Create a server
  61. void *server = test_context_socket (ZMQ_SERVER);
  62. // set server socket options
  63. TEST_ASSERT_SUCCESS_ERRNO (
  64. zmq_setsockopt (server, ZMQ_DISCONNECT_MSG, "D", 1));
  65. // bind server
  66. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, address));
  67. // Create a client
  68. void *client = test_context_socket (ZMQ_CLIENT);
  69. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (client, ZMQ_HELLO_MSG, "H", 1));
  70. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, address));
  71. // Receive the hello message from client
  72. recv_string_expect_success (server, "H", 0);
  73. // disconnect the client
  74. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (client, address));
  75. // Receive the disconnect message
  76. recv_string_expect_success (server, "D", 0);
  77. // Clean up.
  78. test_context_socket_close (client);
  79. test_context_socket_close (server);
  80. }
  81. int main ()
  82. {
  83. setup_test_environment ();
  84. UNITY_BEGIN ();
  85. RUN_TEST (test_tcp);
  86. RUN_TEST (test_inproc);
  87. RUN_TEST (test_inproc_disconnect);
  88. return UNITY_END ();
  89. }