test_ctx_destroy.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #include <unity.h>
  27. void setUp ()
  28. {
  29. }
  30. void tearDown ()
  31. {
  32. }
  33. static void receiver (void *socket_)
  34. {
  35. char buffer[16];
  36. int rc = zmq_recv (socket_, &buffer, sizeof (buffer), 0);
  37. // TODO which error is expected here? use TEST_ASSERT_FAILURE_ERRNO instead
  38. TEST_ASSERT_EQUAL_INT (-1, rc);
  39. }
  40. void test_ctx_destroy ()
  41. {
  42. // Set up our context and sockets
  43. void *ctx = zmq_ctx_new ();
  44. TEST_ASSERT_NOT_NULL (ctx);
  45. void *socket = zmq_socket (ctx, ZMQ_PULL);
  46. TEST_ASSERT_NOT_NULL (socket);
  47. // Close the socket
  48. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket));
  49. // Destroy the context
  50. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_destroy (ctx));
  51. }
  52. void test_ctx_shutdown ()
  53. {
  54. // Set up our context and sockets
  55. void *ctx = zmq_ctx_new ();
  56. TEST_ASSERT_NOT_NULL (ctx);
  57. void *socket = zmq_socket (ctx, ZMQ_PULL);
  58. TEST_ASSERT_NOT_NULL (socket);
  59. // Spawn a thread to receive on socket
  60. void *receiver_thread = zmq_threadstart (&receiver, socket);
  61. // Wait for thread to start up and block
  62. msleep (SETTLE_TIME);
  63. // Shutdown context, if we used destroy here we would deadlock.
  64. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_shutdown (ctx));
  65. // Wait for thread to finish
  66. zmq_threadclose (receiver_thread);
  67. // Close the socket.
  68. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket));
  69. // Destroy the context, will now not hang as we have closed the socket.
  70. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_destroy (ctx));
  71. }
  72. void test_ctx_shutdown_socket_opened_after ()
  73. {
  74. // Set up our context.
  75. void *ctx = zmq_ctx_new ();
  76. TEST_ASSERT_NOT_NULL (ctx);
  77. // Open a socket to start context, and close it immediately again.
  78. void *socket = zmq_socket (ctx, ZMQ_PULL);
  79. TEST_ASSERT_NOT_NULL (socket);
  80. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket));
  81. // Shutdown context.
  82. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_shutdown (ctx));
  83. // Opening socket should now fail.
  84. TEST_ASSERT_NULL (zmq_socket (ctx, ZMQ_PULL));
  85. TEST_ASSERT_FAILURE_ERRNO (ETERM, -1);
  86. // Destroy the context.
  87. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_destroy (ctx));
  88. }
  89. void test_ctx_shutdown_only_socket_opened_after ()
  90. {
  91. // Set up our context.
  92. void *ctx = zmq_ctx_new ();
  93. TEST_ASSERT_NOT_NULL (ctx);
  94. // Shutdown context.
  95. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_shutdown (ctx));
  96. // Opening socket should now fail.
  97. TEST_ASSERT_NULL (zmq_socket (ctx, ZMQ_PULL));
  98. TEST_ASSERT_FAILURE_ERRNO (ETERM, -1);
  99. // Destroy the context.
  100. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_destroy (ctx));
  101. }
  102. void test_zmq_ctx_term_null_fails ()
  103. {
  104. int rc = zmq_ctx_term (NULL);
  105. TEST_ASSERT_EQUAL_INT (-1, rc);
  106. TEST_ASSERT_EQUAL_INT (EFAULT, errno);
  107. }
  108. void test_zmq_term_null_fails ()
  109. {
  110. int rc = zmq_term (NULL);
  111. TEST_ASSERT_EQUAL_INT (-1, rc);
  112. TEST_ASSERT_EQUAL_INT (EFAULT, errno);
  113. }
  114. void test_zmq_ctx_shutdown_null_fails ()
  115. {
  116. int rc = zmq_ctx_shutdown (NULL);
  117. TEST_ASSERT_EQUAL_INT (-1, rc);
  118. TEST_ASSERT_EQUAL_INT (EFAULT, errno);
  119. }
  120. #ifdef ZMQ_HAVE_POLLER
  121. struct poller_test_data_t
  122. {
  123. int socket_type;
  124. void *ctx;
  125. void *counter;
  126. };
  127. void run_poller (void *data_)
  128. {
  129. const poller_test_data_t *const poller_test_data =
  130. static_cast<const poller_test_data_t *> (data_);
  131. void *socket =
  132. zmq_socket (poller_test_data->ctx, poller_test_data->socket_type);
  133. TEST_ASSERT_NOT_NULL (socket);
  134. void *poller = zmq_poller_new ();
  135. TEST_ASSERT_NOT_NULL (poller);
  136. TEST_ASSERT_SUCCESS_ERRNO (
  137. zmq_poller_add (poller, socket, NULL, ZMQ_POLLIN));
  138. zmq_atomic_counter_set (poller_test_data->counter, 1);
  139. zmq_poller_event_t event;
  140. TEST_ASSERT_FAILURE_ERRNO (ETERM, zmq_poller_wait (poller, &event, -1));
  141. TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_destroy (&poller));
  142. // Close the socket
  143. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (socket));
  144. }
  145. #endif
  146. void test_poller_exists_with_socket_on_zmq_ctx_term (const int socket_type_)
  147. {
  148. #ifdef ZMQ_HAVE_POLLER
  149. struct poller_test_data_t poller_test_data;
  150. poller_test_data.socket_type = socket_type_;
  151. // Set up our context and sockets
  152. poller_test_data.ctx = zmq_ctx_new ();
  153. TEST_ASSERT_NOT_NULL (poller_test_data.ctx);
  154. poller_test_data.counter = zmq_atomic_counter_new ();
  155. TEST_ASSERT_NOT_NULL (poller_test_data.counter);
  156. void *thread = zmq_threadstart (run_poller, &poller_test_data);
  157. TEST_ASSERT_NOT_NULL (thread);
  158. while (zmq_atomic_counter_value (poller_test_data.counter) == 0) {
  159. msleep (10);
  160. }
  161. // Destroy the context
  162. TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_destroy (poller_test_data.ctx));
  163. zmq_threadclose (thread);
  164. zmq_atomic_counter_destroy (&poller_test_data.counter);
  165. #else
  166. TEST_IGNORE_MESSAGE ("libzmq without zmq_poller_* support, ignoring test");
  167. #endif
  168. }
  169. void test_poller_exists_with_socket_on_zmq_ctx_term_thread_safe_socket ()
  170. {
  171. #ifdef ZMQ_BUILD_DRAFT_API
  172. test_poller_exists_with_socket_on_zmq_ctx_term (ZMQ_CLIENT);
  173. #else
  174. TEST_IGNORE_MESSAGE ("libzmq without DRAFT support, ignoring test");
  175. #endif
  176. }
  177. void test_poller_exists_with_socket_on_zmq_ctx_term_non_thread_safe_socket ()
  178. {
  179. test_poller_exists_with_socket_on_zmq_ctx_term (ZMQ_DEALER);
  180. }
  181. int main (void)
  182. {
  183. setup_test_environment ();
  184. UNITY_BEGIN ();
  185. RUN_TEST (test_ctx_destroy);
  186. RUN_TEST (test_ctx_shutdown);
  187. RUN_TEST (test_ctx_shutdown_socket_opened_after);
  188. RUN_TEST (test_ctx_shutdown_only_socket_opened_after);
  189. RUN_TEST (test_zmq_ctx_term_null_fails);
  190. RUN_TEST (test_zmq_term_null_fails);
  191. RUN_TEST (test_zmq_ctx_shutdown_null_fails);
  192. RUN_TEST (
  193. test_poller_exists_with_socket_on_zmq_ctx_term_non_thread_safe_socket);
  194. RUN_TEST (
  195. test_poller_exists_with_socket_on_zmq_ctx_term_thread_safe_socket);
  196. return UNITY_END ();
  197. }