122-wx_timer.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.hpp"
  8. #include "rotor/wx.hpp"
  9. #include "supervisor_wx_test.h"
  10. #include <wx/evtloop.h>
  11. #include <wx/apptrait.h>
  12. #include "access.h"
  13. IMPLEMENT_APP_NO_MAIN(rotor::test::RotorApp)
  14. namespace r = rotor;
  15. namespace rx = rotor::wx;
  16. namespace rt = r::test;
  17. namespace pt = boost::posix_time;
  18. struct sample_res_t {};
  19. struct sample_req_t {
  20. using response_t = sample_res_t;
  21. };
  22. using traits_t = r::request_traits_t<sample_req_t>;
  23. struct bad_actor_t : public r::actor_base_t {
  24. using r::actor_base_t::actor_base_t;
  25. r::extended_error_ptr_t ee;
  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(&bad_actor_t::on_response); });
  29. }
  30. void on_start() noexcept override {
  31. r::actor_base_t::on_start();
  32. start_timer(r::pt::milliseconds(1), *this, &bad_actor_t::delayed_start);
  33. start_timer(r::pt::minutes(1), *this, &bad_actor_t::delayed_start); // to be cancelled
  34. }
  35. void delayed_start(r::request_id_t, bool) noexcept {
  36. request<traits_t::request::type>(address).send(r::pt::milliseconds(1));
  37. }
  38. void on_response(traits_t::response::message_t &msg) noexcept {
  39. ee = msg.payload.ee;
  40. // alternative for supervisor.do_shutdown() for better coverage
  41. auto sup_addr = static_cast<r::actor_base_t *>(supervisor)->get_address();
  42. auto shutdown_trigger = r::make_message<r::payload::shutdown_trigger_t>(sup_addr, sup_addr, ee);
  43. supervisor->enqueue(shutdown_trigger);
  44. auto loop = wxEventLoopBase::GetActive();
  45. loop->ScheduleExit();
  46. }
  47. };
  48. TEST_CASE("ping/pong ", "[supervisor][wx]") {
  49. using app_t = rotor::test::RotorApp;
  50. auto app = new app_t();
  51. auto timeout = r::pt::milliseconds{10};
  52. app->CallOnInit();
  53. wxEventLoopBase *loop = app->GetTraits()->CreateEventLoop();
  54. wxEventLoopBase::SetActive(loop);
  55. rx::system_context_ptr_t system_context{new rx::system_context_wx_t(app)};
  56. wxEvtHandler handler;
  57. auto sup =
  58. system_context->create_supervisor<rt::supervisor_wx_test_t>().handler(&handler).timeout(timeout).finish();
  59. sup->start();
  60. auto actor = sup->create_actor<bad_actor_t>().timeout(timeout).finish();
  61. sup->start();
  62. loop->Run();
  63. REQUIRE(actor->ee->ec == r::error_code_t::request_timeout);
  64. REQUIRE(sup->get_state() == r::state_t::SHUT_DOWN);
  65. REQUIRE(sup->get_leader_queue().size() == 0);
  66. CHECK(rt::empty(sup->get_subscription()));
  67. actor.reset();
  68. sup.reset();
  69. delete app;
  70. }