031-next-address.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Copyright (c) 2019-2024 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. #include "access.h"
  7. #include "rotor.hpp"
  8. #include "supervisor_test.h"
  9. namespace r = rotor;
  10. namespace rt = r::test;
  11. namespace payload {
  12. struct sample_payload_t {};
  13. } // namespace payload
  14. namespace message {
  15. using sample_message_t = r::message_t<payload::sample_payload_t>;
  16. }
  17. TEST_CASE("delivery to unknown addr", "[message]") {
  18. struct actor_t : public r::actor_base_t {
  19. using parent_t = r::actor_base_t;
  20. using parent_t::parent_t;
  21. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  22. r::actor_base_t::configure(plugin);
  23. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&actor_t::on_destroy); });
  24. }
  25. void on_destroy(message::sample_message_t &) noexcept { ++samples_received; }
  26. int samples_received = 0;
  27. };
  28. r::system_context_t system_context;
  29. rt::supervisor_test_ptr_t sup;
  30. sup = system_context.create_supervisor<rt::supervisor_test_t>().timeout(rt::default_timeout).finish();
  31. auto act = sup->create_actor<actor_t>().timeout(rt::default_timeout).finish();
  32. sup->do_process();
  33. REQUIRE(sup->get_state() == r::state_t::OPERATIONAL);
  34. REQUIRE(act->samples_received == 0);
  35. auto fake_addr = sup->make_address();
  36. auto msg = r::make_routed_message<payload::sample_payload_t>(fake_addr, act->get_address());
  37. sup->put(std::move(msg));
  38. sup->do_process();
  39. REQUIRE(act->samples_received == 1);
  40. }
  41. TEST_CASE("delivery to local address, then route to destroy address", "[message]") {
  42. struct actor_t : public r::actor_base_t {
  43. using parent_t = r::actor_base_t;
  44. using parent_t::parent_t;
  45. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  46. r::actor_base_t::configure(plugin);
  47. plugin.with_casted<r::plugin::address_maker_plugin_t>([&](auto &p) { destroy_addr = p.create_address(); });
  48. plugin.with_casted<r::plugin::starter_plugin_t>([this](auto &p) {
  49. p.subscribe_actor(&actor_t::on_payoad);
  50. p.subscribe_actor(&actor_t::on_destroy, destroy_addr);
  51. });
  52. }
  53. void on_payoad(message::sample_message_t &) noexcept { samples_received += 2; }
  54. void on_destroy(message::sample_message_t &) noexcept { samples_received *= 10; }
  55. int samples_received = 0;
  56. r::address_ptr_t destroy_addr;
  57. };
  58. r::system_context_t system_context;
  59. rt::supervisor_test_ptr_t sup;
  60. sup = system_context.create_supervisor<rt::supervisor_test_t>().timeout(rt::default_timeout).finish();
  61. auto act = sup->create_actor<actor_t>().timeout(rt::default_timeout).finish();
  62. sup->do_process();
  63. REQUIRE(sup->get_state() == r::state_t::OPERATIONAL);
  64. REQUIRE(act->samples_received == 0);
  65. auto msg = r::make_routed_message<payload::sample_payload_t>(act->get_address(), act->destroy_addr);
  66. sup->put(std::move(msg));
  67. sup->do_process();
  68. REQUIRE(act->samples_received == 20);
  69. }
  70. TEST_CASE("delivery to external address, then route to destroy address", "[message]") {
  71. static int samples_received = 0;
  72. struct target_t : public r::actor_base_t {
  73. using parent_t = r::actor_base_t;
  74. using parent_t::parent_t;
  75. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  76. r::actor_base_t::configure(plugin);
  77. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&target_t::on_payoad); });
  78. }
  79. void on_payoad(message::sample_message_t &) noexcept { samples_received += 2; }
  80. };
  81. struct sink_t : public r::actor_base_t {
  82. using parent_t = r::actor_base_t;
  83. using parent_t::parent_t;
  84. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  85. r::actor_base_t::configure(plugin);
  86. plugin.with_casted<r::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&sink_t::on_destroy); });
  87. }
  88. void on_destroy(message::sample_message_t &) noexcept { samples_received *= 10; }
  89. };
  90. r::system_context_t system_context;
  91. const char locality1[] = "l1";
  92. const char locality2[] = "l2";
  93. auto sup1 = system_context.create_supervisor<rt::supervisor_test_t>()
  94. .locality(locality1)
  95. .timeout(rt::default_timeout)
  96. .finish();
  97. auto sup2 = sup1->create_actor<rt::supervisor_test_t>().locality(locality2).timeout(rt::default_timeout).finish();
  98. auto sink = sup1->create_actor<sink_t>().timeout(rt::default_timeout).finish();
  99. auto target = sup2->create_actor<target_t>().timeout(rt::default_timeout).finish();
  100. while (!sup1->get_leader_queue().empty() || !sup2->get_leader_queue().empty()) {
  101. sup1->do_process();
  102. sup2->do_process();
  103. }
  104. REQUIRE(sup1->get_state() == r::state_t::OPERATIONAL);
  105. REQUIRE(sup2->get_state() == r::state_t::OPERATIONAL);
  106. REQUIRE(samples_received == 0);
  107. auto msg = r::make_routed_message<payload::sample_payload_t>(target->get_address(), sink->get_address());
  108. sup1->put(std::move(msg));
  109. while (!sup1->get_leader_queue().empty() || !sup2->get_leader_queue().empty()) {
  110. sup1->do_process();
  111. sup2->do_process();
  112. }
  113. REQUIRE(samples_received == 20);
  114. }
  115. TEST_CASE("delivery to external subscriber, then route to destroy address", "[message]") {
  116. static int samples_received = 0;
  117. struct target_t : public r::actor_base_t {
  118. using parent_t = r::actor_base_t;
  119. using parent_t::parent_t;
  120. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  121. r::actor_base_t::configure(plugin);
  122. plugin.with_casted<r::plugin::starter_plugin_t>(
  123. [this](auto &p) { p.subscribe_actor(&target_t::on_payoad, sink_addr); });
  124. }
  125. void on_payoad(message::sample_message_t &) noexcept { samples_received += 2; }
  126. r::address_ptr_t sink_addr;
  127. };
  128. struct sink_t : public r::actor_base_t {
  129. using parent_t = r::actor_base_t;
  130. using parent_t::parent_t;
  131. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  132. r::actor_base_t::configure(plugin);
  133. plugin.with_casted<r::plugin::address_maker_plugin_t>([&](auto &p) { destroy_addr = p.create_address(); });
  134. plugin.with_casted<r::plugin::starter_plugin_t>(
  135. [this](auto &p) { p.subscribe_actor(&sink_t::on_destroy, destroy_addr); });
  136. }
  137. void on_destroy(message::sample_message_t &) noexcept { samples_received *= 10; }
  138. r::address_ptr_t destroy_addr;
  139. };
  140. r::system_context_t system_context;
  141. const char locality1[] = "l1";
  142. const char locality2[] = "l2";
  143. auto sup1 = system_context.create_supervisor<rt::supervisor_test_t>()
  144. .locality(locality1)
  145. .timeout(rt::default_timeout)
  146. .finish();
  147. auto sup2 = sup1->create_actor<rt::supervisor_test_t>().locality(locality2).timeout(rt::default_timeout).finish();
  148. auto sink = sup1->create_actor<sink_t>().timeout(rt::default_timeout).finish();
  149. auto target = sup2->create_actor<target_t>().timeout(rt::default_timeout).finish();
  150. target->sink_addr = sink->get_address();
  151. while (!sup1->get_leader_queue().empty() || !sup2->get_leader_queue().empty()) {
  152. sup1->do_process();
  153. sup2->do_process();
  154. }
  155. REQUIRE(sup1->get_state() == r::state_t::OPERATIONAL);
  156. REQUIRE(sup2->get_state() == r::state_t::OPERATIONAL);
  157. REQUIRE(samples_received == 0);
  158. auto msg = r::make_routed_message<payload::sample_payload_t>(sink->get_address(), sink->destroy_addr);
  159. sup1->put(std::move(msg));
  160. while (!sup1->get_leader_queue().empty() || !sup2->get_leader_queue().empty()) {
  161. sup1->do_process();
  162. sup2->do_process();
  163. }
  164. REQUIRE(samples_received == 20);
  165. }