024-supervisor-spawner.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. //
  2. // Copyright (c) 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 "actor_test.h"
  8. #include "supervisor_test.h"
  9. #include "system_context_test.h"
  10. #include "access.h"
  11. namespace r = rotor;
  12. namespace rt = r::test;
  13. namespace payload {
  14. struct sample_payload_t {};
  15. } // namespace payload
  16. namespace message {
  17. using sample_payload_t = r::message_t<payload::sample_payload_t>;
  18. }
  19. struct sample_actor_t : rt::actor_test_t {
  20. using rt::actor_test_t::actor_test_t;
  21. bool restart_flag = false;
  22. void on_start() noexcept override {
  23. rt::actor_test_t::on_start();
  24. auto sup = supervisor->access<rt::to::locality_leader>();
  25. send<payload::sample_payload_t>(sup->get_address());
  26. }
  27. bool should_restart() const noexcept override { return restart_flag; }
  28. };
  29. struct sample_supervisor_t : rt::supervisor_test_t {
  30. using rt::supervisor_test_t::supervisor_test_t;
  31. void trigger_timers_and_process() noexcept {
  32. do_process();
  33. while (!active_timers.empty()) {
  34. auto &handler = *active_timers.begin();
  35. do_invoke_timer(handler->request_id);
  36. do_process();
  37. }
  38. }
  39. };
  40. struct test_supervisor1_t : rt::supervisor_test_t {
  41. using rt::supervisor_test_t::supervisor_test_t;
  42. void on_start() noexcept override {
  43. rt::supervisor_test_t::on_start();
  44. auto sup = supervisor->access<rt::to::locality_leader>();
  45. send<payload::sample_payload_t>(sup->get_address());
  46. }
  47. };
  48. struct test_supervisor2_t : sample_supervisor_t {
  49. using sample_supervisor_t::sample_supervisor_t;
  50. r::actor_ptr_t child;
  51. void on_start() noexcept override {
  52. rt::supervisor_test_t::on_start();
  53. auto factory = [&](r::supervisor_t &sup, const r::address_ptr_t &spawner) -> r::actor_ptr_t {
  54. child = sup.create_actor<sample_actor_t>().timeout(rt::default_timeout).spawner_address(spawner).finish();
  55. return child;
  56. };
  57. spawn(factory).max_attempts(2).restart_policy(r::restart_policy_t::always).spawn();
  58. auto root = supervisor->access<rt::to::locality_leader>();
  59. send<payload::sample_payload_t>(root->get_address());
  60. }
  61. };
  62. TEST_CASE("throw in factory", "[spawner]") {
  63. r::system_context_t system_context;
  64. auto sup = system_context.create_supervisor<sample_supervisor_t>().timeout(rt::default_timeout).finish();
  65. sup->do_process();
  66. REQUIRE(sup->get_state() == r::state_t::OPERATIONAL);
  67. size_t invocations = 0;
  68. SECTION("restart_policy: never") {
  69. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &) -> r::actor_ptr_t {
  70. ++invocations;
  71. throw std::runtime_error("does-not-matter");
  72. };
  73. sup->spawn(factory).restart_policy(r::restart_policy_t::never).restart_period(r::pt::seconds{1}).spawn();
  74. sup->do_process();
  75. CHECK(invocations == 1);
  76. }
  77. SECTION("restart_policy: ask_actor") {
  78. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &) -> r::actor_ptr_t {
  79. ++invocations;
  80. throw std::runtime_error("does-not-matter");
  81. };
  82. sup->spawn(factory).restart_policy(r::restart_policy_t::ask_actor).spawn();
  83. sup->do_process();
  84. CHECK(invocations == 1);
  85. }
  86. SECTION("restart_policy: normal_only") {
  87. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &) -> r::actor_ptr_t {
  88. ++invocations;
  89. throw std::runtime_error("does-not-matter");
  90. };
  91. sup->spawn(factory).restart_policy(r::restart_policy_t::normal_only).spawn();
  92. sup->do_process();
  93. CHECK(invocations == 1);
  94. }
  95. SECTION("restart_policy: failure_only") {
  96. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &) -> r::actor_ptr_t {
  97. ++invocations;
  98. if (invocations > 3) {
  99. sup->do_shutdown();
  100. }
  101. throw std::runtime_error("does-not-matter");
  102. };
  103. sup->spawn(factory).restart_policy(r::restart_policy_t::fail_only).spawn();
  104. sup->trigger_timers_and_process();
  105. CHECK(invocations > 3);
  106. }
  107. SECTION("restart_policy: always") {
  108. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &) -> r::actor_ptr_t {
  109. ++invocations;
  110. if (invocations > 3) {
  111. sup->do_shutdown();
  112. }
  113. throw std::runtime_error("does-not-matter");
  114. };
  115. sup->spawn(factory).restart_policy(r::restart_policy_t::always).spawn();
  116. sup->trigger_timers_and_process();
  117. CHECK(invocations > 3);
  118. }
  119. SECTION("restart_policy: always + max_attempts") {
  120. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &) -> r::actor_ptr_t {
  121. ++invocations;
  122. throw std::runtime_error("does-not-matter");
  123. };
  124. sup->spawn(factory).restart_policy(r::restart_policy_t::always).max_attempts(3).spawn();
  125. sup->trigger_timers_and_process();
  126. CHECK(invocations == 3);
  127. }
  128. sup->do_shutdown();
  129. sup->do_process();
  130. CHECK(sup->get_state() == r::state_t::SHUT_DOWN);
  131. }
  132. TEST_CASE("normal flow", "[spawner]") {
  133. r::system_context_t system_context;
  134. auto sup = system_context.create_supervisor<sample_supervisor_t>().timeout(rt::default_timeout).finish();
  135. size_t samples = 0;
  136. sup->configurer = [&](auto &, r::plugin::plugin_base_t &plugin) {
  137. plugin.with_casted<r::plugin::starter_plugin_t>([&](auto &p) {
  138. p.subscribe_actor(r::lambda<message::sample_payload_t>([&](auto &) noexcept { ++samples; }));
  139. });
  140. };
  141. sup->do_process();
  142. REQUIRE(sup->get_state() == r::state_t::OPERATIONAL);
  143. size_t invocations = 0;
  144. r::actor_ptr_t act;
  145. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &spawner) -> r::actor_ptr_t {
  146. ++invocations;
  147. act = sup->create_actor<sample_actor_t>().timeout(rt::default_timeout).spawner_address(spawner).finish();
  148. return act;
  149. };
  150. auto ec = r::make_error_code(r::error_code_t::cancelled);
  151. auto ee = r::make_error("custom", ec);
  152. SECTION("restart_policy: never") {
  153. sup->spawn(factory).restart_policy(r::restart_policy_t::never).spawn();
  154. sup->do_process();
  155. CHECK(invocations == 1);
  156. CHECK(samples == 1);
  157. act->do_shutdown();
  158. sup->do_process();
  159. sup->do_shutdown();
  160. sup->do_process();
  161. CHECK(invocations == 1);
  162. CHECK(samples == 1);
  163. }
  164. SECTION("restart_policy: always") {
  165. sup->spawn(factory).restart_policy(r::restart_policy_t::always).max_attempts(2).spawn();
  166. sup->do_process();
  167. CHECK(invocations == 1);
  168. CHECK(samples == 1);
  169. act->do_shutdown();
  170. act.reset();
  171. sup->trigger_timers_and_process();
  172. CHECK(act);
  173. CHECK(invocations == 2);
  174. CHECK(samples == 2);
  175. sup->do_shutdown();
  176. sup->do_process();
  177. CHECK(invocations == 2);
  178. CHECK(samples == 2);
  179. }
  180. SECTION("restart_policy: normal_only") {
  181. sup->spawn(factory).restart_policy(r::restart_policy_t::normal_only).spawn();
  182. sup->do_process();
  183. CHECK(invocations == 1);
  184. CHECK(samples == 1);
  185. act->do_shutdown();
  186. act.reset();
  187. sup->trigger_timers_and_process();
  188. CHECK(act);
  189. CHECK(invocations == 2);
  190. CHECK(samples == 2);
  191. act->do_shutdown(ee);
  192. act.reset();
  193. sup->trigger_timers_and_process();
  194. CHECK(!act);
  195. CHECK(invocations == 2);
  196. CHECK(samples == 2);
  197. sup->do_shutdown();
  198. sup->do_process();
  199. CHECK(invocations == 2);
  200. CHECK(samples == 2);
  201. }
  202. SECTION("restart_policy: fail_only") {
  203. sup->spawn(factory).restart_policy(r::restart_policy_t::fail_only).spawn();
  204. sup->do_process();
  205. CHECK(invocations == 1);
  206. CHECK(samples == 1);
  207. act->do_shutdown(ee);
  208. act.reset();
  209. sup->trigger_timers_and_process();
  210. CHECK(act);
  211. CHECK(invocations == 2);
  212. CHECK(samples == 2);
  213. act->do_shutdown();
  214. act.reset();
  215. sup->trigger_timers_and_process();
  216. CHECK(!act);
  217. CHECK(invocations == 2);
  218. CHECK(samples == 2);
  219. sup->do_shutdown();
  220. sup->do_process();
  221. CHECK(invocations == 2);
  222. CHECK(samples == 2);
  223. }
  224. SECTION("restart_policy: ask_actor") {
  225. sup->spawn(factory).restart_policy(r::restart_policy_t::ask_actor).spawn();
  226. sup->do_process();
  227. CHECK(invocations == 1);
  228. CHECK(samples == 1);
  229. auto *actor = static_cast<sample_actor_t *>(act.get());
  230. actor->restart_flag = true;
  231. act->do_shutdown(ee);
  232. act.reset();
  233. sup->trigger_timers_and_process();
  234. CHECK(act);
  235. CHECK(invocations == 2);
  236. CHECK(samples == 2);
  237. actor = static_cast<sample_actor_t *>(act.get());
  238. actor->restart_flag = false;
  239. act->do_shutdown();
  240. act.reset();
  241. sup->trigger_timers_and_process();
  242. CHECK(!act);
  243. CHECK(invocations == 2);
  244. CHECK(samples == 2);
  245. sup->do_shutdown();
  246. sup->do_process();
  247. CHECK(invocations == 2);
  248. CHECK(samples == 2);
  249. }
  250. CHECK(sup->get_state() == r::state_t::SHUT_DOWN);
  251. }
  252. TEST_CASE("trees of supervisors", "[spawner]") {
  253. r::system_context_t system_context;
  254. auto sup = system_context.create_supervisor<sample_supervisor_t>().timeout(rt::default_timeout).finish();
  255. size_t samples = 0;
  256. sup->configurer = [&](auto &, r::plugin::plugin_base_t &plugin) {
  257. plugin.with_casted<r::plugin::starter_plugin_t>([&](auto &p) {
  258. p.subscribe_actor(r::lambda<message::sample_payload_t>([&](auto &) noexcept { ++samples; }));
  259. });
  260. };
  261. sup->do_process();
  262. REQUIRE(sup->get_state() == r::state_t::OPERATIONAL);
  263. SECTION("just with inner sup") {
  264. r::actor_ptr_t act;
  265. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &spawner) -> r::actor_ptr_t {
  266. act =
  267. sup->create_actor<test_supervisor1_t>().timeout(rt::default_timeout).spawner_address(spawner).finish();
  268. return act;
  269. };
  270. sup->spawn(factory).restart_policy(r::restart_policy_t::always).max_attempts(2).spawn();
  271. sup->do_process();
  272. CHECK(samples == 1);
  273. act->do_shutdown();
  274. sup->trigger_timers_and_process();
  275. CHECK(samples == 2);
  276. sup->do_shutdown();
  277. sup->do_process();
  278. CHECK(samples == 2);
  279. }
  280. SECTION("inner sup with a child") {
  281. r::actor_ptr_t act;
  282. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &spawner) -> r::actor_ptr_t {
  283. act =
  284. sup->create_actor<test_supervisor2_t>().timeout(rt::default_timeout).spawner_address(spawner).finish();
  285. return act;
  286. };
  287. sup->spawn(factory).restart_policy(r::restart_policy_t::always).max_attempts(2).spawn();
  288. sup->do_process();
  289. CHECK(samples == 2);
  290. static_cast<test_supervisor2_t *>(act.get())->child->do_shutdown();
  291. static_cast<test_supervisor2_t *>(act.get())->trigger_timers_and_process();
  292. CHECK(samples == 3);
  293. static_cast<test_supervisor2_t *>(act.get())->child->do_shutdown();
  294. static_cast<test_supervisor2_t *>(act.get())->trigger_timers_and_process();
  295. CHECK(samples == 3);
  296. act->do_shutdown();
  297. sup->trigger_timers_and_process();
  298. CHECK(samples == 5);
  299. static_cast<test_supervisor2_t *>(act.get())->child->do_shutdown();
  300. static_cast<test_supervisor2_t *>(act.get())->trigger_timers_and_process();
  301. CHECK(samples == 6);
  302. static_cast<test_supervisor2_t *>(act.get())->child->do_shutdown();
  303. static_cast<test_supervisor2_t *>(act.get())->trigger_timers_and_process();
  304. CHECK(samples == 6);
  305. act->do_shutdown();
  306. sup->trigger_timers_and_process();
  307. CHECK(samples == 6);
  308. sup->do_shutdown();
  309. sup->do_process();
  310. CHECK(samples == 6);
  311. }
  312. CHECK(sup->get_state() == r::state_t::SHUT_DOWN);
  313. }
  314. TEST_CASE("factory throws on supervisor initialization", "[spawner]") {
  315. r::system_context_t system_context;
  316. auto sup = system_context.create_supervisor<sample_supervisor_t>()
  317. .timeout(rt::default_timeout)
  318. .policy(r::supervisor_policy_t::shutdown_self)
  319. .finish();
  320. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &) -> r::actor_ptr_t { throw "does-not-matter"; };
  321. sup->spawn(factory).spawn();
  322. static_cast<r::actor_base_t *>(sup.get())->access<rt::to::resources>()->acquire();
  323. sup->do_process();
  324. static_cast<r::actor_base_t *>(sup.get())->access<rt::to::resources>()->release();
  325. sup->do_process();
  326. CHECK(sup->get_state() == r::state_t::SHUT_DOWN);
  327. }
  328. TEST_CASE("escalate failure", "[spawner]") {
  329. r::system_context_t system_context;
  330. auto sup = system_context.create_supervisor<sample_supervisor_t>().timeout(rt::default_timeout).finish();
  331. size_t samples = 0;
  332. sup->configurer = [&](auto &, r::plugin::plugin_base_t &plugin) {
  333. plugin.with_casted<r::plugin::starter_plugin_t>([&](auto &p) {
  334. p.subscribe_actor(r::lambda<message::sample_payload_t>([&](auto &) noexcept { ++samples; }));
  335. });
  336. };
  337. sup->do_process();
  338. REQUIRE(sup->get_state() == r::state_t::OPERATIONAL);
  339. size_t invocations = 0;
  340. r::actor_ptr_t act;
  341. auto factory = [&](r::supervisor_t &, const r::address_ptr_t &spawner) -> r::actor_ptr_t {
  342. ++invocations;
  343. act = sup->create_actor<sample_actor_t>().timeout(rt::default_timeout).spawner_address(spawner).finish();
  344. return act;
  345. };
  346. auto ec = r::make_error_code(r::error_code_t::cancelled);
  347. auto ee = r::make_error("custom", ec);
  348. SECTION("restart_policy: always") {
  349. sup->spawn(factory).restart_policy(r::restart_policy_t::always).max_attempts(2).escalate_failure().spawn();
  350. sup->do_process();
  351. CHECK(invocations == 1);
  352. CHECK(samples == 1);
  353. act->do_shutdown(ee);
  354. act.reset();
  355. sup->trigger_timers_and_process();
  356. CHECK(act);
  357. CHECK(invocations == 2);
  358. CHECK(samples == 2);
  359. act->do_shutdown(ee);
  360. act.reset();
  361. sup->trigger_timers_and_process();
  362. CHECK(invocations == 2);
  363. CHECK(samples == 2);
  364. }
  365. SECTION("restart_policy: normal_only") {
  366. sup->spawn(factory).restart_policy(r::restart_policy_t::normal_only).max_attempts(2).escalate_failure().spawn();
  367. sup->do_process();
  368. CHECK(invocations == 1);
  369. CHECK(samples == 1);
  370. act->do_shutdown();
  371. act.reset();
  372. sup->trigger_timers_and_process();
  373. CHECK(act);
  374. CHECK(invocations == 2);
  375. CHECK(samples == 2);
  376. act->do_shutdown(ee);
  377. act.reset();
  378. sup->trigger_timers_and_process();
  379. CHECK(invocations == 2);
  380. CHECK(samples == 2);
  381. }
  382. SECTION("restart_policy: fail_only + max_fails") {
  383. sup->spawn(factory).restart_policy(r::restart_policy_t::fail_only).max_attempts(2).escalate_failure().spawn();
  384. sup->do_process();
  385. CHECK(invocations == 1);
  386. CHECK(samples == 1);
  387. act->do_shutdown(ee);
  388. act.reset();
  389. sup->trigger_timers_and_process();
  390. CHECK(act);
  391. CHECK(invocations == 2);
  392. CHECK(samples == 2);
  393. act->do_shutdown(ee);
  394. act.reset();
  395. sup->trigger_timers_and_process();
  396. CHECK(invocations == 2);
  397. CHECK(samples == 2);
  398. }
  399. auto reason = sup->get_shutdown_reason();
  400. REQUIRE(reason);
  401. CHECK(reason->root() == ee);
  402. CHECK(sup->get_state() == r::state_t::SHUT_DOWN);
  403. }