132-ev_timer.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 "catch.hpp"
  7. #include "rotor.hpp"
  8. #include "rotor/ev.hpp"
  9. #include <ev.h>
  10. #include "access.h"
  11. namespace r = rotor;
  12. namespace re = rotor::ev;
  13. namespace pt = boost::posix_time;
  14. namespace rt = r::test;
  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. std::error_code ec;
  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. }
  31. void delayed_start(r::request_id_t, bool) noexcept {
  32. request<traits_t::request::type>(address).send(r::pt::milliseconds(1));
  33. }
  34. void on_response(traits_t::response::message_t &msg) noexcept {
  35. ec = msg.payload.ec;
  36. supervisor->do_shutdown();
  37. }
  38. };
  39. TEST_CASE("timer", "[supervisor][ev]") {
  40. auto *loop = ev_loop_new(0);
  41. auto system_context = r::intrusive_ptr_t<re::system_context_ev_t>{new re::system_context_ev_t()};
  42. auto timeout = r::pt::milliseconds{10};
  43. auto sup = system_context->create_supervisor<re::supervisor_ev_t>()
  44. .loop(loop)
  45. .timeout(timeout)
  46. .loop_ownership(true)
  47. .finish();
  48. auto actor = sup->create_actor<bad_actor_t>().timeout(timeout).finish();
  49. sup->start();
  50. ev_run(loop);
  51. REQUIRE(actor->ec == r::error_code_t::request_timeout);
  52. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  53. }