141-thread_ping-pong.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // Copyright (c) 2019-2021 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. static std::uint32_t destroyed = 0;
  15. static const void *custom_tag = &custom_tag;
  16. struct supervisor_thread_test_t : public rth::supervisor_thread_t {
  17. using rth::supervisor_thread_t::supervisor_thread_t;
  18. ~supervisor_thread_test_t() {
  19. destroyed += 4;
  20. printf("~supervisor_thread_test_t\n");
  21. }
  22. auto get_leader_queue() {
  23. return static_cast<supervisor_t *>(this)->access<rt::to::locality_leader>()->access<rt::to::queue>();
  24. }
  25. auto &get_subscription() noexcept { return subscription_map; }
  26. void intercept(r::message_ptr_t &message, const void *tag,
  27. const r::continuation_t &continuation) noexcept override {
  28. if (tag == custom_tag) {
  29. intercepted = true;
  30. }
  31. rth::supervisor_thread_t::intercept(message, tag, continuation);
  32. }
  33. bool intercepted = false;
  34. };
  35. struct self_shutdowner_sup_t : public rth::supervisor_thread_t {
  36. using rth::supervisor_thread_t::supervisor_thread_t;
  37. void init_finish() noexcept override {
  38. rth::supervisor_thread_t::init_finish();
  39. printf("triggering shutdown\n");
  40. parent->shutdown();
  41. }
  42. void shutdown_finish() noexcept override {
  43. rth::supervisor_thread_t::shutdown_finish();
  44. printf("shutdown_finish\n");
  45. }
  46. };
  47. struct system_context_thread_test_t : public rth::system_context_thread_t {
  48. r::extended_error_ptr_t ee;
  49. void on_error(r::actor_base_t *, const r::extended_error_ptr_t &err) noexcept override { ee = err; }
  50. };
  51. struct ping_t {};
  52. struct pong_t {};
  53. struct pinger_t : public r::actor_base_t {
  54. std::uint32_t ping_sent = 0;
  55. std::uint32_t pong_received = 0;
  56. rotor::address_ptr_t ponger_addr;
  57. using r::actor_base_t::actor_base_t;
  58. ~pinger_t() { destroyed += 1; }
  59. void set_ponger_addr(const rotor::address_ptr_t &addr) { ponger_addr = addr; }
  60. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  61. r::actor_base_t::configure(plugin);
  62. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  63. }
  64. void on_start() noexcept override {
  65. r::actor_base_t::on_start();
  66. send<ping_t>(ponger_addr);
  67. ++ping_sent;
  68. }
  69. void on_pong(rotor::message_t<pong_t> &) noexcept {
  70. ++pong_received;
  71. supervisor->shutdown();
  72. }
  73. };
  74. struct ponger_t : public r::actor_base_t {
  75. std::uint32_t pong_sent = 0;
  76. std::uint32_t ping_received = 0;
  77. rotor::address_ptr_t pinger_addr;
  78. using r::actor_base_t::actor_base_t;
  79. ~ponger_t() { destroyed += 2; }
  80. void set_pinger_addr(const rotor::address_ptr_t &addr) { pinger_addr = addr; }
  81. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  82. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) {
  83. r::subscription_info_ptr_t info = p.subscribe_actor(&ponger_t::on_ping);
  84. info->tag_io();
  85. info->access<rt::to::tag, const void *>(custom_tag);
  86. });
  87. }
  88. void on_ping(rotor::message_t<ping_t> &) noexcept {
  89. ++ping_received;
  90. send<pong_t>(pinger_addr);
  91. ++pong_sent;
  92. }
  93. };
  94. struct bad_actor_t : public r::actor_base_t {
  95. using r::actor_base_t::actor_base_t;
  96. void on_start() noexcept override { supervisor->shutdown(); }
  97. void shutdown_finish() noexcept override {}
  98. ~bad_actor_t() { printf("~bad_actor_t\n"); }
  99. };
  100. TEST_CASE("ping/pong", "[supervisor][thread]") {
  101. auto system_context = r::intrusive_ptr_t<rth::system_context_thread_t>(new rth::system_context_thread_t());
  102. auto timeout = r::pt::milliseconds{10};
  103. auto sup = system_context->create_supervisor<supervisor_thread_test_t>().timeout(timeout).finish();
  104. auto pinger = sup->create_actor<pinger_t>().timeout(timeout).finish();
  105. auto ponger = sup->create_actor<ponger_t>().timeout(timeout).finish();
  106. pinger->set_ponger_addr(static_cast<r::actor_base_t *>(ponger.get())->get_address());
  107. ponger->set_pinger_addr(static_cast<r::actor_base_t *>(pinger.get())->get_address());
  108. sup->start();
  109. system_context->run();
  110. REQUIRE(pinger->ping_sent == 1);
  111. REQUIRE(pinger->pong_received == 1);
  112. REQUIRE(ponger->pong_sent == 1);
  113. REQUIRE(ponger->ping_received == 1);
  114. pinger.reset();
  115. ponger.reset();
  116. CHECK(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  117. CHECK(sup->get_leader_queue().size() == 0);
  118. CHECK(rt::empty(sup->get_subscription()));
  119. CHECK(sup->intercepted);
  120. sup.reset();
  121. system_context.reset();
  122. REQUIRE(destroyed == 1 + 2 + 4);
  123. }
  124. TEST_CASE("no shutdown confirmation", "[supervisor][thread]") {
  125. auto destroyed_start = destroyed;
  126. auto system_context = r::intrusive_ptr_t<system_context_thread_test_t>(new system_context_thread_test_t());
  127. auto timeout = r::pt::milliseconds{10};
  128. auto sup = system_context->create_supervisor<supervisor_thread_test_t>().timeout(timeout).finish();
  129. sup->start();
  130. sup->create_actor<bad_actor_t>().timeout(timeout).finish();
  131. system_context->run();
  132. REQUIRE(system_context->ee->ec == r::error_code_t::request_timeout);
  133. // act->force_cleanup();
  134. sup->shutdown();
  135. sup.reset();
  136. system_context.reset();
  137. auto destroyed_end = destroyed;
  138. CHECK(destroyed_end == destroyed_start + 4);
  139. }
  140. TEST_CASE("supervisors hierarchy", "[supervisor][thread]") {
  141. auto system_context = r::intrusive_ptr_t<system_context_thread_test_t>(new system_context_thread_test_t());
  142. auto timeout = r::pt::milliseconds{10};
  143. auto sup = system_context->create_supervisor<supervisor_thread_test_t>().timeout(timeout).finish();
  144. auto act = sup->create_actor<self_shutdowner_sup_t>().timeout(timeout).finish();
  145. sup->start();
  146. system_context->run();
  147. CHECK(!system_context->ee);
  148. CHECK(((r::actor_base_t *)act.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  149. CHECK(((r::actor_base_t *)sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  150. }