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