30-supervisor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: MIT
  2. // SPDX-FileCopyrightText: 2022 Ivan Baidakou
  3. #if defined(__ANDROID__)
  4. #undef __ANDROID__
  5. #endif
  6. #include "catch.hpp"
  7. #include "rotor-light/context.hpp"
  8. #include "rotor-light/planner.hpp"
  9. #include "rotor-light/queue.hpp"
  10. #include "rotor-light/supervisor.hpp"
  11. using namespace rotor_light;
  12. using MessageStorage =
  13. traits::MessageStorage<message::ChangeState, message::ChangeStateAck>;
  14. using AppQueue = Queue<MessageStorage, 5>;
  15. using AppPlanner = Planner<1>;
  16. TEST_CASE("start & finish empty supervisor", "[sup]") {
  17. using AppSupervisor = Supervisor<3>;
  18. AppPlanner planner;
  19. AppQueue queue;
  20. AppSupervisor sup;
  21. Context context{&queue, &planner, nullptr};
  22. CHECK(sup.get_state() == State::off);
  23. sup.bind(context);
  24. sup.start();
  25. sup.process();
  26. CHECK(sup.get_state() == State::operational);
  27. sup.stop(true);
  28. sup.process();
  29. CHECK(sup.get_state() == State::off);
  30. }
  31. TEST_CASE("supervisor & succesfull actor", "[sup]") {
  32. using DummyActor = Actor<1>;
  33. using AppSupervisor = Supervisor<3, DummyActor>;
  34. AppPlanner planner;
  35. AppQueue queue;
  36. AppSupervisor sup;
  37. Context context{&queue, &planner, nullptr};
  38. CHECK(sup.get_state() == State::off);
  39. sup.bind(context);
  40. CHECK(sup.get_id() == 1 << 0);
  41. auto act = sup.get_child<0>();
  42. CHECK(act->get_state() == State::off);
  43. CHECK(act->get_id() == 1 << 1);
  44. sup.start();
  45. sup.process();
  46. CHECK(sup.get_state() == State::operational);
  47. CHECK(act->get_state() == State::operational);
  48. sup.stop(true);
  49. sup.process();
  50. CHECK(sup.get_state() == State::off);
  51. CHECK(act->get_state() == State::off);
  52. }
  53. TEST_CASE("supervisor & failing actor, fail policy = restart (init)", "[sup]") {
  54. struct DummyActor : Actor<1> {
  55. DummyActor() {
  56. inits = 0;
  57. fail_policy = FailPolicy::restart;
  58. }
  59. void advance_init() override {
  60. send<ctx::thread, message::ChangeStateAck>(0, supervisor->get_id(), id,
  61. State::initialized, false);
  62. if (++inits >= 2) {
  63. fail_policy = FailPolicy::ignore;
  64. }
  65. }
  66. size_t inits;
  67. };
  68. using AppSupervisor = Supervisor<3, DummyActor>;
  69. AppPlanner planner;
  70. AppQueue queue;
  71. AppSupervisor sup;
  72. Context context{&queue, &planner, nullptr};
  73. sup.bind(context);
  74. CHECK(sup.get_state() == State::off);
  75. CHECK(sup.get_id() == 1 << 0);
  76. auto act = sup.get_child<0>();
  77. CHECK(act->get_state() == State::off);
  78. CHECK(act->get_id() == 1 << 1);
  79. sup.start();
  80. sup.process();
  81. CHECK(sup.get_state() == State::initializing);
  82. CHECK(act->get_state() == State::off);
  83. CHECK(act->inits == 1);
  84. sup.stop(true);
  85. sup.process();
  86. CHECK(sup.get_state() == State::off);
  87. CHECK(act->get_state() == State::off);
  88. }
  89. TEST_CASE("supervisor & failing actor, fail policy = restart, do counts",
  90. "[sup]") {
  91. struct DummyActor : Actor<1> {
  92. DummyActor() {
  93. inits = 0;
  94. fail_policy = FailPolicy::restart;
  95. }
  96. void advance_init() override { ++inits; }
  97. size_t inits;
  98. };
  99. using AppSupervisor = Supervisor<3, DummyActor>;
  100. AppPlanner planner;
  101. AppQueue queue;
  102. AppSupervisor sup;
  103. Context context{&queue, &planner, nullptr};
  104. sup.bind(context);
  105. auto act = sup.get_child<0>();
  106. sup.start();
  107. sup.process();
  108. CHECK(sup.get_state() == State::initializing);
  109. CHECK(act->get_state() == State::initializing);
  110. CHECK(act->inits == 1);
  111. sup.stop(true);
  112. sup.process();
  113. CHECK(sup.get_state() == State::off);
  114. CHECK(act->get_state() == State::off);
  115. CHECK(act->inits == 1);
  116. }
  117. TEST_CASE("supervisor & failing actor, fail policy = init (operational)",
  118. "[sup]") {
  119. struct DummyActor : Actor<1> {
  120. using Parent = Actor<1>;
  121. DummyActor() {
  122. starts = 0;
  123. fail_policy = FailPolicy::restart;
  124. }
  125. void advance_start() override {
  126. Parent::advance_start();
  127. if (starts++ > 0) {
  128. set_fail_policy(FailPolicy::escalate);
  129. }
  130. stop(true);
  131. }
  132. size_t starts;
  133. };
  134. using AppSupervisor = Supervisor<3, DummyActor>;
  135. AppPlanner planner;
  136. AppQueue queue;
  137. AppSupervisor sup;
  138. Context context{&queue, &planner, nullptr};
  139. sup.bind(context);
  140. auto act = sup.get_child<0>();
  141. sup.start();
  142. sup.process();
  143. CHECK(sup.get_state() == State::operational);
  144. CHECK(act->get_state() == State::off);
  145. CHECK(act->starts == 2);
  146. sup.stop(true);
  147. sup.process();
  148. CHECK(sup.get_state() == State::off);
  149. CHECK(act->get_state() == State::off);
  150. }
  151. TEST_CASE("supervisor & failing actor, fail policy = force_restart (init)",
  152. "[sup]") {
  153. struct DummyActor : Actor<1> {
  154. DummyActor() {
  155. inits = 0;
  156. fail_policy = FailPolicy::force_restart;
  157. }
  158. void advance_init() override {
  159. send<ctx::thread, message::ChangeStateAck>(0, supervisor->get_id(), id,
  160. State::initialized, false);
  161. if (++inits >= 2) {
  162. fail_policy = FailPolicy::ignore;
  163. }
  164. }
  165. size_t inits;
  166. };
  167. using AppSupervisor = Supervisor<3, DummyActor>;
  168. AppPlanner planner;
  169. AppQueue queue;
  170. AppSupervisor sup;
  171. Context context{&queue, &planner, nullptr};
  172. sup.bind(context);
  173. auto act = sup.get_child<0>();
  174. sup.start();
  175. sup.process();
  176. CHECK(sup.get_state() == State::initializing);
  177. CHECK(act->get_state() == State::off);
  178. CHECK(act->inits == 2);
  179. sup.stop(true);
  180. sup.process();
  181. CHECK(sup.get_state() == State::off);
  182. CHECK(act->get_state() == State::off);
  183. }
  184. TEST_CASE("supervisor & failing actor, fail policy = force_restart (start)",
  185. "[sup]") {
  186. struct DummyActor : Actor<1> {
  187. using Parent = Actor<1>;
  188. DummyActor() {
  189. starts = 0;
  190. fail_policy = FailPolicy::force_restart;
  191. }
  192. void advance_start() override {
  193. Parent::advance_start();
  194. if (++starts >= 2) {
  195. fail_policy = FailPolicy::ignore;
  196. }
  197. stop(true);
  198. }
  199. size_t starts;
  200. };
  201. using AppSupervisor = Supervisor<3, DummyActor>;
  202. AppPlanner planner;
  203. AppQueue queue;
  204. AppSupervisor sup;
  205. Context context{&queue, &planner, nullptr};
  206. sup.bind(context);
  207. CHECK(sup.get_state() == State::off);
  208. CHECK(sup.get_id() == 1 << 0);
  209. auto act = sup.get_child<0>();
  210. CHECK(act->get_state() == State::off);
  211. CHECK(act->get_id() == 1 << 1);
  212. sup.start();
  213. sup.process();
  214. CHECK(sup.get_state() == State::operational);
  215. CHECK(act->get_state() == State::off);
  216. CHECK(act->starts == 2);
  217. sup.stop(true);
  218. sup.process();
  219. CHECK(sup.get_state() == State::off);
  220. CHECK(act->get_state() == State::off);
  221. }
  222. TEST_CASE("supervisor & failing actor, fail policy = escalate (init)",
  223. "[sup]") {
  224. struct DummyActor : Actor<1> {
  225. DummyActor() {
  226. inits = 0;
  227. fail_policy = FailPolicy::escalate;
  228. }
  229. void advance_init() override {
  230. send<ctx::thread, message::ChangeStateAck>(0, supervisor->get_id(), id,
  231. State::initialized, false);
  232. ++inits;
  233. }
  234. size_t inits;
  235. };
  236. using AppSupervisor = Supervisor<3, DummyActor>;
  237. AppPlanner planner;
  238. AppQueue queue;
  239. AppSupervisor sup;
  240. Context context{&queue, &planner, nullptr};
  241. sup.bind(context);
  242. CHECK(sup.get_state() == State::off);
  243. CHECK(sup.get_id() == 1 << 0);
  244. auto act = sup.get_child<0>();
  245. CHECK(act->get_state() == State::off);
  246. CHECK(act->get_id() == 1 << 1);
  247. sup.start();
  248. sup.process();
  249. CHECK(sup.get_state() == State::off);
  250. CHECK(act->get_state() == State::off);
  251. CHECK(act->inits == 1);
  252. }
  253. TEST_CASE("supervisor & failing actor, fail policy = escalate (operational)",
  254. "[sup]") {
  255. struct DummyActor : Actor<1> {
  256. using Parent = Actor<1>;
  257. DummyActor() {
  258. starts = 0;
  259. fail_policy = FailPolicy::escalate;
  260. }
  261. void advance_start() override {
  262. Parent::advance_start();
  263. stop(true);
  264. ++starts;
  265. }
  266. size_t starts;
  267. };
  268. using AppSupervisor = Supervisor<3, DummyActor>;
  269. AppPlanner planner;
  270. AppQueue queue;
  271. AppSupervisor sup;
  272. Context context{&queue, &planner, nullptr};
  273. sup.bind(context);
  274. auto act = sup.get_child<0>();
  275. sup.start();
  276. sup.process();
  277. CHECK(sup.get_state() == State::operational);
  278. CHECK(act->get_state() == State::off);
  279. CHECK(act->starts == 1);
  280. sup.stop(true);
  281. sup.process();
  282. CHECK(sup.get_state() == State::off);
  283. CHECK(act->get_state() == State::off);
  284. }
  285. TEST_CASE("supervisor & failing actor, fail policy = force_escalate (init)",
  286. "[sup]") {
  287. struct DummyActor : Actor<1> {
  288. DummyActor() {
  289. inits = 0;
  290. fail_policy = FailPolicy::force_escalate;
  291. }
  292. void advance_init() override {
  293. send<ctx::thread, message::ChangeStateAck>(0, supervisor->get_id(), id,
  294. State::initialized, false);
  295. ++inits;
  296. }
  297. size_t inits;
  298. };
  299. using AppSupervisor = Supervisor<3, DummyActor>;
  300. AppPlanner planner;
  301. AppQueue queue;
  302. AppSupervisor sup;
  303. Context context{&queue, &planner, nullptr};
  304. sup.bind(context);
  305. auto act = sup.get_child<0>();
  306. sup.start();
  307. sup.process();
  308. CHECK(sup.get_state() == State::off);
  309. CHECK(act->get_state() == State::off);
  310. CHECK(act->inits == 1);
  311. }
  312. TEST_CASE(
  313. "supervisor & failing actor, fail policy = force_escalate (operational)",
  314. "[sup]") {
  315. struct DummyActor : Actor<1> {
  316. using Parent = Actor<1>;
  317. DummyActor() {
  318. starts = 0;
  319. fail_policy = FailPolicy::force_escalate;
  320. }
  321. void advance_start() override {
  322. Parent::advance_start();
  323. stop(true);
  324. ++starts;
  325. }
  326. size_t starts;
  327. };
  328. using AppSupervisor = Supervisor<3, DummyActor>;
  329. AppPlanner planner;
  330. AppQueue queue;
  331. AppSupervisor sup;
  332. Context context{&queue, &planner, nullptr};
  333. sup.bind(context);
  334. auto act = sup.get_child<0>();
  335. sup.start();
  336. sup.process();
  337. CHECK(sup.get_state() == State::off);
  338. CHECK(act->get_state() == State::off);
  339. CHECK(act->starts == 1);
  340. }
  341. TEST_CASE("supervisor double stop", "[sup]") {
  342. struct DummyActor : Actor<1> {
  343. DummyActor() { fail_policy = FailPolicy::escalate; }
  344. void advance_init() override {
  345. send<ctx::thread, message::ChangeStateAck>(0, supervisor->get_id(), id,
  346. State::initialized, false);
  347. supervisor->stop(true);
  348. supervisor->stop(true);
  349. }
  350. };
  351. using AppSupervisor = Supervisor<3, DummyActor>;
  352. AppPlanner planner;
  353. AppQueue queue;
  354. AppSupervisor sup;
  355. Context context{&queue, &planner, nullptr};
  356. sup.bind(context);
  357. auto act = sup.get_child<0>();
  358. sup.start();
  359. sup.process();
  360. CHECK(sup.get_state() == State::off);
  361. CHECK(act->get_state() == State::off);
  362. }