userver: userver/ugrpc/tests/service.hpp Source File
Loading...
Searching...
No Matches
service.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <optional>
8#include <utility>
9#include <vector>
10
18
20#include <userver/ugrpc/client/impl/completion_queue_pool.hpp>
21#include <userver/ugrpc/impl/statistics_storage.hpp>
25
26USERVER_NAMESPACE_BEGIN
27
28namespace ugrpc::server {
30} // namespace ugrpc::server
31
33namespace ugrpc::tests {
34
36class ServiceBase {
37public:
38 ServiceBase();
39
40 explicit ServiceBase(server::ServerConfig&& server_config);
41
42 ServiceBase(ServiceBase&&) = delete;
43 ServiceBase& operator=(ServiceBase&&) = delete;
44 virtual ~ServiceBase();
45
49
52
56
59 void StopServer() noexcept;
60
62 template <typename Client>
63 Client MakeClient() {
64 return GetClientFactory().MakeClient<Client>("test", *endpoint_);
65 }
66
69
72
74 void SetUseProtobufArena(bool enabled);
75
78
82 void ExtendDynamicConfig(const std::vector<dynamic_config::KeyValue>&);
83
85 dynamic_config::Source GetConfigSource() const;
86
88 utils::statistics::Storage& GetStatisticsStorage();
89
91 // For internal use only.
92 client::ClientFactory& GetClientFactory();
93
94 // For internal use only.
95 std::string GetEndpoint() const;
97
98private:
99 server::ServiceConfig MakeServiceConfig();
100
101 utils::statistics::Storage statistics_storage_;
102 utils::statistics::MetricsStoragePtr metrics_storage_;
103 dynamic_config::StorageMock config_storage_;
104 std::optional<std::string> unix_socket_path_;
105 utils::WithResourceScopes<server::Server> server_;
106 server::Middlewares server_middlewares_;
107 bool use_protobuf_arena_{false};
108 SimpleClientMiddlewarePipeline simple_client_middleware_pipeline_;
109 bool middlewares_change_allowed_{true};
110 testsuite::GrpcControl testsuite_;
111 std::optional<std::string> endpoint_;
113 std::optional<client::impl::CompletionQueuePool> client_completion_queues_;
114 std::optional<client::ClientFactory> client_factory_;
115};
116
119
122
125 server::ServerConfig server_config{};
126 client::ClientFactorySettings client_factory_settings{};
129 bool use_protobuf_arena{false};
130};
131
134template <typename GrpcService>
135class Service : public ServiceBase {
136public:
138 template <typename... Args>
139 explicit Service(std::in_place_t = std::in_place, Args&&... args)
140 : Service(ServiceConfigs{}, std::in_place, std::forward<Args>(args)...)
141 {}
142
144 template <typename... Args>
145 explicit Service(ServiceConfigs&& configs, std::in_place_t = std::in_place, Args&&... args)
146 : ServiceBase(std::move(configs.server_config)),
147 service_(std::forward<Args>(args)...)
148 {
149 SetServerMiddlewares(std::move(configs.server_middlewares));
150 SetClientMiddlewares(std::move(configs.client_middlewares));
151 SetUseProtobufArena(configs.use_protobuf_arena);
152 RegisterService(service_);
153 StartServer(std::move(configs.client_factory_settings));
154 }
155
156 ~Service() override { StopServer(); }
157
159 GrpcService& GetService() { return service_; }
160
161private:
162 GrpcService service_{};
163};
164
165} // namespace ugrpc::tests
166
167USERVER_NAMESPACE_END