142-thread_timer.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // Copyright (c) 2019-2020 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. #include <catch2/catch_test_macros.hpp>
  7. #include "rotor.hpp"
  8. #include "rotor/thread.hpp"
  9. #include "access.h"
  10. namespace r = rotor;
  11. namespace rth = rotor::thread;
  12. namespace pt = boost::posix_time;
  13. namespace rt = r::test;
  14. namespace payload {
  15. struct sample_res_t {};
  16. struct sample_req_t {
  17. using response_t = sample_res_t;
  18. };
  19. struct trigger_t {};
  20. } // namespace payload
  21. namespace message {
  22. using sample_req_t = r::request_traits_t<payload::sample_req_t>::request::message_t;
  23. using sample_res_t = r::request_traits_t<payload::sample_req_t>::response::message_t;
  24. using trigger_t = r::message_t<payload::trigger_t>;
  25. } // namespace message
  26. using req_ptr_t = r::intrusive_ptr_t<message::sample_req_t>;
  27. struct bad_actor_t : public r::actor_base_t {
  28. using r::actor_base_t::actor_base_t;
  29. r::extended_error_ptr_t ee;
  30. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  31. r::actor_base_t::configure(plugin);
  32. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&bad_actor_t::on_response); });
  33. }
  34. void on_start() noexcept override {
  35. r::actor_base_t::on_start();
  36. // for coverage
  37. auto sup = static_cast<rth::supervisor_thread_t *>(supervisor);
  38. sup->update_time();
  39. start_timer(r::pt::milliseconds(1), *this, &bad_actor_t::delayed_start);
  40. start_timer(r::pt::minutes(1), *this, &bad_actor_t::delayed_start); // to be cancelled
  41. }
  42. void delayed_start(r::request_id_t, bool) noexcept {
  43. request<payload::sample_req_t>(address).send(r::pt::milliseconds(1));
  44. }
  45. void on_response(message::sample_res_t &msg) noexcept {
  46. ee = msg.payload.ee;
  47. supervisor->do_shutdown();
  48. }
  49. };
  50. struct io_actor1_t : public r::actor_base_t {
  51. using r::actor_base_t::actor_base_t;
  52. r::extended_error_ptr_t ee;
  53. req_ptr_t req;
  54. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  55. r::actor_base_t::configure(plugin);
  56. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) {
  57. p.subscribe_actor(&io_actor1_t::on_request);
  58. p.subscribe_actor(&io_actor1_t::on_response);
  59. });
  60. }
  61. void on_start() noexcept override {
  62. r::actor_base_t::on_start();
  63. request<payload::sample_req_t>(address).send(r::pt::milliseconds(9));
  64. }
  65. void on_request(message::sample_req_t &msg) noexcept {
  66. req = &msg;
  67. start_timer(r::pt::milliseconds(1), *this, &io_actor1_t::on_timeout);
  68. }
  69. void on_timeout(r::request_id_t, bool) noexcept { reply_to(*req); }
  70. void on_response(message::sample_res_t &msg) noexcept {
  71. ee = msg.payload.ee;
  72. supervisor->do_shutdown();
  73. }
  74. };
  75. struct io_actor2_t : public r::actor_base_t {
  76. using r::actor_base_t::actor_base_t;
  77. r::extended_error_ptr_t ee;
  78. r::request_id_t req_id;
  79. std::uint32_t event_id = 0;
  80. std::uint32_t cancel_event;
  81. std::uint32_t timeout_event;
  82. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  83. r::actor_base_t::configure(plugin);
  84. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) {
  85. p.subscribe_actor(&io_actor2_t::on_response);
  86. p.subscribe_actor(&io_actor2_t::on_trigger);
  87. });
  88. }
  89. void on_start() noexcept override {
  90. r::actor_base_t::on_start();
  91. start_timer(r::pt::milliseconds(100), *this, &io_actor2_t::on_timeout);
  92. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  93. send<payload::trigger_t>(address);
  94. }
  95. void on_trigger(message::trigger_t &) noexcept {
  96. req_id = request<payload::sample_req_t>(address).send(r::pt::milliseconds(70));
  97. }
  98. void on_timeout(r::request_id_t, bool) noexcept { cancel_event = ++event_id; }
  99. void on_response(message::sample_res_t &msg) noexcept {
  100. ee = msg.payload.ee;
  101. timeout_event = ++event_id;
  102. supervisor->do_shutdown();
  103. }
  104. };
  105. struct io_actor3_t : public r::actor_base_t {
  106. using r::actor_base_t::actor_base_t;
  107. r::extended_error_ptr_t ee;
  108. r::request_id_t req_id;
  109. std::uint32_t event_id = 0;
  110. std::uint32_t cancel_event;
  111. std::uint32_t timeout_event;
  112. bool cancel_it = false;
  113. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  114. r::actor_base_t::configure(plugin);
  115. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) {
  116. p.subscribe_actor(&io_actor3_t::on_response);
  117. p.subscribe_actor(&io_actor3_t::on_trigger)->tag_io();
  118. });
  119. }
  120. void on_start() noexcept override {
  121. r::actor_base_t::on_start();
  122. start_timer(r::pt::milliseconds(10), *this, &io_actor3_t::on_timeout);
  123. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  124. send<payload::trigger_t>(address);
  125. }
  126. void on_trigger(message::trigger_t &) noexcept {
  127. req_id = request<payload::sample_req_t>(address).send(r::pt::milliseconds(7));
  128. // for coverability
  129. auto id = start_timer(r::pt::milliseconds(10), *this, &io_actor3_t::dummy_timer);
  130. cancel_timer(id);
  131. }
  132. void on_timeout(r::request_id_t, bool) noexcept { cancel_event = ++event_id; }
  133. void dummy_timer(r::request_id_t, bool) noexcept {}
  134. void on_response(message::sample_res_t &msg) noexcept {
  135. ee = msg.payload.ee;
  136. timeout_event = ++event_id;
  137. supervisor->do_shutdown();
  138. }
  139. };
  140. TEST_CASE("timer", "[supervisor][thread]") {
  141. auto system_context = rth::system_context_thread_t();
  142. auto timeout = r::pt::milliseconds{100};
  143. auto sup = system_context.create_supervisor<rth::supervisor_thread_t>().timeout(timeout).finish();
  144. auto actor = sup->create_actor<bad_actor_t>().timeout(timeout).finish();
  145. sup->start();
  146. system_context.run();
  147. REQUIRE(actor->ee);
  148. REQUIRE(actor->ee->ec == r::error_code_t::request_timeout);
  149. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  150. }
  151. TEST_CASE("correct timeout triggering", "[supervisor][thread]") {
  152. auto system_context = rth::system_context_thread_t();
  153. auto timeout = r::pt::milliseconds{10};
  154. auto sup = system_context.create_supervisor<rth::supervisor_thread_t>().timeout(timeout).finish();
  155. auto actor = sup->create_actor<io_actor1_t>().timeout(timeout).finish();
  156. sup->start();
  157. system_context.run();
  158. REQUIRE(!actor->ee);
  159. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  160. }
  161. TEST_CASE("no I/O tag, incorrect timers", "[supervisor][thread]") {
  162. auto system_context = rth::system_context_thread_t();
  163. auto timeout = r::pt::milliseconds{100};
  164. auto sup = system_context.create_supervisor<rth::supervisor_thread_t>().timeout(timeout).finish();
  165. auto actor = sup->create_actor<io_actor2_t>().timeout(timeout).finish();
  166. sup->start();
  167. system_context.run();
  168. REQUIRE(actor->timeout_event == 1);
  169. REQUIRE(actor->cancel_event == 2);
  170. REQUIRE(actor->ee->ec == r::error_code_t::request_timeout);
  171. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  172. }
  173. TEST_CASE("has I/O tag, correct timers", "[supervisor][thread]") {
  174. auto system_context = rth::system_context_thread_t();
  175. auto timeout = r::pt::milliseconds{10};
  176. auto sup = system_context.create_supervisor<rth::supervisor_thread_t>().timeout(timeout).finish();
  177. auto actor = sup->create_actor<io_actor3_t>().timeout(timeout).finish();
  178. sup->start();
  179. system_context.run();
  180. REQUIRE(actor->cancel_event == 1);
  181. REQUIRE(actor->timeout_event == 2);
  182. REQUIRE(actor->ee->ec == r::error_code_t::request_timeout);
  183. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  184. }