36-queue-full.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. using namespace rotor_light;
  11. struct Sample : Message {
  12. static constexpr auto type_id = __LINE__;
  13. using Message::Message;
  14. };
  15. using MessageStorage = traits::MessageStorage<message::ChangeState,
  16. message::ChangeStateAck, Sample>;
  17. using AppQueue = Queue<MessageStorage, 5>;
  18. using AppSupervisor = Supervisor<3>;
  19. using AppPlanner = Planner<1>;
  20. static int overfill = 0;
  21. namespace rotor_light {
  22. void on_queue_full() { ++overfill; }
  23. } // namespace rotor_light
  24. TEST_CASE("simple ping-pong example", "[actor]") {
  25. AppQueue queue;
  26. AppPlanner planner;
  27. Context context{&queue, &planner, nullptr};
  28. AppSupervisor sup;
  29. sup.bind(context);
  30. sup.start();
  31. sup.process();
  32. CHECK(overfill == 0);
  33. for (int i = 0; i < 5; ++i) {
  34. bool ok = sup.send<ctx::thread, Sample>(0, sup.get_id());
  35. CHECK(ok);
  36. }
  37. CHECK(overfill == 0);
  38. bool ok = sup.send<ctx::thread, Sample>(0, sup.get_id());
  39. CHECK(!ok);
  40. CHECK(overfill == 0);
  41. sup.stop();
  42. CHECK(overfill == 1);
  43. sup.process();
  44. sup.stop();
  45. sup.process();
  46. CHECK(sup.get_state() == State::off);
  47. }