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 {
27 public:
28 /// @param client_factory_settings Options settings for the internal
29 /// ugrpc::client::ClientFactory.
31 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
45 private:
46 utils::statistics::Storage statistics_storage_;
47 ugrpc::impl::StatisticsStorage client_statistics_storage_{
48 statistics_storage_, ugrpc::impl::StatisticsDomain::kClient};
49 dynamic_config::StorageMock config_storage_{
50 dynamic_config::MakeDefaultStorage({})};
51 client::impl::CompletionQueuePool completion_queues_{1};
52 testsuite::GrpcControl testsuite_control_{{}, false};
53 client::ClientFactory client_factory_;
54};
55
56/// Acquire a free IPv6 port. Note: there is a small chance that a race could
57/// occur, and the port will be taken immediately after this function returns.
58/// Prefer spinning up a server with port auto-detection if possible.
59std::uint16_t GetFreeIpv6Port();
60
61/// Make an IPv6 localhost gRPC endpoint from port.
62std::string MakeIpv6Endpoint(std::uint16_t port);
63
64} // namespace ugrpc::tests
65
66USERVER_NAMESPACE_END