|
| 1 | +#include <userver/utest/utest.hpp> |
| 2 | + |
| 3 | +#include <userver/components/component_base.hpp> |
| 4 | +#include <userver/components/component_context.hpp> |
| 5 | +#include <userver/components/minimal_component_list.hpp> |
| 6 | +#include <userver/components/run.hpp> |
| 7 | +#include <userver/components/scope.hpp> |
| 8 | +#include <userver/logging/log.hpp> |
| 9 | +#include <userver/utils/fast_scope_guard.hpp> |
| 10 | + |
| 11 | +USERVER_NAMESPACE_BEGIN |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +const std::string kConfig = R"( |
| 16 | +components_manager: |
| 17 | + coro_pool: |
| 18 | + initial_size: 50 |
| 19 | + max_size: 500 |
| 20 | + default_task_processor: main-task-processor |
| 21 | + fs_task_processor: main-task-processor |
| 22 | + event_thread_pool: |
| 23 | + threads: 1 |
| 24 | + task_processors: |
| 25 | + main-task-processor: |
| 26 | + worker_threads: 1 |
| 27 | + components: |
| 28 | + component: {} |
| 29 | + logging: |
| 30 | + loggers: {} |
| 31 | +)"; |
| 32 | + |
| 33 | +} // namespace |
| 34 | + |
| 35 | +TEST(ComponentsScope, Smoke) |
| 36 | +{ |
| 37 | + static bool init_called = false; |
| 38 | + static bool destroy_called = false; |
| 39 | + |
| 40 | + class ComponentWithResource final : public components::ComponentBase { |
| 41 | + public: |
| 42 | + ComponentWithResource(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 43 | + : components::ComponentBase(config, context) |
| 44 | + { |
| 45 | + context.RegisterScope(components::MakeScope([] { |
| 46 | + init_called = true; |
| 47 | + return utils::FastScopeGuard([]() noexcept { destroy_called = true; }); |
| 48 | + })); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + auto component_list = components::MinimalComponentList().Append<ComponentWithResource>("component"); |
| 53 | + components::RunOnce(components::InMemoryConfig{kConfig}, component_list); |
| 54 | + |
| 55 | + EXPECT_TRUE(init_called); |
| 56 | + EXPECT_TRUE(destroy_called); |
| 57 | +} |
| 58 | + |
| 59 | +TEST(ComponentsScope, HappyPathOrder) |
| 60 | +{ |
| 61 | + static std::vector<int> trace; |
| 62 | + |
| 63 | + class ComponentWithResource final : public components::ComponentBase { |
| 64 | + public: |
| 65 | + ComponentWithResource(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 66 | + : components::ComponentBase(config, context) |
| 67 | + { |
| 68 | + trace.push_back(0); |
| 69 | + |
| 70 | + context.RegisterScope(components::MakeScope([] { |
| 71 | + trace.push_back(1); |
| 72 | + return utils::FastScopeGuard([]() noexcept { trace.push_back(2); }); |
| 73 | + })); |
| 74 | + |
| 75 | + context.RegisterScope(components::MakeScope([] { |
| 76 | + trace.push_back(3); |
| 77 | + return utils::FastScopeGuard([]() noexcept { trace.push_back(4); }); |
| 78 | + })); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + auto component_list = components::MinimalComponentList().Append<ComponentWithResource>("component"); |
| 83 | + components::RunOnce(components::InMemoryConfig{kConfig}, component_list); |
| 84 | + |
| 85 | + EXPECT_EQ(trace, (std::vector{0, 1, 3, 4, 2})); |
| 86 | +} |
| 87 | + |
| 88 | +TEST(ComponentsScope, CtrThrow) |
| 89 | +{ |
| 90 | + static std::vector<int> trace; |
| 91 | + |
| 92 | + class ComponentWithResource final : public components::ComponentBase { |
| 93 | + public: |
| 94 | + ComponentWithResource(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 95 | + : components::ComponentBase(config, context) |
| 96 | + { |
| 97 | + trace.push_back(0); |
| 98 | + |
| 99 | + context.RegisterScope(components::MakeScope([] { |
| 100 | + trace.push_back(1); |
| 101 | + return utils::FastScopeGuard([]() noexcept { trace.push_back(2); }); |
| 102 | + })); |
| 103 | + |
| 104 | + context.RegisterScope(components::MakeScope([] { |
| 105 | + trace.push_back(3); |
| 106 | + return utils::FastScopeGuard([]() noexcept { trace.push_back(4); }); |
| 107 | + })); |
| 108 | + |
| 109 | + throw std::runtime_error("1"); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + auto component_list = components::MinimalComponentList().Append<ComponentWithResource>("component"); |
| 114 | + EXPECT_THROW(components::RunOnce(components::InMemoryConfig{kConfig}, component_list), std::runtime_error); |
| 115 | + |
| 116 | + EXPECT_EQ(trace, (std::vector{0})); |
| 117 | +} |
| 118 | + |
| 119 | +TEST(ComponentsScope, CallbackThrow) |
| 120 | +{ |
| 121 | + static std::vector<int> trace; |
| 122 | + |
| 123 | + class ComponentWithResource final : public components::ComponentBase { |
| 124 | + public: |
| 125 | + ComponentWithResource(const components::ComponentConfig& config, const components::ComponentContext& context) |
| 126 | + : components::ComponentBase(config, context) |
| 127 | + { |
| 128 | + trace.push_back(0); |
| 129 | + |
| 130 | + context.RegisterScope(components::MakeScope([] { |
| 131 | + trace.push_back(1); |
| 132 | + return utils::FastScopeGuard([]() noexcept { trace.push_back(2); }); |
| 133 | + })); |
| 134 | + |
| 135 | + context.RegisterScope(components::MakeScope([] { |
| 136 | + trace.push_back(3); |
| 137 | + throw std::runtime_error("1"); |
| 138 | + return utils::FastScopeGuard([]() noexcept { trace.push_back(4); }); |
| 139 | + })); |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + auto component_list = components::MinimalComponentList().Append<ComponentWithResource>("component"); |
| 144 | + EXPECT_THROW(components::RunOnce(components::InMemoryConfig{kConfig}, component_list), std::runtime_error); |
| 145 | + |
| 146 | + EXPECT_EQ(trace, (std::vector{0, 1, 3, 2})); |
| 147 | +} |
| 148 | + |
| 149 | +USERVER_NAMESPACE_END |
0 commit comments