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