131-ev_ping-pong.cpp 6.1 KB

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