104-asio_timer.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/asio.hpp"
  8. #include "supervisor_asio_test.h"
  9. #include "access.h"
  10. namespace r = rotor;
  11. namespace ra = rotor::asio;
  12. namespace rt = r::test;
  13. namespace asio = boost::asio;
  14. namespace pt = boost::posix_time;
  15. struct sample_res_t {};
  16. struct sample_req_t {
  17. using response_t = sample_res_t;
  18. };
  19. using traits_t = r::request_traits_t<sample_req_t>;
  20. struct bad_actor_t : public r::actor_base_t {
  21. using r::actor_base_t::actor_base_t;
  22. r::extended_error_ptr_t ee;
  23. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  24. r::actor_base_t::configure(plugin);
  25. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&bad_actor_t::on_response); });
  26. }
  27. void on_start() noexcept override {
  28. r::actor_base_t::on_start();
  29. start_timer(r::pt::milliseconds(1), *this, &bad_actor_t::delayed_start);
  30. start_timer(r::pt::minutes(1), *this, &bad_actor_t::delayed_start); // to be cancelled
  31. }
  32. void delayed_start(r::request_id_t, bool) noexcept {
  33. request<traits_t::request::type>(address).send(r::pt::milliseconds(1));
  34. }
  35. void on_response(traits_t::response::message_t &msg) noexcept {
  36. ee = msg.payload.ee;
  37. supervisor->do_shutdown();
  38. }
  39. };
  40. TEST_CASE("timer", "[supervisor][asio]") {
  41. asio::io_context io_context{1};
  42. auto timeout = r::pt::milliseconds{10};
  43. auto system_context = ra::system_context_asio_t::ptr_t{new ra::system_context_asio_t(io_context)};
  44. auto strand = std::make_shared<asio::io_context::strand>(io_context);
  45. auto sup = system_context->create_supervisor<rt::supervisor_asio_test_t>().strand(strand).timeout(timeout).finish();
  46. auto actor = sup->create_actor<bad_actor_t>().timeout(timeout).finish();
  47. sup->start();
  48. io_context.run();
  49. REQUIRE(actor->ee->ec == r::error_code_t::request_timeout);
  50. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  51. CHECK(rt::empty(sup->get_subscription()));
  52. REQUIRE(sup->get_state() == r::state_t::SHUT_DOWN);
  53. REQUIRE(sup->get_leader_queue().size() == 0);
  54. CHECK(rt::empty(sup->get_subscription()));
  55. }