IOEventLoop_test.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "IOEventLoop.h"
  17. #include <gtest/gtest.h>
  18. #include <atomic>
  19. #include <chrono>
  20. #include <thread>
  21. #include <android-base/logging.h>
  22. TEST(IOEventLoop, read) {
  23. int fd[2];
  24. ASSERT_EQ(0, pipe(fd));
  25. IOEventLoop loop;
  26. int count = 0;
  27. int retry_count = 0;
  28. ASSERT_NE(nullptr, loop.AddReadEvent(fd[0], [&]() {
  29. while (true) {
  30. char c;
  31. int ret = read(fd[0], &c, 1);
  32. if (ret == 1) {
  33. if (++count == 100) {
  34. return loop.ExitLoop();
  35. }
  36. } else if (ret == -1 && errno == EAGAIN) {
  37. retry_count++;
  38. break;
  39. } else {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }));
  45. std::thread thread([&]() {
  46. for (int i = 0; i < 100; ++i) {
  47. usleep(1000);
  48. char c;
  49. CHECK_EQ(write(fd[1], &c, 1), 1);
  50. }
  51. });
  52. ASSERT_TRUE(loop.RunLoop());
  53. thread.join();
  54. ASSERT_EQ(100, count);
  55. // Test retry_count to make sure we are not doing blocking read.
  56. ASSERT_GT(retry_count, 0);
  57. close(fd[0]);
  58. close(fd[1]);
  59. }
  60. TEST(IOEventLoop, write) {
  61. int fd[2];
  62. ASSERT_EQ(0, pipe(fd));
  63. IOEventLoop loop;
  64. int count = 0;
  65. ASSERT_NE(nullptr, loop.AddWriteEvent(fd[1], [&]() {
  66. int ret = 0;
  67. char buf[4096];
  68. while ((ret = write(fd[1], buf, sizeof(buf))) > 0) {
  69. }
  70. if (ret == -1 && errno == EAGAIN) {
  71. if (++count == 100) {
  72. loop.ExitLoop();
  73. }
  74. return true;
  75. }
  76. return false;
  77. }));
  78. std::thread thread([&]() {
  79. usleep(500000);
  80. while (true) {
  81. usleep(1000);
  82. char buf[4096];
  83. if (read(fd[0], buf, sizeof(buf)) <= 0) {
  84. break;
  85. }
  86. }
  87. });
  88. ASSERT_TRUE(loop.RunLoop());
  89. // close fd[1] to make read thread stop.
  90. close(fd[1]);
  91. thread.join();
  92. close(fd[0]);
  93. ASSERT_EQ(100, count);
  94. }
  95. TEST(IOEventLoop, signal) {
  96. IOEventLoop loop;
  97. int count = 0;
  98. ASSERT_TRUE(loop.AddSignalEvent(SIGINT, [&]() {
  99. if (++count == 100) {
  100. loop.ExitLoop();
  101. }
  102. return true;
  103. }));
  104. std::atomic<bool> stop_thread(false);
  105. std::thread thread([&]() {
  106. while (!stop_thread) {
  107. usleep(1000);
  108. kill(getpid(), SIGINT);
  109. }
  110. });
  111. ASSERT_TRUE(loop.RunLoop());
  112. stop_thread = true;
  113. thread.join();
  114. ASSERT_EQ(100, count);
  115. }
  116. void TestPeriodicEvents(int period_in_us, int iterations, bool precise) {
  117. timeval tv;
  118. tv.tv_sec = period_in_us / 1000000;
  119. tv.tv_usec = period_in_us % 1000000;
  120. int count = 0;
  121. IOEventLoop loop;
  122. if (precise) {
  123. ASSERT_TRUE(loop.UsePreciseTimer());
  124. }
  125. ASSERT_TRUE(loop.AddPeriodicEvent(tv, [&]() {
  126. if (++count == iterations) {
  127. loop.ExitLoop();
  128. }
  129. return true;
  130. }));
  131. auto start_time = std::chrono::steady_clock::now();
  132. ASSERT_TRUE(loop.RunLoop());
  133. auto end_time = std::chrono::steady_clock::now();
  134. ASSERT_EQ(iterations, count);
  135. double time_used = std::chrono::duration_cast<std::chrono::duration<double>>(
  136. end_time - start_time).count();
  137. double min_time_in_sec = period_in_us / 1e6 * iterations;
  138. double max_time_in_sec = min_time_in_sec + (precise ? 0.1 : 1);
  139. ASSERT_GE(time_used, min_time_in_sec);
  140. ASSERT_LT(time_used, max_time_in_sec);
  141. }
  142. TEST(IOEventLoop, periodic) {
  143. TestPeriodicEvents(1000000, 1, false);
  144. }
  145. TEST(IOEventLoop, periodic_precise) {
  146. TestPeriodicEvents(1000, 100, true);
  147. }
  148. TEST(IOEventLoop, read_and_del_event) {
  149. int fd[2];
  150. ASSERT_EQ(0, pipe(fd));
  151. IOEventLoop loop;
  152. int count = 0;
  153. IOEventRef ref = loop.AddReadEvent(fd[0], [&]() {
  154. count++;
  155. return IOEventLoop::DelEvent(ref);
  156. });
  157. ASSERT_NE(nullptr, ref);
  158. std::thread thread([&]() {
  159. for (int i = 0; i < 100; ++i) {
  160. usleep(1000);
  161. char c;
  162. CHECK_EQ(write(fd[1], &c, 1), 1);
  163. }
  164. });
  165. ASSERT_TRUE(loop.RunLoop());
  166. thread.join();
  167. ASSERT_EQ(1, count);
  168. close(fd[0]);
  169. close(fd[1]);
  170. }
  171. TEST(IOEventLoop, disable_enable_event) {
  172. int fd[2];
  173. ASSERT_EQ(0, pipe(fd));
  174. IOEventLoop loop;
  175. int count = 0;
  176. IOEventRef ref = loop.AddWriteEvent(fd[1], [&]() {
  177. count++;
  178. return IOEventLoop::DisableEvent(ref);
  179. });
  180. ASSERT_NE(nullptr, ref);
  181. timeval tv;
  182. tv.tv_sec = 0;
  183. tv.tv_usec = 500000;
  184. int periodic_count = 0;
  185. ASSERT_TRUE(loop.AddPeriodicEvent(tv, [&]() {
  186. periodic_count++;
  187. if (periodic_count == 1) {
  188. if (count != 1) {
  189. return false;
  190. }
  191. return IOEventLoop::EnableEvent(ref);
  192. } else {
  193. if (count != 2) {
  194. return false;
  195. }
  196. return loop.ExitLoop();
  197. }
  198. }));
  199. ASSERT_TRUE(loop.RunLoop());
  200. ASSERT_EQ(2, count);
  201. ASSERT_EQ(2, periodic_count);
  202. close(fd[0]);
  203. close(fd[1]);
  204. }
  205. TEST(IOEventLoop, disable_enable_periodic_event) {
  206. timeval tv;
  207. tv.tv_sec = 0;
  208. tv.tv_usec = 200000;
  209. IOEventLoop loop;
  210. IOEventRef wait_ref = loop.AddPeriodicEvent(tv, [&]() { return loop.ExitLoop(); });
  211. ASSERT_TRUE(wait_ref != nullptr);
  212. ASSERT_TRUE(loop.DisableEvent(wait_ref));
  213. tv.tv_sec = 0;
  214. tv.tv_usec = 100000;
  215. size_t periodic_count = 0;
  216. IOEventRef ref = loop.AddPeriodicEvent(tv, [&]() {
  217. if (!loop.DisableEvent(ref)) {
  218. return false;
  219. }
  220. periodic_count++;
  221. if (periodic_count < 2u) {
  222. return loop.EnableEvent(ref);
  223. }
  224. return loop.EnableEvent(wait_ref);
  225. });
  226. ASSERT_TRUE(loop.RunLoop());
  227. ASSERT_EQ(2u, periodic_count);
  228. }
  229. TEST(IOEventLoop, exit_before_loop) {
  230. IOEventLoop loop;
  231. ASSERT_TRUE(loop.ExitLoop());
  232. }