userver: userver/ugrpc/tests/standalone_client.hpp Source File
Loading...
Searching...
No Matches
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/utils/statistics/storage.hpp>
11
12#include <userver/testsuite/grpc_control.hpp>
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
17USERVER_NAMESPACE_BEGIN
18
19namespace ugrpc::tests {
20
21/// @brief Sets up a mini gRPC client that is not directly associated with any
22/// userver gRPC server.
23///
24/// @note Prefer ugrpc::tests::ServiceBase and friends by default to create
25/// a userver gRPC service + client pair.
26class StandaloneClientFactory final {
27public:
28 /// @param client_factory_settings Options settings for the internal
29 /// ugrpc::client::ClientFactory.
30 explicit StandaloneClientFactory(client::ClientFactorySettings&& client_factory_settings = {});
31
32 /// @returns a client for the specified gRPC service, connected
33 /// to the specified endpoint.
34 /// @see GetFreeIpv6Port
35 /// @see MakeIpv6Endpoint
36 template <typename Client>
37 Client MakeClient(const std::string& endpoint) {
38 return client_factory_.MakeClient<Client>("test", endpoint);
39 }
40
41 /// @returns the internal ugrpc::client::ClientFactory.
42 client::ClientFactory& GetClientFactory() { return client_factory_; }
43
44private:
45 utils::statistics::Storage statistics_storage_;
46 ugrpc::impl::StatisticsStorage client_statistics_storage_{
47 statistics_storage_,
48 ugrpc::impl::StatisticsDomain::kClient};
49 dynamic_config::StorageMock config_storage_{dynamic_config::MakeDefaultStorage({})};
50 client::impl::CompletionQueuePool completion_queues_{1};
51 testsuite::GrpcControl testsuite_control_{{}, false};
52 client::ClientFactory client_factory_;
53};
54
55/// Acquire a free IPv6 port. Note: there is a small chance that a race could
56/// occur, and the port will be taken immediately after this function returns.
57/// Prefer spinning up a server with port auto-detection if possible.
58std::uint16_t GetFreeIpv6Port();
59
60/// Make an IPv6 localhost gRPC endpoint from port.
61std::string MakeIpv6Endpoint(std::uint16_t port);
62
63} // namespace ugrpc::tests
64
65USERVER_NAMESPACE_END