122-wx_timer.cpp 2.7 KB

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