102-asio_ping-pong-2-strands.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "actor_test.h"
  10. #include "access.h"
  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 ping_t {};
  17. struct pong_t {};
  18. struct pinger_t : public rt::actor_test_t {
  19. std::uint32_t ping_sent = 0;
  20. std::uint32_t pong_received = 0;
  21. using rt::actor_test_t::actor_test_t;
  22. void set_ponger_addr(const r::address_ptr_t &addr) { ponger_addr = addr; }
  23. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  24. plugin.with_casted<r::plugin::starter_plugin_t>([&](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  25. plugin.with_casted<r::plugin::link_client_plugin_t>(
  26. [&](auto &p) { p.link(ponger_addr, true, [&](auto &ec) mutable { REQUIRE(!ec); }); });
  27. }
  28. void on_start() noexcept override {
  29. r::actor_base_t::on_start();
  30. do_send_ping();
  31. }
  32. void on_pong(r::message_t<pong_t> &) noexcept {
  33. ++pong_received;
  34. do_shutdown();
  35. }
  36. void do_send_ping() {
  37. ++ping_sent;
  38. send<ping_t>(ponger_addr);
  39. }
  40. r::address_ptr_t ponger_addr;
  41. };
  42. struct ponger_t : public rt::actor_test_t {
  43. std::uint32_t ping_received = 0;
  44. std::uint32_t pong_sent = 0;
  45. using rt::actor_test_t::actor_test_t;
  46. void set_pinger_addr(const r::address_ptr_t &addr) { pinger_addr = addr; }
  47. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  48. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&ponger_t::on_ping); });
  49. }
  50. void on_ping(rotor::message_t<ping_t> &) noexcept {
  51. ++ping_received;
  52. send<pong_t>(pinger_addr);
  53. ++pong_sent;
  54. }
  55. private:
  56. r::address_ptr_t pinger_addr;
  57. };
  58. TEST_CASE("ping/pong 2 sups", "[supervisor][asio]") {
  59. asio::io_context io_context{1};
  60. auto system_context = ra::system_context_asio_t::ptr_t{new ra::system_context_asio_t(io_context)};
  61. auto strand1 = std::make_shared<asio::io_context::strand>(io_context);
  62. auto strand2 = std::make_shared<asio::io_context::strand>(io_context);
  63. auto timeout = r::pt::milliseconds{50};
  64. auto sup1 =
  65. system_context->create_supervisor<rt::supervisor_asio_test_t>().strand(strand1).timeout(timeout).finish();
  66. auto sup2 = sup1->create_actor<rt::supervisor_asio_test_t>().strand(strand2).timeout(timeout).finish();
  67. auto pinger = sup1->create_actor<pinger_t>().timeout(timeout).autoshutdown_supervisor().finish();
  68. auto ponger = sup2->create_actor<ponger_t>().timeout(timeout).finish();
  69. pinger->set_ponger_addr(static_cast<r::actor_base_t *>(ponger.get())->get_address());
  70. ponger->set_pinger_addr(static_cast<r::actor_base_t *>(pinger.get())->get_address());
  71. sup1->start();
  72. io_context.run();
  73. REQUIRE(pinger->ping_sent == 1);
  74. REQUIRE(pinger->pong_received == 1);
  75. REQUIRE(ponger->ping_received == 1);
  76. REQUIRE(ponger->pong_sent == 1);
  77. REQUIRE(static_cast<r::actor_base_t *>(sup1.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  78. REQUIRE(sup1->get_leader_queue().size() == 0);
  79. CHECK(rt::empty(sup1->get_subscription()));
  80. REQUIRE(static_cast<r::actor_base_t *>(sup2.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  81. REQUIRE(sup2->get_leader_queue().size() == 0);
  82. CHECK(rt::empty(sup2->get_subscription()));
  83. REQUIRE(pinger->get_state() == r::state_t::SHUT_DOWN);
  84. REQUIRE(ponger->get_state() == r::state_t::SHUT_DOWN);
  85. }