test_tc_coroutine.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //
  2. // Created by jarod on 2020/2/20.
  3. //
  4. #include "util/tc_coroutine.h"
  5. #include "util/tc_coroutine_queue.h"
  6. #include "util/tc_thread.h"
  7. #include "gtest/gtest.h"
  8. using namespace tars;
  9. class UtilCoroutineTest : public testing::Test
  10. {
  11. public:
  12. //添加日志
  13. static void SetUpTestCase()
  14. {
  15. // cout<<"SetUpTestCase"<<endl;
  16. }
  17. static void TearDownTestCase()
  18. {
  19. // cout<<"TearDownTestCase"<<endl;
  20. }
  21. virtual void SetUp() //TEST跑之前会执行SetUp
  22. {
  23. // cout<<"SetUp"<<endl;
  24. }
  25. virtual void TearDown() //TEST跑完之后会执行TearDown
  26. {
  27. // cout<<"TearDown"<<endl;
  28. }
  29. };
  30. class CoSleepThread : public TC_Thread
  31. {
  32. public:
  33. CoSleepThread() {}
  34. virtual void run()
  35. {
  36. int64_t at = TC_Common::now2ms();
  37. int64_t lt = 0;
  38. _scheduler->sleep(100);
  39. lt = TC_Common::now2ms();
  40. ASSERT_TRUE(lt - at >= 100 && lt - at <= 106);
  41. at = lt;
  42. _scheduler->sleep(100);
  43. lt = TC_Common::now2ms();
  44. ASSERT_TRUE(lt - at >= 100 && lt - at <= 106);
  45. at = lt;
  46. _scheduler->sleep(100);
  47. lt = TC_Common::now2ms();
  48. ASSERT_TRUE(lt - at >= 100 && lt - at <= 106);
  49. }
  50. };
  51. class CoYieldThread : public TC_Thread
  52. {
  53. public:
  54. CoYieldThread() {}
  55. virtual void run()
  56. {
  57. int64_t lt, at;
  58. int coroId = this->getScheduler()->getCoroutineId();
  59. {
  60. std::thread check([&]()
  61. {
  62. TC_Common::msleep(500);
  63. this->_scheduler->put(coroId);
  64. });
  65. at = TC_Common::now2ms();
  66. lt = 0;
  67. _scheduler->yield(false);
  68. lt = TC_Common::now2ms();
  69. ASSERT_TRUE(lt - at >= 500 && lt - at <= 506);
  70. check.join();
  71. }
  72. {
  73. std::thread check([&]()
  74. {
  75. TC_Common::msleep(500);
  76. this->_scheduler->notify();
  77. });
  78. at = lt;
  79. _scheduler->yield(true);
  80. lt = TC_Common::now2ms();
  81. ASSERT_TRUE(lt - at >= 500 && lt - at <= 506);
  82. check.join();
  83. }
  84. }
  85. };
  86. bool CoThreadDo = false;
  87. class CoThread : public TC_Thread
  88. {
  89. public:
  90. CoThread() {}
  91. virtual void run()
  92. {
  93. CoThreadDo = true;
  94. cout << "in coroutine" << endl;
  95. }
  96. };
  97. TEST_F(UtilCoroutineTest, CoThread)
  98. {
  99. CoThreadDo = false;
  100. CoThread* a = new CoThread();
  101. a->startCoroutine(10, 128*1024, true);
  102. a->getThreadControl().join();
  103. ASSERT_TRUE(CoThreadDo);
  104. delete a;
  105. }
  106. class CoThread1 : public TC_Thread
  107. {
  108. public:
  109. CoThread1() {}
  110. virtual void run()
  111. {
  112. TC_CoroutineScheduler::scheduler()->go(std::bind(&CoThread1::doCo, this));
  113. LOG_CONSOLE_DEBUG << "in coroutine" << endl;
  114. }
  115. void doCo()
  116. {
  117. CoThreadDo = true;
  118. LOG_CONSOLE_DEBUG << "in coroutine" << endl;
  119. }
  120. };
  121. TEST_F(UtilCoroutineTest, CoThread1)
  122. {
  123. CoThreadDo = false;
  124. CoThread1* a = new CoThread1();
  125. a->startCoroutine(10, 128*1024, true);
  126. a->getThreadControl().join();
  127. ASSERT_TRUE(CoThreadDo);
  128. delete a;
  129. }
  130. class CoAThread : public TC_Thread
  131. {
  132. public:
  133. CoAThread(TC_CoroutineQueue<int> * data): _queue(data) {}
  134. virtual void run()
  135. {
  136. for(int i = 0; i < 100; i++) {
  137. _queue->push_back(i);
  138. _scheduler->sleep(10);
  139. }
  140. }
  141. TC_CoroutineQueue<int> *_queue;
  142. };
  143. class CoBThread : public TC_Thread
  144. {
  145. public:
  146. CoBThread(TC_CoroutineQueue<int> * data): _queue(data) {}
  147. virtual void run()
  148. {
  149. _queue->exec([&](int i) {
  150. if(_last != -1) {
  151. ASSERT_TRUE(_last + 1 == i);
  152. }
  153. _last = i;
  154. });
  155. }
  156. int _last = -1;
  157. TC_CoroutineQueue<int> *_queue;
  158. };
  159. TEST_F(UtilCoroutineTest, coThreadQuit)
  160. {
  161. TC_CoroutineQueue<int> * data = new TC_CoroutineQueue<int>();
  162. CoAThread* a = new CoAThread(data);
  163. a->startCoroutine(10, 128*1024, true);
  164. a->getThreadControl().join();
  165. ASSERT_TRUE(data->size() == 100);
  166. delete data;
  167. delete a;
  168. }
  169. TEST_F(UtilCoroutineTest, coThread)
  170. {
  171. TC_CoroutineQueue<int> * data = new TC_CoroutineQueue<int>();
  172. CoAThread* a = new CoAThread(data);
  173. a->startCoroutine(10, 128*1024, false);
  174. std::thread start([=]{
  175. TC_Common::sleep(2);
  176. ASSERT_TRUE(data->size() == 100 );
  177. a->getScheduler()->terminate();
  178. });
  179. a->getThreadControl().join();
  180. start.join();
  181. delete data;
  182. delete a;
  183. }
  184. TEST_F(UtilCoroutineTest, coQueue)
  185. {
  186. TC_CoroutineQueue<int> * data = new TC_CoroutineQueue<int>();
  187. CoAThread* a = new CoAThread(data);
  188. CoBThread* b = new CoBThread(data);
  189. a->startCoroutine(10, 128*1024, true);
  190. b->startCoroutine(10, 128*1024, true);
  191. a->getThreadControl().join();
  192. b->_queue->terminate();
  193. b->getThreadControl().join();
  194. delete data;
  195. delete a;
  196. delete b;
  197. }
  198. TEST_F(UtilCoroutineTest, sleep)
  199. {
  200. CoSleepThread* a = new CoSleepThread();
  201. a->startCoroutine(10, 128*1024, true);
  202. a->getThreadControl().join();
  203. delete a;
  204. }
  205. TEST_F(UtilCoroutineTest, yield)
  206. {
  207. CoYieldThread* a = new CoYieldThread();
  208. a->startCoroutine(10, 128*1024, true);
  209. a->getThreadControl().join();
  210. delete a;
  211. }
  212. TEST_F(UtilCoroutineTest, aliveQuit)
  213. {
  214. CoSleepThread* a = new CoSleepThread();
  215. a->startCoroutine(10, 128*1024, true);
  216. TC_Common::msleep(100);
  217. ASSERT_TRUE(a->isAlive());
  218. a->getThreadControl().join();
  219. delete a;
  220. }
  221. TEST_F(UtilCoroutineTest, alive)
  222. {
  223. CoSleepThread* a = new CoSleepThread();
  224. a->startCoroutine(10, 128*1024, false);
  225. TC_Common::msleep(200);
  226. ASSERT_TRUE(a->isAlive());
  227. a->getScheduler()->terminate();
  228. a->getThreadControl().join();
  229. delete a;
  230. }
  231. class MyCoroutine : public TC_Coroutine
  232. {
  233. protected:
  234. void handle()
  235. {
  236. ++_count;
  237. this->go(std::bind(&MyCoroutine::co_test, this));
  238. }
  239. void co_test()
  240. {
  241. ++_count;
  242. }
  243. public:
  244. static atomic<int> _count;
  245. };
  246. atomic<int> MyCoroutine::_count{0};
  247. TEST_F(UtilCoroutineTest, coCoroutine)
  248. {
  249. MyCoroutine::_count = 0;
  250. MyCoroutine co;
  251. co.setCoroInfo(10, 200, 128*1024);
  252. co.start();
  253. co.join();
  254. ASSERT_TRUE(MyCoroutine::_count == 20);
  255. }
  256. TEST_F(UtilCoroutineTest, coCoroutineQuit)
  257. {
  258. std::thread cor_call([&]()
  259. {
  260. auto scheduler = TC_CoroutineScheduler::create();
  261. scheduler->go([&]()
  262. {
  263. scheduler->setNoCoroutineCallback([=](TC_CoroutineScheduler* s)
  264. {
  265. s->terminate();
  266. });
  267. TC_Common::msleep(100);
  268. });
  269. scheduler->run();
  270. });
  271. cor_call.join();
  272. }