reqrep.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Copyright (c) 2012 Martin Sustrik All rights reserved.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. and/or sell copies of the Software, and to permit persons to whom
  8. the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. */
  19. #include "../src/nn.h"
  20. #include "../src/reqrep.h"
  21. #include "testutil.h"
  22. #define SOCKET_ADDRESS "inproc://test"
  23. int main ()
  24. {
  25. int rc;
  26. int rep1;
  27. int rep2;
  28. int req1;
  29. int req2;
  30. int resend_ivl;
  31. char buf [7];
  32. int timeo;
  33. /* Test req/rep with full socket types. */
  34. rep1 = test_socket (AF_SP, NN_REP);
  35. test_bind (rep1, SOCKET_ADDRESS);
  36. req1 = test_socket (AF_SP, NN_REQ);
  37. test_connect (req1, SOCKET_ADDRESS);
  38. req2 = test_socket (AF_SP, NN_REQ);
  39. test_connect (req2, SOCKET_ADDRESS);
  40. /* Check invalid sequence of sends and recvs. */
  41. rc = nn_send (rep1, "ABC", 3, 0);
  42. nn_assert (rc == -1 && nn_errno () == EFSM);
  43. rc = nn_recv (req1, buf, sizeof (buf), 0);
  44. nn_assert (rc == -1 && nn_errno () == EFSM);
  45. /* Check fair queueing the requests. */
  46. test_send (req2, "ABC");
  47. test_recv (rep1, "ABC");
  48. test_send (rep1, "ABC");
  49. test_recv (req2, "ABC");
  50. test_send (req1, "ABC");
  51. test_recv (rep1, "ABC");
  52. test_send (rep1, "ABC");
  53. test_recv (req1, "ABC");
  54. test_close (rep1);
  55. test_close (req1);
  56. test_close (req2);
  57. /* Check load-balancing of requests. */
  58. req1 = test_socket (AF_SP, NN_REQ);
  59. test_bind (req1, SOCKET_ADDRESS);
  60. rep1 = test_socket (AF_SP, NN_REP);
  61. test_connect (rep1, SOCKET_ADDRESS);
  62. rep2 = test_socket (AF_SP, NN_REP);
  63. test_connect (rep2, SOCKET_ADDRESS);
  64. test_send (req1, "ABC");
  65. test_recv (rep1, "ABC");
  66. test_send (rep1, "ABC");
  67. test_recv (req1, "ABC");
  68. test_send (req1, "ABC");
  69. test_recv (rep2, "ABC");
  70. test_send (rep2, "ABC");
  71. test_recv (req1, "ABC");
  72. test_close (rep2);
  73. test_close (rep1);
  74. test_close (req1);
  75. /* Test re-sending of the request. */
  76. rep1 = test_socket (AF_SP, NN_REP);
  77. test_bind (rep1, SOCKET_ADDRESS);
  78. req1 = test_socket (AF_SP, NN_REQ);
  79. test_connect (req1, SOCKET_ADDRESS);
  80. resend_ivl = 100;
  81. rc = nn_setsockopt (req1, NN_REQ, NN_REQ_RESEND_IVL,
  82. &resend_ivl, sizeof (resend_ivl));
  83. errno_assert (rc == 0);
  84. test_send (req1, "ABC");
  85. test_recv (rep1, "ABC");
  86. /* The following waits for request to be resent */
  87. test_recv (rep1, "ABC");
  88. test_close (req1);
  89. test_close (rep1);
  90. /* Check sending a request when the peer is not available. (It should
  91. be sent immediatelly when the peer comes online rather than relying
  92. on the resend algorithm. */
  93. req1 = test_socket (AF_SP, NN_REQ);
  94. test_connect (req1, SOCKET_ADDRESS);
  95. test_send (req1, "ABC");
  96. rep1 = test_socket (AF_SP, NN_REP);
  97. test_bind (rep1, SOCKET_ADDRESS);
  98. timeo = 200;
  99. rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
  100. &timeo, sizeof (timeo));
  101. errno_assert (rc == 0);
  102. test_recv (rep1, "ABC");
  103. test_close (req1);
  104. test_close (rep1);
  105. /* Check removing socket request sent to (It should
  106. be sent immediatelly to other peer rather than relying
  107. on the resend algorithm). */
  108. req1 = test_socket (AF_SP, NN_REQ);
  109. test_bind (req1, SOCKET_ADDRESS);
  110. rep1 = test_socket (AF_SP, NN_REP);
  111. test_connect (rep1, SOCKET_ADDRESS);
  112. rep2 = test_socket (AF_SP, NN_REP);
  113. test_connect (rep2, SOCKET_ADDRESS);
  114. timeo = 200;
  115. rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
  116. &timeo, sizeof (timeo));
  117. errno_assert (rc == 0);
  118. rc = nn_setsockopt (rep2, NN_SOL_SOCKET, NN_RCVTIMEO,
  119. &timeo, sizeof (timeo));
  120. errno_assert (rc == 0);
  121. test_send (req1, "ABC");
  122. /* We got request through rep1 */
  123. test_recv (rep1, "ABC");
  124. /* But instead replying we simulate crash */
  125. test_close (rep1);
  126. /* The rep2 should get request immediately */
  127. test_recv (rep2, "ABC");
  128. /* Let's check it's delivered well */
  129. test_send (rep2, "REPLY");
  130. test_recv (req1, "REPLY");
  131. test_close (req1);
  132. test_close (rep2);
  133. /* Test cancelling delayed request */
  134. req1 = test_socket (AF_SP, NN_REQ);
  135. test_connect (req1, SOCKET_ADDRESS);
  136. test_send (req1, "ABC");
  137. test_send (req1, "DEF");
  138. rep1 = test_socket (AF_SP, NN_REP);
  139. test_bind (rep1, SOCKET_ADDRESS);
  140. timeo = 100;
  141. test_recv (rep1, "DEF");
  142. test_close (req1);
  143. test_close (rep1);
  144. return 0;
  145. }