101-asio_ping-pong-1-strand.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/asio.hpp"
  9. #include "supervisor_asio_test.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 ping_t {};
  16. struct pong_t {};
  17. static std::uint32_t destroyed = 0;
  18. struct pinger_t : public r::actor_base_t {
  19. std::uint32_t ping_sent = 0;
  20. std::uint32_t pong_received = 0;
  21. rotor::address_ptr_t ponger_addr;
  22. using r::actor_base_t::actor_base_t;
  23. ~pinger_t() { destroyed += 1; }
  24. void set_ponger_addr(const rotor::address_ptr_t &addr) { ponger_addr = addr; }
  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(&pinger_t::on_pong); });
  28. }
  29. void on_start() noexcept override {
  30. r::actor_base_t::on_start();
  31. send<ping_t>(ponger_addr);
  32. ++ping_sent;
  33. }
  34. void on_pong(rotor::message_t<pong_t> &) noexcept {
  35. ++pong_received;
  36. supervisor->shutdown();
  37. }
  38. };
  39. struct ponger_t : public r::actor_base_t {
  40. std::uint32_t pong_sent = 0;
  41. std::uint32_t ping_received = 0;
  42. rotor::address_ptr_t pinger_addr;
  43. using r::actor_base_t::actor_base_t;
  44. ~ponger_t() { destroyed += 3; }
  45. void set_pinger_addr(const rotor::address_ptr_t &addr) { pinger_addr = addr; }
  46. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  47. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&ponger_t::on_ping); });
  48. }
  49. void on_ping(rotor::message_t<ping_t> &) noexcept {
  50. ++ping_received;
  51. send<pong_t>(pinger_addr);
  52. ++pong_sent;
  53. }
  54. };
  55. TEST_CASE("ping/pong ", "[supervisor][asio]") {
  56. asio::io_context io_context{1};
  57. auto system_context = ra::system_context_asio_t::ptr_t{new ra::system_context_asio_t(io_context)};
  58. auto strand = std::make_shared<asio::io_context::strand>(io_context);
  59. auto timeout = r::pt::milliseconds{10};
  60. auto sup = system_context->create_supervisor<rt::supervisor_asio_test_t>().timeout(timeout).strand(strand).finish();
  61. auto pinger = sup->create_actor<pinger_t>().timeout(timeout).finish();
  62. auto ponger = sup->create_actor<ponger_t>().timeout(timeout).finish();
  63. pinger->set_ponger_addr(static_cast<r::actor_base_t *>(ponger.get())->get_address());
  64. ponger->set_pinger_addr(static_cast<r::actor_base_t *>(pinger.get())->get_address());
  65. sup->start();
  66. io_context.run();
  67. REQUIRE(pinger->ping_sent == 1);
  68. REQUIRE(pinger->pong_received == 1);
  69. REQUIRE(ponger->pong_sent == 1);
  70. REQUIRE(ponger->ping_received == 1);
  71. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  72. REQUIRE(sup->get_leader_queue().size() == 0);
  73. CHECK(rt::empty(sup->get_subscription()));
  74. pinger.reset();
  75. ponger.reset();
  76. io_context.run();
  77. CHECK(sup->get_timers_map().size() == 0);
  78. CHECK(destroyed == 4);
  79. }