151-fltk-ping-pong.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // Copyright (c) 2019-2024 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/fltk.hpp>
  8. #include <FL/Fl.H>
  9. #include "access.h"
  10. namespace r = rotor;
  11. namespace rf = rotor::fltk;
  12. namespace rt = r::test;
  13. static std::uint32_t destroyed = 0;
  14. struct supervisor_fltk_test_t : public rf::supervisor_fltk_t {
  15. using rf::supervisor_fltk_t::supervisor_fltk_t;
  16. ~supervisor_fltk_test_t() {
  17. destroyed += 4;
  18. printf("~supervisor_fltk_test_t\n");
  19. }
  20. auto get_leader_queue() {
  21. return static_cast<supervisor_t *>(this)->access<rt::to::locality_leader>()->access<rt::to::queue>();
  22. }
  23. auto &get_subscription() noexcept { return subscription_map; }
  24. };
  25. struct self_shutdowner_sup_t : public rf::supervisor_fltk_t {
  26. using parent_t = rf::supervisor_fltk_t;
  27. using parent_t::parent_t;
  28. void init_finish() noexcept override {
  29. parent_t::init_finish();
  30. printf("triggering shutdown\n");
  31. parent->shutdown();
  32. }
  33. void shutdown_finish() noexcept override {
  34. parent_t::shutdown_finish();
  35. printf("shutdown_finish\n");
  36. }
  37. };
  38. struct system_context_fltk_test_t : public rf::system_context_fltk_t {
  39. r::extended_error_ptr_t ee;
  40. void on_error(r::actor_base_t *, const r::extended_error_ptr_t &err) noexcept override { ee = err; }
  41. };
  42. struct ping_t {};
  43. struct pong_t {};
  44. struct pinger_t : public r::actor_base_t {
  45. std::uint32_t ping_sent = 0;
  46. std::uint32_t pong_received = 0;
  47. rotor::address_ptr_t ponger_addr;
  48. using r::actor_base_t::actor_base_t;
  49. ~pinger_t() { destroyed += 1; }
  50. void set_ponger_addr(const rotor::address_ptr_t &addr) { ponger_addr = addr; }
  51. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  52. r::actor_base_t::configure(plugin);
  53. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  54. }
  55. void on_start() noexcept override {
  56. r::actor_base_t::on_start();
  57. send<ping_t>(ponger_addr);
  58. ++ping_sent;
  59. printf("pinger_t::on_start\n");
  60. }
  61. void on_pong(rotor::message_t<pong_t> &) noexcept {
  62. printf("on pong\n");
  63. ++pong_received;
  64. // supervisor->do_shutdown();
  65. supervisor->shutdown();
  66. }
  67. };
  68. struct ponger_t : public r::actor_base_t {
  69. std::uint32_t pong_sent = 0;
  70. std::uint32_t ping_received = 0;
  71. rotor::address_ptr_t pinger_addr;
  72. using r::actor_base_t::actor_base_t;
  73. ~ponger_t() { destroyed += 2; }
  74. void set_pinger_addr(const rotor::address_ptr_t &addr) { pinger_addr = addr; }
  75. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  76. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&ponger_t::on_ping); });
  77. }
  78. void on_ping(rotor::message_t<ping_t> &) noexcept {
  79. ++ping_received;
  80. send<pong_t>(pinger_addr);
  81. ++pong_sent;
  82. printf("on ping\n");
  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][fltk]") {
  92. Fl::lock();
  93. auto system_context = rf::system_context_ptr_t(new rf::system_context_fltk_t());
  94. auto timeout = r::pt::milliseconds{100};
  95. auto sup = system_context->create_supervisor<supervisor_fltk_test_t>().timeout(timeout).finish();
  96. sup->do_process();
  97. auto pinger = sup->create_actor<pinger_t>().timeout(timeout).finish();
  98. auto ponger = sup->create_actor<ponger_t>().timeout(timeout).finish();
  99. pinger->set_ponger_addr(static_cast<r::actor_base_t *>(ponger.get())->get_address());
  100. ponger->set_pinger_addr(static_cast<r::actor_base_t *>(pinger.get())->get_address());
  101. sup->start();
  102. while (((r::actor_base_t *)sup.get())->access<rt::to::state>() != r::state_t::SHUT_DOWN) {
  103. sup->do_process();
  104. Fl::wait(0.1);
  105. }
  106. CHECK(pinger->ping_sent == 1);
  107. CHECK(pinger->pong_received == 1);
  108. CHECK(ponger->pong_sent == 1);
  109. CHECK(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. Fl::unlock();
  119. }
  120. TEST_CASE("supervisors hierarchy", "[supervisor][fltk]") {
  121. Fl::lock();
  122. auto system_context = system_context_fltk_test_t();
  123. auto timeout = r::pt::milliseconds{100};
  124. auto sup = system_context.create_supervisor<supervisor_fltk_test_t>().timeout(timeout).finish();
  125. auto act = sup->create_actor<self_shutdowner_sup_t>().timeout(timeout).finish();
  126. sup->start();
  127. while (((r::actor_base_t *)sup.get())->access<rt::to::state>() != r::state_t::SHUT_DOWN) {
  128. sup->do_process();
  129. Fl::wait(0.1);
  130. }
  131. CHECK(!sup->get_shutdown_reason()->root()->ec);
  132. CHECK(!system_context.ee);
  133. CHECK(((r::actor_base_t *)act.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  134. CHECK(((r::actor_base_t *)sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  135. Fl::unlock();
  136. }
  137. TEST_CASE("no shutdown confirmation", "[supervisor][fltk]") {
  138. Fl::lock();
  139. auto destroyed_start = destroyed;
  140. auto system_context = new system_context_fltk_test_t();
  141. auto timeout = r::pt::milliseconds{10};
  142. auto sup = system_context->create_supervisor<supervisor_fltk_test_t>().timeout(timeout).finish();
  143. sup->start();
  144. sup->create_actor<bad_actor_t>().timeout(timeout).finish();
  145. while (((r::actor_base_t *)sup.get())->access<rt::to::state>() != r::state_t::SHUT_DOWN) {
  146. sup->do_process();
  147. Fl::wait(0.1);
  148. }
  149. REQUIRE(system_context->ee);
  150. REQUIRE(system_context->ee->ec == r::error_code_t::request_timeout);
  151. sup->shutdown();
  152. Fl::wait(0);
  153. sup.reset();
  154. delete system_context;
  155. auto destroyed_end = destroyed;
  156. CHECK(destroyed_end == destroyed_start + 4);
  157. Fl::unlock();
  158. }