基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_async_event_timeout.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. 
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include <conio.h>
  6. #include <thread>
  7. #include "librf.h"
  8. using namespace resumef;
  9. future_vt resumalbe_set_event(const event_t & e, std::chrono::milliseconds dt)
  10. {
  11. co_await resumef::sleep_for(dt);
  12. e.signal();
  13. std::cout << "+";
  14. }
  15. void async_set_event(const event_t & e, std::chrono::milliseconds dt)
  16. {
  17. std::thread([=]
  18. {
  19. std::this_thread::sleep_for(dt);
  20. e.signal();
  21. }).detach();
  22. }
  23. void test_wait_timeout_one()
  24. {
  25. std::cout << __FUNCTION__ << std::endl;
  26. using namespace std::chrono;
  27. event_t evt;
  28. go [&evt]() -> future_vt
  29. {
  30. intptr_t counter = 0;
  31. for (;;)
  32. {
  33. if (co_await evt.wait_for(500ms))
  34. break;
  35. ++counter;
  36. std::cout << ".";
  37. }
  38. std::cout << counter << std::endl;
  39. };
  40. async_set_event(evt, 10s + 50ms);
  41. //go resumalbe_set_event(evt, 10s + 50ms);
  42. this_scheduler()->run_until_notask();
  43. }
  44. void test_wait_timeout_any_invalid()
  45. {
  46. std::cout << __FUNCTION__ << std::endl;
  47. using namespace std::chrono;
  48. event_t evts[8];
  49. //无效的等待
  50. go[&]()-> future_vt
  51. {
  52. intptr_t idx = co_await event_t::wait_any_for(500ms, std::begin(evts), std::end(evts));
  53. assert(idx < 0);
  54. std::cout << "invalid wait!" << std::endl;
  55. };
  56. this_scheduler()->run_until_notask();
  57. }
  58. void test_wait_timeout_any()
  59. {
  60. std::cout << __FUNCTION__ << std::endl;
  61. using namespace std::chrono;
  62. event_t evts[8];
  63. go[&]() -> future_vt
  64. {
  65. intptr_t counter = 0;
  66. for (;;)
  67. {
  68. intptr_t idx = co_await event_t::wait_any_for(500ms, evts);
  69. if (idx >= 0)
  70. {
  71. std::cout << counter << std::endl;
  72. std::cout << "event " << idx << " signal!" << std::endl;
  73. break;
  74. }
  75. ++counter;
  76. std::cout << ".";
  77. }
  78. //取消剩下的定时器,以便于协程调度器退出来
  79. this_scheduler()->timer()->clear();
  80. };
  81. srand((int)time(nullptr));
  82. for (auto & e : evts)
  83. {
  84. //go resumalbe_set_event(e, 1ms * (1000 + rand() % 5000));
  85. async_set_event(e, 1ms * (1000 + rand() % 5000));
  86. }
  87. this_scheduler()->run_until_notask();
  88. }
  89. void test_wait_timeout_all_invalid()
  90. {
  91. std::cout << __FUNCTION__ << std::endl;
  92. using namespace std::chrono;
  93. event_t evts[8];
  94. //无效的等待
  95. go[&]()-> future_vt
  96. {
  97. bool result = co_await event_t::wait_all_for(500ms, std::begin(evts), std::end(evts));
  98. assert(!result);
  99. std::cout << "invalid wait!" << std::endl;
  100. };
  101. this_scheduler()->run_until_notask();
  102. }
  103. void test_wait_timeout_all()
  104. {
  105. std::cout << __FUNCTION__ << std::endl;
  106. using namespace std::chrono;
  107. event_t evts[8];
  108. go[&]() -> future_vt
  109. {
  110. intptr_t counter = 0;
  111. for (;;)
  112. {
  113. if (co_await event_t::wait_all_until(system_clock::now() + 500ms, evts))
  114. {
  115. std::cout << counter << std::endl;
  116. std::cout << "all event signal!" << std::endl;
  117. break;
  118. }
  119. ++counter;
  120. std::cout << ".";
  121. }
  122. };
  123. srand((int)time(nullptr));
  124. for (auto & e : evts)
  125. {
  126. go resumalbe_set_event(e, 1ms * (1000 + rand() % 5000));
  127. //async_set_event(e, 1ms * (1000 + rand() % 5000));
  128. }
  129. this_scheduler()->run_until_notask();
  130. }
  131. void resumable_main_event_timeout()
  132. {
  133. using namespace std::chrono;
  134. test_wait_timeout_one();
  135. std::cout << std::endl;
  136. test_wait_timeout_any_invalid();
  137. std::cout << std::endl << std::endl;
  138. test_wait_timeout_any();
  139. std::cout << std::endl << std::endl;
  140. test_wait_timeout_all_invalid();
  141. std::cout << std::endl;
  142. test_wait_timeout_all();
  143. std::cout << std::endl;
  144. }