104-asio_timer.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Copyright (c) 2019 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. #include "catch.hpp"
  7. #include "rotor.hpp"
  8. #include "rotor/asio.hpp"
  9. #include "supervisor_asio_test.h"
  10. #include <iostream>
  11. namespace r = rotor;
  12. namespace ra = rotor::asio;
  13. namespace rt = r::test;
  14. namespace asio = boost::asio;
  15. namespace pt = boost::posix_time;
  16. struct sample_res_t {};
  17. struct sample_req_t {
  18. using response_t = sample_res_t;
  19. };
  20. using traits_t = r::request_traits_t<sample_req_t>;
  21. struct bad_actor_t : public r::actor_base_t {
  22. using r::actor_base_t::actor_base_t;
  23. std::error_code ec;
  24. void init_start() noexcept override {
  25. subscribe(&bad_actor_t::on_response);
  26. r::actor_base_t::init_start();
  27. }
  28. void on_start(r::message_t<r::payload::start_actor_t> &msg) noexcept override {
  29. r::actor_base_t::on_start(msg);
  30. request<traits_t::request::type>(address).send(r::pt::milliseconds(1));
  31. }
  32. void on_response(traits_t::response::message_t &msg) noexcept {
  33. ec = msg.payload.ec;
  34. supervisor.do_shutdown();
  35. }
  36. };
  37. TEST_CASE("timer", "[supervisor][asio]") {
  38. asio::io_context io_context{1};
  39. auto timeout = r::pt::milliseconds{10};
  40. auto system_context = ra::system_context_asio_t::ptr_t{new ra::system_context_asio_t(io_context)};
  41. auto stand = std::make_shared<asio::io_context::strand>(io_context);
  42. ra::supervisor_config_asio_t conf{timeout, std::move(stand)};
  43. auto sup = system_context->create_supervisor<rt::supervisor_asio_test_t>(conf);
  44. auto actor = sup->create_actor<bad_actor_t>(timeout);
  45. sup->start();
  46. io_context.run();
  47. REQUIRE(actor->ec == r::error_code_t::request_timeout);
  48. REQUIRE(sup->get_state() == r::state_t::SHUTTED_DOWN);
  49. REQUIRE(sup->get_leader_queue().size() == 0);
  50. REQUIRE(sup->get_points().size() == 0);
  51. REQUIRE(sup->get_subscription().size() == 0);
  52. }