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

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