userver: userver/ugrpc/tests/standalone_client.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
standalone_client.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/tests/standalone_client.hpp
4/// @brief @copybrief ugrpc::tests::StandaloneClientFactory
5
6#include <string>
7
8#include <userver/dynamic_config/test_helpers.hpp>
9#include <userver/engine/io/sockaddr.hpp>
10#include <userver/testsuite/grpc_control.hpp>
11#include <userver/utils/statistics/storage.hpp>
12
13#include <userver/ugrpc/client/client_factory.hpp>
14#include <userver/ugrpc/client/impl/completion_queue_pool.hpp>
15#include <userver/ugrpc/impl/statistics_storage.hpp>
16#include <userver/ugrpc/tests/simple_client_middleware_pipeline.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace ugrpc::tests {
21
22/// @brief Sets up a mini gRPC client that is not directly associated with any
23/// userver gRPC server.
24///
25/// @note Prefer ugrpc::tests::ServiceBase and friends by default to create
26/// a userver gRPC service + client pair.
27class StandaloneClientFactory final {
28public:
29 /// @param client_factory_settings Options settings for the internal
30 /// ugrpc::client::ClientFactory.
31 explicit StandaloneClientFactory(client::ClientFactorySettings&& client_factory_settings = {});
32
33 /// @returns a client for the specified gRPC service, connected
34 /// to the specified endpoint.
35 /// @see GetFreeIpv6Port
36 /// @see MakeIpv6Endpoint
37 template <typename Client>
38 Client MakeClient(const std::string& endpoint) {
39 return client_factory_.MakeClient<Client>("test", endpoint);
40 }
41
42 /// @returns the internal ugrpc::client::ClientFactory.
43 client::ClientFactory& GetClientFactory() { return client_factory_; }
44
45private:
46 utils::statistics::Storage statistics_storage_;
47 ugrpc::impl::StatisticsStorage client_statistics_storage_{
48 statistics_storage_,
49 ugrpc::impl::StatisticsDomain::kClient};
50 dynamic_config::StorageMock config_storage_{dynamic_config::MakeDefaultStorage({})};
51 client::impl::CompletionQueuePool completion_queues_{1};
52 testsuite::GrpcControl testsuite_control_{{}, false};
53 SimpleClientMiddlewarePipeline simple_client_middleware_pipeline_;
54 // must be the last filed.
55 client::ClientFactory client_factory_;
56};
57
58/// Acquire a free IPv6 port. Note: there is a small chance that a race could
59/// occur, and the port will be taken immediately after this function returns.
60/// Prefer spinning up a server with port auto-detection if possible.
62
63/// Make an IPv6 localhost gRPC endpoint from port.
64std::string MakeIpv6Endpoint(std::uint16_t port);
65
66} // namespace ugrpc::tests
67
68USERVER_NAMESPACE_END