132-ev_timer.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <catch2/catch_test_macros.hpp>
  7. #include "rotor.hpp"
  8. #include "rotor/ev.hpp"
  9. #include "access.h"
  10. #include <ev.h>
  11. #include <boost/asio/detail/winsock_init.hpp> // for calling WSAStartup on Windows
  12. namespace r = rotor;
  13. namespace re = rotor::ev;
  14. namespace pt = boost::posix_time;
  15. namespace rt = r::test;
  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. r::extended_error_ptr_t ee;
  24. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  25. r::actor_base_t::configure(plugin);
  26. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&bad_actor_t::on_response); });
  27. }
  28. void on_start() noexcept override {
  29. r::actor_base_t::on_start();
  30. start_timer(r::pt::milliseconds(1), *this, &bad_actor_t::delayed_start);
  31. start_timer(r::pt::minutes(1), *this, &bad_actor_t::delayed_start); // to be cancelled
  32. }
  33. void delayed_start(r::request_id_t, bool) noexcept {
  34. request<traits_t::request::type>(address).send(r::pt::milliseconds(1));
  35. }
  36. void on_response(traits_t::response::message_t &msg) noexcept {
  37. ee = msg.payload.ee;
  38. supervisor->do_shutdown();
  39. }
  40. };
  41. TEST_CASE("timer", "[supervisor][ev]") {
  42. auto *loop = ev_loop_new(0);
  43. auto system_context = r::intrusive_ptr_t<re::system_context_ev_t>{new re::system_context_ev_t()};
  44. auto timeout = r::pt::milliseconds{10};
  45. auto sup = system_context->create_supervisor<re::supervisor_ev_t>()
  46. .loop(loop)
  47. .timeout(timeout)
  48. .loop_ownership(true)
  49. .finish();
  50. auto actor = sup->create_actor<bad_actor_t>().timeout(timeout).finish();
  51. sup->start();
  52. ev_run(loop);
  53. REQUIRE(actor->ee->ec == r::error_code_t::request_timeout);
  54. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  55. }