xref: /aosp_15_r20/external/grpc-grpc/test/core/xds/xds_client_test_peer.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_TEST_CORE_XDS_XDS_CLIENT_TEST_PEER_H
18 #define GRPC_TEST_CORE_XDS_XDS_CLIENT_TEST_PEER_H
19 
20 #include <set>
21 
22 #include "absl/functional/function_ref.h"
23 #include "absl/strings/str_cat.h"
24 
25 #include <grpc/support/port_platform.h>
26 
27 #include "src/core/ext/xds/xds_client.h"
28 
29 namespace grpc_core {
30 namespace testing {
31 
32 class XdsClientTestPeer {
33  public:
XdsClientTestPeer(XdsClient * xds_client)34   explicit XdsClientTestPeer(XdsClient* xds_client) : xds_client_(xds_client) {}
35 
TestDumpClientConfig()36   void TestDumpClientConfig() {
37     upb::Arena arena;
38     auto client_config = envoy_service_status_v3_ClientConfig_new(arena.ptr());
39     std::set<std::string> string_pool;
40     MutexLock lock(xds_client_->mu());
41     xds_client_->DumpClientConfig(&string_pool, arena.ptr(), client_config);
42   }
43 
44   struct ResourceCountLabels {
45     std::string xds_authority;
46     std::string resource_type;
47     std::string cache_state;
48 
ToStringResourceCountLabels49     std::string ToString() const {
50       return absl::StrCat("xds_authority=\"", xds_authority,
51                           "\" resource_type=\"", resource_type,
52                           "\" cache_state=\"", cache_state, "\"");
53     }
54   };
TestReportResourceCounts(absl::FunctionRef<void (const ResourceCountLabels &,uint64_t)> func)55   void TestReportResourceCounts(
56       absl::FunctionRef<void(const ResourceCountLabels&, uint64_t)> func) {
57     MutexLock lock(xds_client_->mu());
58     xds_client_->ReportResourceCounts(
59         [&](const XdsClient::ResourceCountLabels& labels, uint64_t count) {
60           ResourceCountLabels labels_copy = {std::string(labels.xds_authority),
61                                              std::string(labels.resource_type),
62                                              std::string(labels.cache_state)};
63           func(labels_copy, count);
64         });
65   }
66 
TestReportServerConnections(absl::FunctionRef<void (absl::string_view,bool)> func)67   void TestReportServerConnections(
68       absl::FunctionRef<void(absl::string_view, bool)> func) {
69     MutexLock lock(xds_client_->mu());
70     xds_client_->ReportServerConnections(func);
71   }
72 
73  private:
74   XdsClient* xds_client_;
75 };
76 
77 }  // namespace testing
78 }  // namespace grpc_core
79 
80 #endif  // GRPC_TEST_CORE_XDS_XDS_CLIENT_TEST_PEER_H
81