supervisor_test.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2020 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "rotor/supervisor.h"
  8. #include "actor_test.h"
  9. #include <list>
  10. #include <catch2/catch_test_macros.hpp>
  11. namespace rotor {
  12. namespace test {
  13. extern pt::time_duration default_timeout;
  14. using interceptor_t = std::function<void(message_ptr_t &, const void *, const continuation_t &)>;
  15. struct supervisor_config_test_t : public supervisor_config_t {
  16. const void *locality = nullptr;
  17. plugin_configurer_t configurer = plugin_configurer_t{};
  18. interceptor_t interceptor = interceptor_t{};
  19. };
  20. template <typename Supervisor> struct supervisor_test_config_builder_t;
  21. struct supervisor_test_t : public supervisor_t {
  22. using timers_t = std::list<timer_handler_base_t *>;
  23. using config_t = supervisor_config_test_t;
  24. template <typename Supervisor> using config_builder_t = supervisor_test_config_builder_t<Supervisor>;
  25. supervisor_test_t(supervisor_config_test_t &config_);
  26. ~supervisor_test_t();
  27. void configure(plugin::plugin_base_t &plugin) noexcept override;
  28. virtual void do_start_timer(const pt::time_duration &interval, timer_handler_base_t &handler) noexcept override;
  29. virtual void do_cancel_timer(request_id_t timer_id) noexcept override;
  30. void do_invoke_timer(request_id_t timer_id) noexcept;
  31. request_id_t get_timer(std::size_t index) noexcept;
  32. virtual void start() noexcept override {}
  33. virtual void shutdown() noexcept override {}
  34. virtual void enqueue(rotor::message_ptr_t message) noexcept override;
  35. virtual address_ptr_t make_address() noexcept override;
  36. void intercept(message_ptr_t &message, const void *tag, const continuation_t &continuation) noexcept override;
  37. state_t &get_state() noexcept { return state; }
  38. messages_queue_t &get_leader_queue() { return get_leader().queue; }
  39. supervisor_test_t &get_leader();
  40. subscription_container_t &get_points() noexcept;
  41. subscription_t &get_subscription() noexcept { return subscription_map; }
  42. size_t get_children_count() noexcept;
  43. request_map_t &get_requests() noexcept { return request_map; }
  44. auto get_activating_plugins() noexcept { return this->activating_plugins; }
  45. auto get_deactivating_plugins() noexcept { return this->deactivating_plugins; }
  46. template <typename Plugin> Plugin *get_casted_plugin() noexcept {
  47. for (auto &p : plugins) {
  48. if (p->identity() == Plugin::class_identity) {
  49. return static_cast<Plugin *>(p);
  50. }
  51. }
  52. return nullptr;
  53. }
  54. const void *locality;
  55. timers_t active_timers;
  56. plugin_configurer_t configurer;
  57. interceptor_t interceptor;
  58. };
  59. using supervisor_test_ptr_t = rotor::intrusive_ptr_t<supervisor_test_t>;
  60. template <typename Supervisor> struct supervisor_test_config_builder_t : supervisor_config_builder_t<Supervisor> {
  61. using builder_t = typename Supervisor::template config_builder_t<Supervisor>;
  62. using parent_t = supervisor_config_builder_t<Supervisor>;
  63. using parent_t::parent_t;
  64. builder_t &&locality(const void *locality_) && {
  65. parent_t::config.locality = locality_;
  66. return std::move(*this);
  67. }
  68. builder_t &&configurer(plugin_configurer_t &&value) && {
  69. parent_t::config.configurer = std::move(value);
  70. return std::move(*this);
  71. }
  72. builder_t &&interceptor(interceptor_t &&value) && {
  73. parent_t::config.interceptor = std::move(value);
  74. return std::move(*this);
  75. }
  76. };
  77. struct system_test_context_t : system_context_t {
  78. using system_context_t::system_context_t;
  79. ~system_test_context_t();
  80. };
  81. } // namespace test
  82. } // namespace rotor