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