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
3/// @file userver/ugrpc/tests/service.hpp
4/// @brief Base classes for testing and benchmarking ugrpc service
5/// implementations in a simplified gRPC environment.
6
7#include <memory>
8#include <utility>
9
10#include <userver/dynamic_config/snapshot.hpp>
11#include <userver/dynamic_config/storage_mock.hpp>
12#include <userver/dynamic_config/test_helpers.hpp>
13#include <userver/utils/statistics/storage.hpp>
14
15#include <userver/ugrpc/client/client_factory.hpp>
16#include <userver/ugrpc/server/server.hpp>
17#include <userver/ugrpc/server/service_base.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace ugrpc::server {
23} // namespace ugrpc::server
24
25/// userver gRPC testing facilities
26namespace ugrpc::tests {
27
28/// Sets up a mini gRPC server using the provided service implementations.
30public:
31 ServiceBase();
32
33 explicit ServiceBase(server::ServerConfig&& server_config);
34
35 ServiceBase(ServiceBase&&) = delete;
36 ServiceBase& operator=(ServiceBase&&) = delete;
37 virtual ~ServiceBase();
38
39 /// Register a gRPC service implementation. The caller owns the service and
40 /// should ensure that the services live at least until StopServer is called.
42
43 /// @overload
45
46 /// Starts the server and connects a grpc channel to it.
47 /// Should be called after the services are registered.
48 void StartServer(client::ClientFactorySettings&& settings = {});
49
50 /// Should be called before the registered services are destroyed.
51 /// Should typically be called in the destructor of your gtest fixture.
52 void StopServer() noexcept;
53
54 /// @returns a client for the specified gRPC service, connected to the server.
55 template <typename Client>
56 Client MakeClient() {
57 return GetClientFactory().MakeClient<Client>("test", *endpoint_);
58 }
59
60 /// @returns the stored @ref server::Server for advanced tweaking.
61 server::Server& GetServer() noexcept;
62
63 /// Server middlewares can be modified before the first RegisterService call.
64 void SetServerMiddlewares(server::Middlewares middlewares);
65
66 /// Client middlewares can be modified before the first RegisterService call.
67 void SetClientMiddlewareFactories(client::MiddlewareFactories middleware_factories);
68
69 /// Modifies the internal dynamic configs storage. It is used by the server
70 /// and clients, and is accessible through @ref GetConfigSource.
71 /// Initially, the configs are filled with compile-time defaults.
73
74 /// @returns the dynamic configs affected by @ref ExtendDynamicConfig.
75 dynamic_config::Source GetConfigSource() const;
76
77 /// @returns the statistics storage used by the server and clients.
78 utils::statistics::Storage& GetStatisticsStorage();
79
80 /// @cond
81 // For internal use only.
82 client::ClientFactory& GetClientFactory();
83
84 // For internal use only.
85 std::string GetEndpoint() const;
86 /// @endcond
87
88private:
89 server::ServiceConfig MakeServiceConfig();
90
91 utils::statistics::Storage statistics_storage_;
92 dynamic_config::StorageMock config_storage_;
93 std::optional<std::string> unix_socket_path_;
94 server::Server server_;
95 server::Middlewares server_middlewares_;
96 client::MiddlewareFactories client_middleware_factories_;
97 bool middlewares_change_allowed_{true};
98 testsuite::GrpcControl testsuite_;
99 std::optional<std::string> endpoint_;
100 ugrpc::impl::StatisticsStorage client_statistics_storage_;
101 std::optional<client::ClientFactory> client_factory_;
102};
103
104/// @brief return list of default server middlewares for tests
106
107/// @brief return list of default client middleware factories for tests
109
110/// @brief Sets up a mini gRPC server using a single service implementation.
111/// @see @ref ugrpc::tests::ServiceBase
112template <typename GrpcService>
113class Service : public ServiceBase {
114public:
115 /// Default-constructs the service.
116 Service() : Service(std::in_place) {}
117
118 /// Passes @a args to the service.
119 template <typename... Args>
120 explicit Service(std::in_place_t, Args&&... args)
121 : Service(server::ServerConfig{}, std::in_place, std::forward<Args>(args)...) {}
122
123 /// Passes @a args to the service.
124 template <typename... Args>
125 Service(server::ServerConfig&& server_config, std::in_place_t = std::in_place, Args&&... args)
126 : ServiceBase(std::move(server_config)), service_(std::forward<Args>(args)...) {
127 SetServerMiddlewares(GetDefaultServerMiddlewares());
128 SetClientMiddlewareFactories(GetDefaultClientMiddlewareFactories());
129 RegisterService(service_);
131 }
132
133 ~Service() override { StopServer(); }
134
135 /// @returns the stored service.
136 GrpcService& GetService() { return service_; }
137
138private:
139 GrpcService service_{};
140};
141
142} // namespace ugrpc::tests
143
144USERVER_NAMESPACE_END