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.
30 public:
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.
68 client::MiddlewareFactories middleware_factories);
69
70 /// Modifies the internal dynamic configs storage. It is used by the server
71 /// and clients, and is accessible through @ref GetConfigSource.
72 /// Initially, the configs are filled with compile-time defaults.
74
75 /// @returns the dynamic configs affected by @ref ExtendDynamicConfig.
76 dynamic_config::Source GetConfigSource() const;
77
78 /// @returns the statistics storage used by the server and clients.
79 utils::statistics::Storage& GetStatisticsStorage();
80
81 /// @cond
82 // For internal use only.
83 client::ClientFactory& GetClientFactory();
84
85 // For internal use only.
86 std::string GetEndpoint() const;
87 /// @endcond
88
89 private:
90 server::ServiceConfig MakeServiceConfig();
91
92 utils::statistics::Storage statistics_storage_;
93 dynamic_config::StorageMock config_storage_;
94 std::optional<std::string> unix_socket_path_;
95 server::Server server_;
96 server::Middlewares server_middlewares_;
97 client::MiddlewareFactories client_middleware_factories_;
98 bool middlewares_change_allowed_{true};
99 testsuite::GrpcControl testsuite_;
100 std::optional<std::string> endpoint_;
101 ugrpc::impl::StatisticsStorage client_statistics_storage_;
102 std::optional<client::ClientFactory> client_factory_;
103};
104
105/// @brief return list of default server middlewares for tests
107
108/// @brief return list of default client middleware factories for tests
110
111/// @brief Sets up a mini gRPC server using a single service implementation.
112/// @see @ref ugrpc::tests::ServiceBase
113template <typename GrpcService>
114class Service : public ServiceBase {
115 public:
116 /// Default-constructs the service.
117 Service() : Service(std::in_place) {}
118
119 /// Passes @a args to the service.
120 template <typename... Args>
121 explicit Service(std::in_place_t, Args&&... args)
122 : Service(server::ServerConfig{}, std::in_place,
123 std::forward<Args>(args)...) {}
124
125 /// Passes @a args to the service.
126 template <typename... Args>
127 Service(server::ServerConfig&& server_config, std::in_place_t = std::in_place,
128 Args&&... args)
129 : ServiceBase(std::move(server_config)),
130 service_(std::forward<Args>(args)...) {
131 SetServerMiddlewares(GetDefaultServerMiddlewares());
132 SetClientMiddlewareFactories(GetDefaultClientMiddlewareFactories());
133 RegisterService(service_);
135 }
136
137 ~Service() override { StopServer(); }
138
139 /// @returns the stored service.
140 GrpcService& GetService() { return service_; }
141
142 private:
143 GrpcService service_{};
144};
145
146} // namespace ugrpc::tests
147
148USERVER_NAMESPACE_END