018-lambda-handlers.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // Copyright (c) 2019-2020 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. #include "rotor.hpp"
  7. #include "supervisor_test.h"
  8. #include "access.h"
  9. namespace r = rotor;
  10. namespace rt = r::test;
  11. static const void *my_tag = &my_tag;
  12. struct payload {
  13. const void *data;
  14. };
  15. using message_t = r::message_t<payload>;
  16. struct sample_actor_t : public r::actor_base_t {
  17. using r::actor_base_t::actor_base_t;
  18. bool received = false;
  19. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  20. plugin.with_casted<r::plugin::starter_plugin_t>([this](auto &p) {
  21. r::subscription_info_ptr_t info =
  22. p.subscribe_actor(r::lambda<message_t>([this](message_t &) noexcept { received = true; }));
  23. info->access<rt::to::tag, const void *>(my_tag);
  24. });
  25. }
  26. void on_start() noexcept override {
  27. r::actor_base_t::on_start();
  28. send<payload>(address, my_tag);
  29. }
  30. };
  31. TEST_CASE("lambda handler + interceptor", "[actor]") {
  32. r::system_context_t system_context;
  33. bool intercepted = false;
  34. rt::interceptor_t interceptor = [&](auto &msg, const void *tag, const auto &cont) {
  35. if (tag == my_tag) {
  36. auto my_msg = static_cast<message_t *>(msg.get());
  37. if (my_msg->payload.data == my_tag) {
  38. intercepted = true;
  39. }
  40. }
  41. cont();
  42. };
  43. auto sup = system_context.create_supervisor<rt::supervisor_test_t>()
  44. .interceptor(std::move(interceptor))
  45. .timeout(rt::default_timeout)
  46. .finish();
  47. auto actor = sup->create_actor<sample_actor_t>().timeout(rt::default_timeout).finish();
  48. sup->do_process();
  49. REQUIRE(sup->active_timers.size() == 0);
  50. REQUIRE(actor->received == true);
  51. sup->do_shutdown();
  52. sup->do_process();
  53. REQUIRE(sup->get_state() == r::state_t::SHUT_DOWN);
  54. REQUIRE(sup->get_leader_queue().size() == 0);
  55. REQUIRE(sup->get_points().size() == 0);
  56. REQUIRE(rt::empty(sup->get_subscription()));
  57. CHECK(intercepted);
  58. }