125-cluster-supervisor.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "catch.hpp"
  2. #include "test-utils.h"
  3. #include "access.h"
  4. #include "fs/fs_actor.h"
  5. #include "net/db_actor.h"
  6. #include "net/cluster_supervisor.h"
  7. #include "net/names.h"
  8. #include "ui/messages.hpp"
  9. #include "utils/error_code.h"
  10. #include "utils/log.h"
  11. using namespace syncspirit;
  12. using namespace syncspirit::test;
  13. using namespace syncspirit::model;
  14. using namespace syncspirit::net;
  15. using supervisor_t = rotor::asio::supervisor_asio_t;
  16. namespace {
  17. using configure_callback_t = std::function<void(r::plugin::plugin_base_t &)>;
  18. struct sample_coordinator_t : r::actor_base_t {
  19. using message_t = message::cluster_ready_notify_t;
  20. using message_ptr_t = r::intrusive_ptr_t<message_t>;
  21. using r::actor_base_t::actor_base_t;
  22. message_ptr_t cluster_ready;
  23. configure_callback_t configure_callback;
  24. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  25. r::actor_base_t::configure(plugin);
  26. plugin.with_casted<r::plugin::registry_plugin_t>(
  27. [&](auto &p) { p.register_name(names::coordinator, get_address()); });
  28. plugin.with_casted<r::plugin::starter_plugin_t>(
  29. [&](auto &p) { p.subscribe_actor(&sample_coordinator_t::on_cluster_ready); });
  30. if (configure_callback) {
  31. configure_callback(plugin);
  32. }
  33. }
  34. void on_cluster_ready(message::cluster_ready_notify_t &msg) noexcept { cluster_ready = &msg; }
  35. };
  36. struct Fixture {
  37. using coordinator_ptr_t = r::intrusive_ptr_t<sample_coordinator_t>;
  38. model::device_ptr_t device_my;
  39. r::intrusive_ptr_t<supervisor_t> sup;
  40. coordinator_ptr_t coord;
  41. r::address_ptr_t cluster_addr;
  42. bfs::path root_path;
  43. model::cluster_ptr_t cluster;
  44. r::pt::time_duration timeout = r::pt::millisec{10};
  45. Fixture() { utils::set_default("trace"); }
  46. void run() {
  47. root_path = bfs::unique_path();
  48. bfs::create_directory(root_path);
  49. auto root_path_guard = path_guard_t(root_path);
  50. std::uint64_t key = 0;
  51. db::Device db_my;
  52. db_my.set_id(test::device_id2sha256("KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD"));
  53. db_my.set_name("d1");
  54. db_my.set_cert_name("d1_cert_name");
  55. device_my = model::device_ptr_t(new model::device_t(db_my, ++key));
  56. cluster = new cluster_t(device_my);
  57. asio::io_context io_context{1};
  58. ra::system_context_asio_t ctx(io_context);
  59. auto strand = std::make_shared<asio::io_context::strand>(io_context);
  60. sup = ctx.create_supervisor<supervisor_t>().timeout(timeout).strand(strand).create_registry().finish();
  61. sup->start();
  62. coord = sup->create_actor<sample_coordinator_t>().timeout(timeout).finish();
  63. sup->create_actor<fs::fs_actor_t>().fs_config({1024, 5, 0}).timeout(timeout).finish();
  64. sup->create_actor<db_actor_t>().db_dir((root_path / "db").string()).device(device_my).timeout(timeout).finish();
  65. pre_run();
  66. sup->do_process();
  67. auto reason = sup->get_shutdown_reason();
  68. if (reason) {
  69. spdlog::warn("shutdown reason = {}", reason->message());
  70. };
  71. REQUIRE(!reason);
  72. model::devices_map_t devices;
  73. devices.put(device_my);
  74. model::ignored_folders_map_t ignored_folders;
  75. auto cluster_sup = sup->create_actor<cluster_supervisor_t>()
  76. .timeout(timeout)
  77. .strand(strand)
  78. .device(device_my)
  79. .devices(&devices)
  80. .ignored_folders(&ignored_folders)
  81. .cluster(cluster)
  82. .bep_config(config::bep_config_t{})
  83. .finish();
  84. cluster_addr = cluster_sup->get_address();
  85. sup->do_process();
  86. reason = cluster_sup->get_shutdown_reason();
  87. if (reason) {
  88. spdlog::warn("shutdown reason = {}", reason->message());
  89. };
  90. REQUIRE(!reason);
  91. auto &state = static_cast<r::actor_base_t *>(sup.get())->access<to::state>();
  92. REQUIRE(state == r::state_t::OPERATIONAL);
  93. main();
  94. sup->shutdown();
  95. sup->do_process();
  96. io_context.run(); // avoid mem leaks
  97. }
  98. virtual void pre_run(){};
  99. virtual void main(){};
  100. };
  101. } // namespace
  102. void test_start_empty_cluster() {
  103. struct F : Fixture {
  104. void main() override { REQUIRE(coord->cluster_ready); }
  105. };
  106. F().run();
  107. }
  108. void test_add_new_folder() {
  109. using request_t = ui::message::create_folder_request_t;
  110. using response_t = ui::message::create_folder_response_t;
  111. using response_ptr_t = r::intrusive_ptr_t<response_t>;
  112. struct F : Fixture {
  113. response_ptr_t res;
  114. void pre_run() override {
  115. coord->configure_callback = [&](r::plugin::plugin_base_t &plugin) {
  116. plugin.template with_casted<r::plugin::starter_plugin_t>(
  117. [&](auto &p) { p.subscribe_actor(r::lambda<response_t>([&](response_t &msg) { res = &msg; })); });
  118. };
  119. }
  120. void main() override {
  121. REQUIRE(coord->cluster_ready);
  122. REQUIRE(cluster->get_folders().size() == 0);
  123. auto new_dir = root_path / "new_dir";
  124. auto new_file = new_dir / "some-file.txt";
  125. bfs::create_directory(new_dir);
  126. write_file(new_file, "zzz");
  127. db::Folder folder;
  128. folder.set_path(new_dir.string());
  129. folder.set_id("1235");
  130. folder.set_label("my-label");
  131. coord->request<ui::payload::create_folder_request_t>(cluster_addr, folder).send(timeout);
  132. sup->do_process();
  133. REQUIRE(res);
  134. REQUIRE(!res->payload.ee);
  135. REQUIRE(cluster->get_folders().size() == 1);
  136. }
  137. };
  138. F().run();
  139. }
  140. REGISTER_TEST_CASE(test_start_empty_cluster, "test_start_empty_cluster", "[cluster]");
  141. REGISTER_TEST_CASE(test_add_new_folder, "test_add_new_folder", "[cluster]");