test_spec_rep.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <stdlib.h>
  27. SETUP_TEARDOWN_TESTCONTEXT
  28. char connect_address[MAX_SOCKET_STRING];
  29. void test_fair_queue_in (const char *bind_address_)
  30. {
  31. void *rep = test_context_socket (ZMQ_REP);
  32. int timeout = 250;
  33. TEST_ASSERT_SUCCESS_ERRNO (
  34. zmq_setsockopt (rep, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  35. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (rep, bind_address_));
  36. size_t len = MAX_SOCKET_STRING;
  37. TEST_ASSERT_SUCCESS_ERRNO (
  38. zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, connect_address, &len));
  39. const size_t services = 5;
  40. void *reqs[services];
  41. for (size_t peer = 0; peer < services; ++peer) {
  42. reqs[peer] = test_context_socket (ZMQ_REQ);
  43. TEST_ASSERT_SUCCESS_ERRNO (
  44. zmq_setsockopt (reqs[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  45. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (reqs[peer], connect_address));
  46. }
  47. msleep (SETTLE_TIME);
  48. s_send_seq (reqs[0], "A", SEQ_END);
  49. s_recv_seq (rep, "A", SEQ_END);
  50. s_send_seq (rep, "A", SEQ_END);
  51. s_recv_seq (reqs[0], "A", SEQ_END);
  52. s_send_seq (reqs[0], "A", SEQ_END);
  53. s_recv_seq (rep, "A", SEQ_END);
  54. s_send_seq (rep, "A", SEQ_END);
  55. s_recv_seq (reqs[0], "A", SEQ_END);
  56. // TODO: following test fails randomly on some boxes
  57. #ifdef SOMEONE_FIXES_THIS
  58. // send N requests
  59. for (size_t peer = 0; peer < services; ++peer) {
  60. char *str = strdup ("A");
  61. str[0] += peer;
  62. s_send_seq (reqs[peer], str, SEQ_END);
  63. free (str);
  64. }
  65. // handle N requests
  66. for (size_t peer = 0; peer < services; ++peer) {
  67. char *str = strdup ("A");
  68. str[0] += peer;
  69. // Test fails here
  70. s_recv_seq (rep, str, SEQ_END);
  71. s_send_seq (rep, str, SEQ_END);
  72. s_recv_seq (reqs[peer], str, SEQ_END);
  73. free (str);
  74. }
  75. #endif
  76. test_context_socket_close_zero_linger (rep);
  77. for (size_t peer = 0; peer < services; ++peer)
  78. test_context_socket_close_zero_linger (reqs[peer]);
  79. }
  80. void test_envelope (const char *bind_address_)
  81. {
  82. void *rep = test_context_socket (ZMQ_REP);
  83. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (rep, bind_address_));
  84. size_t len = MAX_SOCKET_STRING;
  85. TEST_ASSERT_SUCCESS_ERRNO (
  86. zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, connect_address, &len));
  87. void *dealer = test_context_socket (ZMQ_DEALER);
  88. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, connect_address));
  89. // minimal envelope
  90. s_send_seq (dealer, 0, "A", SEQ_END);
  91. s_recv_seq (rep, "A", SEQ_END);
  92. s_send_seq (rep, "A", SEQ_END);
  93. s_recv_seq (dealer, 0, "A", SEQ_END);
  94. // big envelope
  95. s_send_seq (dealer, "X", "Y", 0, "A", SEQ_END);
  96. s_recv_seq (rep, "A", SEQ_END);
  97. s_send_seq (rep, "A", SEQ_END);
  98. s_recv_seq (dealer, "X", "Y", 0, "A", SEQ_END);
  99. test_context_socket_close_zero_linger (rep);
  100. test_context_socket_close_zero_linger (dealer);
  101. }
  102. const char bind_inproc[] = "inproc://a";
  103. const char bind_tcp[] = "tcp://127.0.0.1:*";
  104. void test_fair_queue_in_inproc ()
  105. {
  106. test_fair_queue_in (bind_inproc);
  107. }
  108. void test_fair_queue_in_tcp ()
  109. {
  110. test_fair_queue_in (bind_tcp);
  111. }
  112. void test_envelope_inproc ()
  113. {
  114. test_envelope (bind_inproc);
  115. }
  116. void test_envelope_tcp ()
  117. {
  118. test_envelope (bind_tcp);
  119. }
  120. int main ()
  121. {
  122. setup_test_environment ();
  123. UNITY_BEGIN ();
  124. // SHALL receive incoming messages from its peers using a fair-queuing
  125. // strategy.
  126. RUN_TEST (test_fair_queue_in_inproc);
  127. RUN_TEST (test_fair_queue_in_tcp);
  128. // For an incoming message:
  129. // SHALL remove and store the address envelope, including the delimiter.
  130. // SHALL pass the remaining data frames to its calling application.
  131. // SHALL wait for a single reply message from its calling application.
  132. // SHALL prepend the address envelope and delimiter.
  133. // SHALL deliver this message back to the originating peer.
  134. RUN_TEST (test_envelope_inproc);
  135. RUN_TEST (test_envelope_tcp);
  136. return UNITY_END ();
  137. }