011-ping_pong.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "rotor.hpp"
  7. #include "access.h"
  8. #include "supervisor_test.h"
  9. namespace r = rotor;
  10. namespace rt = r::test;
  11. struct ping_t {};
  12. struct pong_t {};
  13. static std::uint32_t destroyed = 0;
  14. struct pinger_t : public r::actor_base_t {
  15. std::uint32_t ping_sent = 0;
  16. std::uint32_t pong_received = 0;
  17. using r::actor_base_t::actor_base_t;
  18. ~pinger_t() override { destroyed += 1; }
  19. void set_ponger_addr(const r::address_ptr_t &addr) { ponger_addr = addr; }
  20. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  21. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  22. }
  23. void on_start() noexcept override {
  24. r::actor_base_t::on_start();
  25. ++ping_sent;
  26. send<ping_t>(ponger_addr);
  27. }
  28. void on_pong(r::message_t<pong_t> &) noexcept { ++pong_received; }
  29. r::address_ptr_t ponger_addr;
  30. };
  31. struct ponger_t : public r::actor_base_t {
  32. std::uint32_t ping_received = 0;
  33. std::uint32_t pong_sent = 0;
  34. using r::actor_base_t::actor_base_t;
  35. ~ponger_t() override { destroyed += 3; }
  36. void set_pinger_addr(const r::address_ptr_t &addr) { pinger_addr = addr; }
  37. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  38. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&ponger_t::on_ping); });
  39. }
  40. void on_ping(r::message_t<ping_t> &) noexcept {
  41. ++ping_received;
  42. send<pong_t>(pinger_addr);
  43. ++pong_sent;
  44. }
  45. private:
  46. r::address_ptr_t pinger_addr;
  47. };
  48. TEST_CASE("ping-pong", "[supervisor]") {
  49. r::system_context_t system_context;
  50. auto sup = system_context.create_supervisor<rt::supervisor_test_t>().timeout(rt::default_timeout).finish();
  51. auto pinger = sup->create_actor<pinger_t>().timeout(rt::default_timeout).finish();
  52. auto ponger = sup->create_actor<ponger_t>().timeout(rt::default_timeout).finish();
  53. pinger->set_ponger_addr(ponger->get_address());
  54. ponger->set_pinger_addr(pinger->get_address());
  55. sup->do_process();
  56. REQUIRE(pinger->ping_sent == 1);
  57. REQUIRE(pinger->pong_received == 1);
  58. REQUIRE(ponger->pong_sent == 1);
  59. REQUIRE(ponger->ping_received == 1);
  60. sup->do_shutdown();
  61. sup->do_process();
  62. REQUIRE(sup->get_state() == r::state_t::SHUT_DOWN);
  63. REQUIRE(sup->get_leader_queue().size() == 0);
  64. REQUIRE(sup->get_points().size() == 0);
  65. CHECK(rt::empty(sup->get_subscription()));
  66. pinger.reset();
  67. ponger.reset();
  68. REQUIRE(destroyed == 4);
  69. }