poller_ut.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <util/system/error.h>
  3. #include "pair.h"
  4. #include "poller.h"
  5. #include "pollerimpl.h"
  6. Y_UNIT_TEST_SUITE(TSocketPollerTest) {
  7. Y_UNIT_TEST(TestSimple) {
  8. SOCKET sockets[2];
  9. UNIT_ASSERT(SocketPair(sockets) == 0);
  10. TSocketHolder s1(sockets[0]);
  11. TSocketHolder s2(sockets[1]);
  12. TSocketPoller poller;
  13. poller.WaitRead(sockets[1], (void*)17);
  14. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  15. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  16. for (ui32 i = 0; i < 3; ++i) {
  17. char buf[] = {18};
  18. UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0));
  19. UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
  20. UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 1, 0));
  21. UNIT_ASSERT_VALUES_EQUAL(18, buf[0]);
  22. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  23. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  24. }
  25. }
  26. Y_UNIT_TEST(TestSimpleOneShot) {
  27. SOCKET sockets[2];
  28. UNIT_ASSERT(SocketPair(sockets) == 0);
  29. TSocketHolder s1(sockets[0]);
  30. TSocketHolder s2(sockets[1]);
  31. TSocketPoller poller;
  32. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  33. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  34. for (ui32 i = 0; i < 3; ++i) {
  35. poller.WaitReadOneShot(sockets[1], (void*)17);
  36. char buf[1];
  37. buf[0] = i + 20;
  38. UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0));
  39. UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
  40. UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 1, 0));
  41. UNIT_ASSERT_VALUES_EQUAL(char(i + 20), buf[0]);
  42. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  43. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  44. buf[0] = i + 21;
  45. UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0));
  46. // this fails if socket is not oneshot
  47. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  48. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  49. UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 1, 0));
  50. UNIT_ASSERT_VALUES_EQUAL(char(i + 21), buf[0]);
  51. }
  52. }
  53. Y_UNIT_TEST(TestItIsSafeToUnregisterUnregisteredDescriptor) {
  54. SOCKET sockets[2];
  55. UNIT_ASSERT(SocketPair(sockets) == 0);
  56. TSocketHolder s1(sockets[0]);
  57. TSocketHolder s2(sockets[1]);
  58. TSocketPoller poller;
  59. poller.Unwait(s1);
  60. }
  61. Y_UNIT_TEST(TestItIsSafeToReregisterDescriptor) {
  62. SOCKET sockets[2];
  63. UNIT_ASSERT(SocketPair(sockets) == 0);
  64. TSocketHolder s1(sockets[0]);
  65. TSocketHolder s2(sockets[1]);
  66. TSocketPoller poller;
  67. poller.WaitRead(s1, nullptr);
  68. poller.WaitRead(s1, nullptr);
  69. poller.WaitWrite(s1, nullptr);
  70. }
  71. Y_UNIT_TEST(TestSimpleEdgeTriggered) {
  72. SOCKET sockets[2];
  73. UNIT_ASSERT(SocketPair(sockets) == 0);
  74. TSocketHolder s1(sockets[0]);
  75. TSocketHolder s2(sockets[1]);
  76. SetNonBlock(sockets[1]);
  77. TSocketPoller poller;
  78. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  79. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  80. for (ui32 i = 0; i < 3; ++i) {
  81. poller.WaitReadWriteEdgeTriggered(sockets[1], (void*)17);
  82. // notify about writeble
  83. UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
  84. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  85. char buf[2];
  86. buf[0] = i + 10;
  87. buf[1] = i + 20;
  88. // send one byte
  89. UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0));
  90. UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
  91. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  92. // restart without reading
  93. poller.RestartReadWriteEdgeTriggered(sockets[1], (void*)17, false);
  94. // after restart read and write might generate separate events
  95. {
  96. void* events[3];
  97. size_t count = poller.WaitT(events, 3, TDuration::Zero());
  98. UNIT_ASSERT_GE(count, 1);
  99. UNIT_ASSERT_LE(count, 2);
  100. UNIT_ASSERT_VALUES_EQUAL(events[0], (void*)17);
  101. }
  102. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  103. // second two more bytes
  104. UNIT_ASSERT_VALUES_EQUAL(2, send(sockets[0], buf, 2, 0));
  105. // here poller could notify or not because we haven't seen end
  106. Y_UNUSED(poller.WaitT(TDuration::Zero()));
  107. // recv one, leave two
  108. UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 1, 0));
  109. UNIT_ASSERT_VALUES_EQUAL(char(i + 10), buf[0]);
  110. // nothing new
  111. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  112. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  113. // recv the rest
  114. UNIT_ASSERT_VALUES_EQUAL(2, recv(sockets[1], buf, 2, 0));
  115. UNIT_ASSERT_VALUES_EQUAL(char(i + 10), buf[0]);
  116. UNIT_ASSERT_VALUES_EQUAL(char(i + 20), buf[1]);
  117. // still nothing new
  118. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  119. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  120. // hit end
  121. ClearLastSystemError();
  122. UNIT_ASSERT_VALUES_EQUAL(-1, recv(sockets[1], buf, 1, 0));
  123. UNIT_ASSERT_VALUES_EQUAL(EAGAIN, LastSystemError());
  124. // restart after end (noop for epoll)
  125. poller.RestartReadWriteEdgeTriggered(sockets[1], (void*)17, true);
  126. // send and recv byte
  127. UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0));
  128. UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
  129. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  130. // recv and see end
  131. UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 2, 0));
  132. UNIT_ASSERT_VALUES_EQUAL(char(i + 10), buf[0]);
  133. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  134. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  135. // the same but send before restart
  136. UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0));
  137. // restart after end (noop for epoll)
  138. poller.RestartReadWriteEdgeTriggered(sockets[1], (void*)17, true);
  139. UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
  140. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  141. UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 2, 0));
  142. UNIT_ASSERT_VALUES_EQUAL(char(i + 10), buf[0]);
  143. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  144. UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
  145. poller.Unwait(sockets[1]);
  146. }
  147. }
  148. #if defined(HAVE_EPOLL_POLLER)
  149. Y_UNIT_TEST(TestRdhup) {
  150. SOCKET sockets[2];
  151. UNIT_ASSERT(SocketPair(sockets) == 0);
  152. TSocketHolder s1(sockets[0]);
  153. TSocketHolder s2(sockets[1]);
  154. char buf[1] = {0};
  155. UNIT_ASSERT_VALUES_EQUAL(1, send(s1, buf, 1, 0));
  156. shutdown(s1, SHUT_WR);
  157. using TPoller = TGenericPoller<TEpollPoller<TWithoutLocking>>;
  158. TPoller poller;
  159. poller.Set((void*)17, s2, CONT_POLL_RDHUP);
  160. TPoller::TEvent e;
  161. UNIT_ASSERT_VALUES_EQUAL(poller.WaitD(&e, 1, TDuration::Zero().ToDeadLine()), 1);
  162. UNIT_ASSERT_EQUAL(TPoller::ExtractStatus(&e), 0);
  163. UNIT_ASSERT_EQUAL(TPoller::ExtractFilter(&e), CONT_POLL_RDHUP);
  164. UNIT_ASSERT_EQUAL(TPoller::ExtractEvent(&e), (void*)17);
  165. }
  166. Y_UNIT_TEST(TestSetSocketErrors) {
  167. TGenericPoller<TEpollPoller<TWithoutLocking>> poller;
  168. UNIT_ASSERT_EXCEPTION_CONTAINS(poller.Set(nullptr, Max<int>(), CONT_POLL_READ), TSystemError, "epoll add failed");
  169. UNIT_ASSERT_EXCEPTION_CONTAINS(poller.Set(nullptr, Max<int>(), CONT_POLL_READ | CONT_POLL_MODIFY), TSystemError, "epoll modify failed");
  170. }
  171. #endif
  172. }