30-supervisor.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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<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 = init (operational)",
  90. "[sup]") {
  91. struct DummyActor : Actor<1> {
  92. using Parent = Actor<1>;
  93. DummyActor() {
  94. starts = 0;
  95. fail_policy = FailPolicy::restart;
  96. }
  97. void advance_start() override {
  98. Parent::advance_start();
  99. stop();
  100. ++starts;
  101. }
  102. size_t starts;
  103. };
  104. using AppSupervisor = Supervisor<3, DummyActor>;
  105. AppPlanner planner;
  106. AppQueue queue;
  107. AppSupervisor sup;
  108. Context context{&queue, &planner, nullptr};
  109. sup.bind(context);
  110. auto act = sup.get_child<0>();
  111. sup.start();
  112. sup.process();
  113. CHECK(sup.get_state() == State::operational);
  114. CHECK(act->get_state() == State::off);
  115. CHECK(act->starts == 1);
  116. sup.stop();
  117. sup.process();
  118. CHECK(sup.get_state() == State::off);
  119. CHECK(act->get_state() == State::off);
  120. }
  121. TEST_CASE("supervisor & failing actor, fail policy = force_restart (init)",
  122. "[sup]") {
  123. struct DummyActor : Actor<1> {
  124. DummyActor() {
  125. inits = 0;
  126. fail_policy = FailPolicy::force_restart;
  127. }
  128. void advance_init() override {
  129. send<message::ChangeStateAck>(0, supervisor->get_id(), id,
  130. State::initialized, false);
  131. if (++inits >= 2) {
  132. fail_policy = FailPolicy::ignore;
  133. }
  134. }
  135. size_t inits;
  136. };
  137. using AppSupervisor = Supervisor<3, DummyActor>;
  138. AppPlanner planner;
  139. AppQueue queue;
  140. AppSupervisor sup;
  141. Context context{&queue, &planner, nullptr};
  142. sup.bind(context);
  143. auto act = sup.get_child<0>();
  144. sup.start();
  145. sup.process();
  146. CHECK(sup.get_state() == State::initializing);
  147. CHECK(act->get_state() == State::off);
  148. CHECK(act->inits == 2);
  149. sup.stop();
  150. sup.process();
  151. CHECK(sup.get_state() == State::off);
  152. CHECK(act->get_state() == State::off);
  153. }
  154. TEST_CASE("supervisor & failing actor, fail policy = force_restart (start)",
  155. "[sup]") {
  156. struct DummyActor : Actor<1> {
  157. using Parent = Actor<1>;
  158. DummyActor() {
  159. starts = 0;
  160. fail_policy = FailPolicy::force_restart;
  161. }
  162. void advance_start() override {
  163. Parent::advance_start();
  164. if (++starts >= 2) {
  165. fail_policy = FailPolicy::ignore;
  166. }
  167. stop();
  168. }
  169. size_t starts;
  170. };
  171. using AppSupervisor = Supervisor<3, DummyActor>;
  172. AppPlanner planner;
  173. AppQueue queue;
  174. AppSupervisor sup;
  175. Context context{&queue, &planner, nullptr};
  176. sup.bind(context);
  177. CHECK(sup.get_state() == State::off);
  178. CHECK(sup.get_id() == 1 << 0);
  179. auto act = sup.get_child<0>();
  180. CHECK(act->get_state() == State::off);
  181. CHECK(act->get_id() == 1 << 1);
  182. sup.start();
  183. sup.process();
  184. CHECK(sup.get_state() == State::operational);
  185. CHECK(act->get_state() == State::off);
  186. CHECK(act->starts == 2);
  187. sup.stop();
  188. sup.process();
  189. CHECK(sup.get_state() == State::off);
  190. CHECK(act->get_state() == State::off);
  191. }
  192. TEST_CASE("supervisor & failing actor, fail policy = escalate (init)",
  193. "[sup]") {
  194. struct DummyActor : Actor<1> {
  195. DummyActor() {
  196. inits = 0;
  197. fail_policy = FailPolicy::escalate;
  198. }
  199. void advance_init() override {
  200. send<message::ChangeStateAck>(0, supervisor->get_id(), id,
  201. State::initialized, false);
  202. ++inits;
  203. }
  204. size_t inits;
  205. };
  206. using AppSupervisor = Supervisor<3, DummyActor>;
  207. AppPlanner planner;
  208. AppQueue queue;
  209. AppSupervisor sup;
  210. Context context{&queue, &planner, nullptr};
  211. sup.bind(context);
  212. CHECK(sup.get_state() == State::off);
  213. CHECK(sup.get_id() == 1 << 0);
  214. auto act = sup.get_child<0>();
  215. CHECK(act->get_state() == State::off);
  216. CHECK(act->get_id() == 1 << 1);
  217. sup.start();
  218. sup.process();
  219. CHECK(sup.get_state() == State::off);
  220. CHECK(act->get_state() == State::off);
  221. CHECK(act->inits == 1);
  222. }
  223. TEST_CASE("supervisor & failing actor, fail policy = escalate (operational)",
  224. "[sup]") {
  225. struct DummyActor : Actor<1> {
  226. using Parent = Actor<1>;
  227. DummyActor() {
  228. starts = 0;
  229. fail_policy = FailPolicy::escalate;
  230. }
  231. void advance_start() override {
  232. Parent::advance_start();
  233. stop();
  234. ++starts;
  235. }
  236. size_t starts;
  237. };
  238. using AppSupervisor = Supervisor<3, DummyActor>;
  239. AppPlanner planner;
  240. AppQueue queue;
  241. AppSupervisor sup;
  242. Context context{&queue, &planner, nullptr};
  243. sup.bind(context);
  244. auto act = sup.get_child<0>();
  245. sup.start();
  246. sup.process();
  247. CHECK(sup.get_state() == State::operational);
  248. CHECK(act->get_state() == State::off);
  249. CHECK(act->starts == 1);
  250. sup.stop();
  251. sup.process();
  252. CHECK(sup.get_state() == State::off);
  253. CHECK(act->get_state() == State::off);
  254. }
  255. TEST_CASE("supervisor & failing actor, fail policy = force_escalate (init)",
  256. "[sup]") {
  257. struct DummyActor : Actor<1> {
  258. DummyActor() {
  259. inits = 0;
  260. fail_policy = FailPolicy::force_escalate;
  261. }
  262. void advance_init() override {
  263. send<message::ChangeStateAck>(0, supervisor->get_id(), id,
  264. State::initialized, false);
  265. ++inits;
  266. }
  267. size_t inits;
  268. };
  269. using AppSupervisor = Supervisor<3, DummyActor>;
  270. AppPlanner planner;
  271. AppQueue queue;
  272. AppSupervisor sup;
  273. Context context{&queue, &planner, nullptr};
  274. sup.bind(context);
  275. auto act = sup.get_child<0>();
  276. sup.start();
  277. sup.process();
  278. CHECK(sup.get_state() == State::off);
  279. CHECK(act->get_state() == State::off);
  280. CHECK(act->inits == 1);
  281. }
  282. TEST_CASE(
  283. "supervisor & failing actor, fail policy = force_escalate (operational)",
  284. "[sup]") {
  285. struct DummyActor : Actor<1> {
  286. using Parent = Actor<1>;
  287. DummyActor() {
  288. starts = 0;
  289. fail_policy = FailPolicy::force_escalate;
  290. }
  291. void advance_start() override {
  292. Parent::advance_start();
  293. stop();
  294. ++starts;
  295. }
  296. size_t starts;
  297. };
  298. using AppSupervisor = Supervisor<3, DummyActor>;
  299. AppPlanner planner;
  300. AppQueue queue;
  301. AppSupervisor sup;
  302. Context context{&queue, &planner, nullptr};
  303. sup.bind(context);
  304. auto act = sup.get_child<0>();
  305. sup.start();
  306. sup.process();
  307. CHECK(sup.get_state() == State::off);
  308. CHECK(act->get_state() == State::off);
  309. CHECK(act->starts == 1);
  310. }