31-messages-order.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/planner.hpp"
  8. #include "rotor-light/queue.hpp"
  9. #include "rotor-light/supervisor.hpp"
  10. #include <string>
  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. static std::string order;
  17. void append(std::string_view marker, State state) {
  18. std::string_view s = "/unknown/";
  19. switch (state) {
  20. case State::initialized:
  21. s = "/initialized/";
  22. break;
  23. case State::initializing:
  24. s = "/initializing/";
  25. break;
  26. case State::operational:
  27. s = "/operational/";
  28. break;
  29. case State::shutting_down:
  30. s = "/shutting_down/";
  31. break;
  32. case State::off:
  33. s = "/off/";
  34. break;
  35. }
  36. order += marker;
  37. order += s;
  38. order += ", ";
  39. }
  40. struct A : Actor<1> {
  41. using Parent = Actor<1>;
  42. using Parent::Parent;
  43. void advance_init() override {
  44. append("a:init", state);
  45. Parent::advance_init();
  46. append("a:init", state);
  47. };
  48. void advance_start() override {
  49. append("a:start", state);
  50. Parent::advance_start();
  51. append("a:start", state);
  52. }
  53. void advance_stop() override {
  54. append("a:stop", state);
  55. Parent::advance_stop();
  56. append("a:stop", state);
  57. }
  58. };
  59. struct S : Supervisor<3, A> {
  60. using Parent = Supervisor<3, A>;
  61. using Parent::Parent;
  62. void advance_init() override {
  63. append("s:init", state);
  64. Parent::advance_init();
  65. append("s:init", state);
  66. };
  67. void advance_start() override {
  68. append("s:start", state);
  69. Parent::advance_start();
  70. append("s:start", state);
  71. }
  72. void advance_stop() override {
  73. append("s:stop", state);
  74. Parent::advance_stop();
  75. append("s:stop", state);
  76. }
  77. };
  78. TEST_CASE("actor & supervisor events order", "[sup]") {
  79. AppQueue queue;
  80. AppPlanner planner;
  81. Context context{&queue, &planner, nullptr};
  82. S sup;
  83. sup.bind(context);
  84. sup.start();
  85. sup.process();
  86. // clang-format off
  87. CHECK(order == "s:init/initializing/, "
  88. "s:init/initializing/, "
  89. "a:init/initializing/, "
  90. "a:init/initialized/, "
  91. "s:start/operational/, "
  92. "s:start/operational/, "
  93. "a:start/operational/, "
  94. "a:start/operational/, ");
  95. // clang-format on
  96. order = "";
  97. sup.stop();
  98. sup.process();
  99. // clang-format off
  100. CHECK(order == "s:stop/shutting_down/, "
  101. "s:stop/shutting_down/, "
  102. "a:stop/shutting_down/, "
  103. "a:stop/off/, ");
  104. // clang-format on
  105. }