30-supervisor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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();
  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();
  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 == 2);
  84. sup.stop();
  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();
  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. stop();
  128. ++starts;
  129. }
  130. size_t starts;
  131. };
  132. using AppSupervisor = Supervisor<3, DummyActor>;
  133. AppPlanner planner;
  134. AppQueue queue;
  135. AppSupervisor sup;
  136. Context context{&queue, &planner, nullptr};
  137. sup.bind(context);
  138. auto act = sup.get_child<0>();
  139. sup.start();
  140. sup.process();
  141. CHECK(sup.get_state() == State::operational);
  142. CHECK(act->get_state() == State::off);
  143. CHECK(act->starts == 1);
  144. sup.stop();
  145. sup.process();
  146. CHECK(sup.get_state() == State::off);
  147. CHECK(act->get_state() == State::off);
  148. }
  149. TEST_CASE("supervisor & failing actor, fail policy = force_restart (init)",
  150. "[sup]") {
  151. struct DummyActor : Actor<1> {
  152. DummyActor() {
  153. inits = 0;
  154. fail_policy = FailPolicy::force_restart;
  155. }
  156. void advance_init() override {
  157. send<ctx::thread, message::ChangeStateAck>(0, supervisor->get_id(), id,
  158. State::initialized, false);
  159. if (++inits >= 2) {
  160. fail_policy = FailPolicy::ignore;
  161. }
  162. }
  163. size_t inits;
  164. };
  165. using AppSupervisor = Supervisor<3, DummyActor>;
  166. AppPlanner planner;
  167. AppQueue queue;
  168. AppSupervisor sup;
  169. Context context{&queue, &planner, nullptr};
  170. sup.bind(context);
  171. auto act = sup.get_child<0>();
  172. sup.start();
  173. sup.process();
  174. CHECK(sup.get_state() == State::initializing);
  175. CHECK(act->get_state() == State::off);
  176. CHECK(act->inits == 2);
  177. sup.stop();
  178. sup.process();
  179. CHECK(sup.get_state() == State::off);
  180. CHECK(act->get_state() == State::off);
  181. }
  182. TEST_CASE("supervisor & failing actor, fail policy = force_restart (start)",
  183. "[sup]") {
  184. struct DummyActor : Actor<1> {
  185. using Parent = Actor<1>;
  186. DummyActor() {
  187. starts = 0;
  188. fail_policy = FailPolicy::force_restart;
  189. }
  190. void advance_start() override {
  191. Parent::advance_start();
  192. if (++starts >= 2) {
  193. fail_policy = FailPolicy::ignore;
  194. }
  195. stop();
  196. }
  197. size_t starts;
  198. };
  199. using AppSupervisor = Supervisor<3, DummyActor>;
  200. AppPlanner planner;
  201. AppQueue queue;
  202. AppSupervisor sup;
  203. Context context{&queue, &planner, nullptr};
  204. sup.bind(context);
  205. CHECK(sup.get_state() == State::off);
  206. CHECK(sup.get_id() == 1 << 0);
  207. auto act = sup.get_child<0>();
  208. CHECK(act->get_state() == State::off);
  209. CHECK(act->get_id() == 1 << 1);
  210. sup.start();
  211. sup.process();
  212. CHECK(sup.get_state() == State::operational);
  213. CHECK(act->get_state() == State::off);
  214. CHECK(act->starts == 2);
  215. sup.stop();
  216. sup.process();
  217. CHECK(sup.get_state() == State::off);
  218. CHECK(act->get_state() == State::off);
  219. }
  220. TEST_CASE("supervisor & failing actor, fail policy = escalate (init)",
  221. "[sup]") {
  222. struct DummyActor : Actor<1> {
  223. DummyActor() {
  224. inits = 0;
  225. fail_policy = FailPolicy::escalate;
  226. }
  227. void advance_init() override {
  228. send<ctx::thread, message::ChangeStateAck>(0, supervisor->get_id(), id,
  229. State::initialized, false);
  230. ++inits;
  231. }
  232. size_t inits;
  233. };
  234. using AppSupervisor = Supervisor<3, DummyActor>;
  235. AppPlanner planner;
  236. AppQueue queue;
  237. AppSupervisor sup;
  238. Context context{&queue, &planner, nullptr};
  239. sup.bind(context);
  240. CHECK(sup.get_state() == State::off);
  241. CHECK(sup.get_id() == 1 << 0);
  242. auto act = sup.get_child<0>();
  243. CHECK(act->get_state() == State::off);
  244. CHECK(act->get_id() == 1 << 1);
  245. sup.start();
  246. sup.process();
  247. CHECK(sup.get_state() == State::off);
  248. CHECK(act->get_state() == State::off);
  249. CHECK(act->inits == 1);
  250. }
  251. TEST_CASE("supervisor & failing actor, fail policy = escalate (operational)",
  252. "[sup]") {
  253. struct DummyActor : Actor<1> {
  254. using Parent = Actor<1>;
  255. DummyActor() {
  256. starts = 0;
  257. fail_policy = FailPolicy::escalate;
  258. }
  259. void advance_start() override {
  260. Parent::advance_start();
  261. stop();
  262. ++starts;
  263. }
  264. size_t starts;
  265. };
  266. using AppSupervisor = Supervisor<3, DummyActor>;
  267. AppPlanner planner;
  268. AppQueue queue;
  269. AppSupervisor sup;
  270. Context context{&queue, &planner, nullptr};
  271. sup.bind(context);
  272. auto act = sup.get_child<0>();
  273. sup.start();
  274. sup.process();
  275. CHECK(sup.get_state() == State::operational);
  276. CHECK(act->get_state() == State::off);
  277. CHECK(act->starts == 1);
  278. sup.stop();
  279. sup.process();
  280. CHECK(sup.get_state() == State::off);
  281. CHECK(act->get_state() == State::off);
  282. }
  283. TEST_CASE("supervisor & failing actor, fail policy = force_escalate (init)",
  284. "[sup]") {
  285. struct DummyActor : Actor<1> {
  286. DummyActor() {
  287. inits = 0;
  288. fail_policy = FailPolicy::force_escalate;
  289. }
  290. void advance_init() override {
  291. send<ctx::thread, message::ChangeStateAck>(0, supervisor->get_id(), id,
  292. State::initialized, false);
  293. ++inits;
  294. }
  295. size_t inits;
  296. };
  297. using AppSupervisor = Supervisor<3, DummyActor>;
  298. AppPlanner planner;
  299. AppQueue queue;
  300. AppSupervisor sup;
  301. Context context{&queue, &planner, nullptr};
  302. sup.bind(context);
  303. auto act = sup.get_child<0>();
  304. sup.start();
  305. sup.process();
  306. CHECK(sup.get_state() == State::off);
  307. CHECK(act->get_state() == State::off);
  308. CHECK(act->inits == 1);
  309. }
  310. TEST_CASE(
  311. "supervisor & failing actor, fail policy = force_escalate (operational)",
  312. "[sup]") {
  313. struct DummyActor : Actor<1> {
  314. using Parent = Actor<1>;
  315. DummyActor() {
  316. starts = 0;
  317. fail_policy = FailPolicy::force_escalate;
  318. }
  319. void advance_start() override {
  320. Parent::advance_start();
  321. stop();
  322. ++starts;
  323. }
  324. size_t starts;
  325. };
  326. using AppSupervisor = Supervisor<3, DummyActor>;
  327. AppPlanner planner;
  328. AppQueue queue;
  329. AppSupervisor sup;
  330. Context context{&queue, &planner, nullptr};
  331. sup.bind(context);
  332. auto act = sup.get_child<0>();
  333. sup.start();
  334. sup.process();
  335. CHECK(sup.get_state() == State::off);
  336. CHECK(act->get_state() == State::off);
  337. CHECK(act->starts == 1);
  338. }
  339. TEST_CASE("supervisor double stop", "[sup]") {
  340. struct DummyActor : Actor<1> {
  341. DummyActor() { fail_policy = FailPolicy::escalate; }
  342. void advance_init() override {
  343. send<ctx::thread, message::ChangeStateAck>(0, supervisor->get_id(), id,
  344. State::initialized, false);
  345. supervisor->stop();
  346. supervisor->stop();
  347. }
  348. };
  349. using AppSupervisor = Supervisor<3, DummyActor>;
  350. AppPlanner planner;
  351. AppQueue queue;
  352. AppSupervisor sup;
  353. Context context{&queue, &planner, nullptr};
  354. sup.bind(context);
  355. auto act = sup.get_child<0>();
  356. sup.start();
  357. sup.process();
  358. CHECK(sup.get_state() == State::off);
  359. CHECK(act->get_state() == State::off);
  360. }