131-ev_ping-pong.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/ev.hpp"
  9. #include "access.h"
  10. #include <ev.h>
  11. #include <boost/asio/detail/winsock_init.hpp> // for calling WSAStartup on Windows
  12. namespace r = rotor;
  13. namespace re = rotor::ev;
  14. namespace rt = r::test;
  15. static std::uint32_t destroyed = 0;
  16. struct supervisor_ev_test_t : public re::supervisor_ev_t {
  17. using re::supervisor_ev_t::supervisor_ev_t;
  18. ~supervisor_ev_test_t() {
  19. destroyed += 4;
  20. printf("~supervisor_ev_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. };
  27. struct self_shutdowner_sup_t : public re::supervisor_ev_t {
  28. using re::supervisor_ev_t::supervisor_ev_t;
  29. void init_finish() noexcept override {
  30. re::supervisor_ev_t::init_finish();
  31. printf("triggering shutdown\n");
  32. parent->shutdown();
  33. }
  34. void shutdown_finish() noexcept override {
  35. re::supervisor_ev_t::shutdown_finish();
  36. printf("shutdown_finish\n");
  37. }
  38. };
  39. struct system_context_ev_test_t : public re::system_context_ev_t {
  40. r::extended_error_ptr_t ee;
  41. void on_error(r::actor_base_t *, const r::extended_error_ptr_t &err) noexcept override {
  42. ee = err;
  43. auto loop = static_cast<re::supervisor_ev_t *>(get_supervisor().get())->get_loop();
  44. ev_break(loop);
  45. }
  46. };
  47. struct ping_t {};
  48. struct pong_t {};
  49. struct pinger_t : public r::actor_base_t {
  50. std::uint32_t ping_sent = 0;
  51. std::uint32_t pong_received = 0;
  52. rotor::address_ptr_t ponger_addr;
  53. using r::actor_base_t::actor_base_t;
  54. ~pinger_t() { destroyed += 1; }
  55. void set_ponger_addr(const rotor::address_ptr_t &addr) { ponger_addr = addr; }
  56. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  57. r::actor_base_t::configure(plugin);
  58. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  59. }
  60. void on_start() noexcept override {
  61. r::actor_base_t::on_start();
  62. send<ping_t>(ponger_addr);
  63. ++ping_sent;
  64. }
  65. void on_pong(rotor::message_t<pong_t> &) noexcept {
  66. ++pong_received;
  67. supervisor->shutdown();
  68. }
  69. };
  70. struct ponger_t : public r::actor_base_t {
  71. std::uint32_t pong_sent = 0;
  72. std::uint32_t ping_received = 0;
  73. rotor::address_ptr_t pinger_addr;
  74. using r::actor_base_t::actor_base_t;
  75. ~ponger_t() { destroyed += 2; }
  76. void set_pinger_addr(const rotor::address_ptr_t &addr) { pinger_addr = addr; }
  77. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  78. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&ponger_t::on_ping); });
  79. }
  80. void on_ping(rotor::message_t<ping_t> &) noexcept {
  81. ++ping_received;
  82. send<pong_t>(pinger_addr);
  83. ++pong_sent;
  84. }
  85. };
  86. struct bad_actor_t : public r::actor_base_t {
  87. using r::actor_base_t::actor_base_t;
  88. void on_start() noexcept override { supervisor->shutdown(); }
  89. void shutdown_finish() noexcept override {}
  90. ~bad_actor_t() { printf("~bad_actor_t\n"); }
  91. };
  92. TEST_CASE("ping/pong", "[supervisor][ev]") {
  93. auto *loop = ev_loop_new(0);
  94. auto system_context = re::system_context_ptr_t{new re::system_context_ev_t()};
  95. auto timeout = r::pt::milliseconds{10};
  96. auto sup = system_context->create_supervisor<supervisor_ev_test_t>()
  97. .loop(loop)
  98. .loop_ownership(true)
  99. .timeout(timeout)
  100. .finish();
  101. auto pinger = sup->create_actor<pinger_t>().timeout(timeout).finish();
  102. auto ponger = sup->create_actor<ponger_t>().timeout(timeout).finish();
  103. pinger->set_ponger_addr(static_cast<r::actor_base_t *>(ponger.get())->get_address());
  104. ponger->set_pinger_addr(static_cast<r::actor_base_t *>(pinger.get())->get_address());
  105. sup->start();
  106. ev_run(loop);
  107. REQUIRE(pinger->ping_sent == 1);
  108. REQUIRE(pinger->pong_received == 1);
  109. REQUIRE(ponger->pong_sent == 1);
  110. REQUIRE(ponger->ping_received == 1);
  111. pinger.reset();
  112. ponger.reset();
  113. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  114. REQUIRE(sup->get_leader_queue().size() == 0);
  115. CHECK(rt::empty(sup->get_subscription()));
  116. sup.reset();
  117. system_context.reset();
  118. REQUIRE(destroyed == 1 + 2 + 4);
  119. }
  120. TEST_CASE("no shutdown confirmation", "[supervisor][ev]") {
  121. auto destroyed_start = destroyed;
  122. auto *loop = ev_loop_new(0);
  123. auto system_context = r::intrusive_ptr_t<system_context_ev_test_t>{new system_context_ev_test_t()};
  124. auto timeout = r::pt::milliseconds{10};
  125. auto sup = system_context->create_supervisor<supervisor_ev_test_t>()
  126. .loop(loop)
  127. .loop_ownership(true)
  128. .timeout(timeout)
  129. .finish();
  130. sup->start();
  131. sup->create_actor<bad_actor_t>().timeout(timeout).finish();
  132. ev_run(loop);
  133. REQUIRE(system_context->ee->ec == r::error_code_t::request_timeout);
  134. // act->force_cleanup();
  135. sup->shutdown();
  136. ev_run(loop);
  137. sup.reset();
  138. system_context.reset();
  139. auto destroyed_end = destroyed;
  140. CHECK(destroyed_end == destroyed_start + 4);
  141. }
  142. TEST_CASE("supervisors hierarchy", "[supervisor][ev]") {
  143. auto *loop = ev_loop_new(0);
  144. auto system_context = r::intrusive_ptr_t<system_context_ev_test_t>{new system_context_ev_test_t()};
  145. auto timeout = r::pt::milliseconds{10};
  146. auto sup = system_context->create_supervisor<supervisor_ev_test_t>()
  147. .loop(loop)
  148. .loop_ownership(true)
  149. .timeout(timeout)
  150. .finish();
  151. auto act = sup->create_actor<self_shutdowner_sup_t>().loop(loop).loop_ownership(false).timeout(timeout).finish();
  152. sup->start();
  153. ev_run(loop);
  154. CHECK(!system_context->ee);
  155. CHECK(((r::actor_base_t *)act.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  156. CHECK(((r::actor_base_t *)sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  157. }