1 // 2 // Copyright 2018 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_UTIL_TEST_LB_POLICIES_H 18 #define GRPC_TEST_CORE_UTIL_TEST_LB_POLICIES_H 19 20 #include <atomic> 21 #include <functional> 22 #include <string> 23 #include <utility> 24 #include <vector> 25 26 #include "absl/status/status.h" 27 #include "absl/strings/string_view.h" 28 29 #include <grpc/support/port_platform.h> 30 31 #include "src/core/lib/config/core_configuration.h" 32 #include "src/core/load_balancing/backend_metric_data.h" 33 #include "src/core/resolver/endpoint_addresses.h" 34 35 namespace grpc_core { 36 37 using MetadataVector = std::vector<std::pair<std::string, std::string>>; 38 39 struct PickArgsSeen { 40 std::string path; 41 MetadataVector metadata; 42 }; 43 44 using TestPickArgsCallback = std::function<void(const PickArgsSeen&)>; 45 46 // Registers an LB policy called "test_pick_args_lb" that passes the args passed 47 // to SubchannelPicker::Pick() to cb. 48 void RegisterTestPickArgsLoadBalancingPolicy( 49 CoreConfiguration::Builder* builder, TestPickArgsCallback cb, 50 absl::string_view delegate_policy_name = "pick_first"); 51 52 struct TrailingMetadataArgsSeen { 53 absl::Status status; 54 const BackendMetricData* backend_metric_data; 55 MetadataVector metadata; 56 }; 57 58 using InterceptRecvTrailingMetadataCallback = 59 std::function<void(const TrailingMetadataArgsSeen&)>; 60 61 // Registers an LB policy called "intercept_trailing_metadata_lb" that 62 // invokes cb when trailing metadata is received for each call. 63 void RegisterInterceptRecvTrailingMetadataLoadBalancingPolicy( 64 CoreConfiguration::Builder* builder, 65 InterceptRecvTrailingMetadataCallback cb); 66 67 using AddressTestCallback = std::function<void(const EndpointAddresses&)>; 68 69 // Registers an LB policy called "address_test_lb" that invokes cb for each 70 // address used to create a subchannel. 71 void RegisterAddressTestLoadBalancingPolicy(CoreConfiguration::Builder* builder, 72 AddressTestCallback cb); 73 74 // Registers an LB policy called "fixed_address_lb" that provides a 75 // single subchannel whose address is in its configuration. 76 void RegisterFixedAddressLoadBalancingPolicy( 77 CoreConfiguration::Builder* builder); 78 79 using OobBackendMetricCallback = 80 std::function<void(EndpointAddresses, const BackendMetricData&)>; 81 82 // Registers an LB policy called "oob_backend_metric_test_lb" that invokes 83 // cb for each OOB backend metric report on each subchannel. 84 void RegisterOobBackendMetricTestLoadBalancingPolicy( 85 CoreConfiguration::Builder* builder, OobBackendMetricCallback cb); 86 87 // Registers an LB policy called "fail_lb" that fails all picks with the 88 // specified status. If pick_counter is non-null, it will be 89 // incremented for each pick. 90 void RegisterFailLoadBalancingPolicy(CoreConfiguration::Builder* builder, 91 absl::Status status, 92 std::atomic<int>* pick_counter = nullptr); 93 94 // Registers an LB policy called "queue_once" that queues at least one pick, and 95 // then delegates to PickFirst. 96 void RegisterQueueOnceLoadBalancingPolicy(CoreConfiguration::Builder* builder); 97 } // namespace grpc_core 98 99 #endif // GRPC_TEST_CORE_UTIL_TEST_LB_POLICIES_H 100