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 <grpc/support/port_platform.h> 21 22 #include <set> 23 24 #include "absl/functional/function_ref.h" 25 #include "absl/strings/str_cat.h" 26 #include "src/core/xds/xds_client/xds_client.h" 27 28 namespace grpc_core { 29 namespace testing { 30 31 class XdsClientTestPeer { 32 public: XdsClientTestPeer(XdsClient * xds_client)33 explicit XdsClientTestPeer(XdsClient* xds_client) : xds_client_(xds_client) {} 34 TestDumpClientConfig()35 std::string TestDumpClientConfig() { 36 upb::Arena arena; 37 auto* client_config = envoy_service_status_v3_ClientConfig_new(arena.ptr()); 38 std::set<std::string> string_pool; 39 MutexLock lock(xds_client_->mu()); 40 xds_client_->DumpClientConfig(&string_pool, arena.ptr(), client_config); 41 size_t output_length; 42 char* output = envoy_service_status_v3_ClientConfig_serialize( 43 client_config, arena.ptr(), &output_length); 44 return std::string(output, output_length); 45 } 46 47 struct ResourceCountLabels { 48 std::string xds_authority; 49 std::string resource_type; 50 std::string cache_state; 51 ToStringResourceCountLabels52 std::string ToString() const { 53 return absl::StrCat("xds_authority=\"", xds_authority, 54 "\" resource_type=\"", resource_type, 55 "\" cache_state=\"", cache_state, "\""); 56 } 57 }; TestReportResourceCounts(absl::FunctionRef<void (const ResourceCountLabels &,uint64_t)> func)58 void TestReportResourceCounts( 59 absl::FunctionRef<void(const ResourceCountLabels&, uint64_t)> func) { 60 MutexLock lock(xds_client_->mu()); 61 xds_client_->ReportResourceCounts( 62 [&](const XdsClient::ResourceCountLabels& labels, uint64_t count) { 63 ResourceCountLabels labels_copy = {std::string(labels.xds_authority), 64 std::string(labels.resource_type), 65 std::string(labels.cache_state)}; 66 func(labels_copy, count); 67 }); 68 } 69 TestReportServerConnections(absl::FunctionRef<void (absl::string_view,bool)> func)70 void TestReportServerConnections( 71 absl::FunctionRef<void(absl::string_view, bool)> func) { 72 MutexLock lock(xds_client_->mu()); 73 xds_client_->ReportServerConnections(func); 74 } 75 76 private: 77 XdsClient* xds_client_; 78 }; 79 80 } // namespace testing 81 } // namespace grpc_core 82 83 #endif // GRPC_TEST_CORE_XDS_XDS_CLIENT_TEST_PEER_H 84