121-wx_ping_ping.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "rotor.hpp"
  7. #include "rotor/wx.hpp"
  8. #include "supervisor_wx_test.h"
  9. #include <wx/evtloop.h>
  10. #include <wx/apptrait.h>
  11. #include "access.h"
  12. IMPLEMENT_APP_NO_MAIN(rotor::test::RotorApp)
  13. namespace r = rotor;
  14. namespace rx = rotor::wx;
  15. namespace rt = rotor::test;
  16. struct ping_t {};
  17. struct pong_t {};
  18. static std::uint32_t destroyed = 0;
  19. struct pinger_t : public r::actor_base_t {
  20. std::uint32_t ping_sent = 0;
  21. std::uint32_t pong_received = 0;
  22. rotor::address_ptr_t ponger_addr;
  23. using r::actor_base_t::actor_base_t;
  24. ~pinger_t() { destroyed += 1; }
  25. void set_ponger_addr(const rotor::address_ptr_t &addr) { ponger_addr = addr; }
  26. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  27. r::actor_base_t::configure(plugin);
  28. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  29. }
  30. void on_start() noexcept override {
  31. r::actor_base_t::on_start();
  32. send<ping_t>(ponger_addr);
  33. ++ping_sent;
  34. }
  35. void on_pong(rotor::message_t<pong_t> &) noexcept {
  36. ++pong_received;
  37. supervisor->shutdown();
  38. auto loop = wxEventLoopBase::GetActive();
  39. loop->ScheduleExit();
  40. }
  41. };
  42. struct ponger_t : public r::actor_base_t {
  43. std::uint32_t pong_sent = 0;
  44. std::uint32_t ping_received = 0;
  45. rotor::address_ptr_t pinger_addr;
  46. using r::actor_base_t::actor_base_t;
  47. ~ponger_t() { destroyed += 3; }
  48. void set_pinger_addr(const rotor::address_ptr_t &addr) { pinger_addr = addr; }
  49. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  50. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&ponger_t::on_ping); });
  51. }
  52. void on_ping(rotor::message_t<ping_t> &) noexcept {
  53. ++ping_received;
  54. send<pong_t>(pinger_addr);
  55. ++pong_sent;
  56. }
  57. };
  58. TEST_CASE("ping/pong ", "[supervisor][wx]") {
  59. using app_t = rotor::test::RotorApp;
  60. auto app = new app_t();
  61. auto timeout = r::pt::milliseconds{10};
  62. app->CallOnInit();
  63. wxEventLoopBase *loop = app->GetTraits()->CreateEventLoop();
  64. wxEventLoopBase::SetActive(loop);
  65. rx::system_context_ptr_t system_context{new rx::system_context_wx_t(app)};
  66. wxEvtHandler handler;
  67. auto sup =
  68. system_context->create_supervisor<rt::supervisor_wx_test_t>().handler(&handler).timeout(timeout).finish();
  69. sup->start();
  70. auto pinger = sup->create_actor<pinger_t>().timeout(timeout).finish();
  71. auto ponger = sup->create_actor<ponger_t>().timeout(timeout).finish();
  72. pinger->set_ponger_addr(static_cast<r::actor_base_t *>(ponger.get())->get_address());
  73. ponger->set_pinger_addr(static_cast<r::actor_base_t *>(pinger.get())->get_address());
  74. sup->start();
  75. loop->Run();
  76. REQUIRE(pinger->ping_sent == 1);
  77. REQUIRE(pinger->pong_received == 1);
  78. REQUIRE(ponger->pong_sent == 1);
  79. REQUIRE(ponger->ping_received == 1);
  80. pinger.reset();
  81. ponger.reset();
  82. REQUIRE(destroyed == 4);
  83. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  84. REQUIRE(sup->get_leader_queue().size() == 0);
  85. CHECK(rt::empty(sup->get_subscription()));
  86. delete app;
  87. }