Debian Codegraph @

test/core/client_channel/lb_policy/outlier_detection_test.cc

raw
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 #include <stddef.h>
18 #include <stdint.h>
19
20 #include <algorithm>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 #include "absl/status/status.h"
28 #include "absl/status/statusor.h"
29 #include "absl/strings/string_view.h"
30 #include "gtest/gtest.h"
31
32 #include <grpc/grpc.h>
33
34 #include "src/core/ext/filters/client_channel/subchannel_pool_interface.h"
35 #include "src/core/lib/channel/channel_args.h"
36 #include "src/core/lib/gprpp/orphanable.h"
37 #include "src/core/lib/gprpp/ref_counted_ptr.h"
38 #include "src/core/lib/gprpp/time.h"
39 #include "src/core/lib/iomgr/resolved_address.h"
40 #include "src/core/lib/json/json.h"
41 #include "src/core/lib/load_balancing/lb_policy.h"
42 #include "src/core/lib/resolver/server_address.h"
43 #include "test/core/client_channel/lb_policy/lb_policy_test_lib.h"
44 #include "test/core/util/test_config.h"
45
46 namespace grpc_core {
47 namespace testing {
48 namespace {
49
50 class OutlierDetectionTest : public LoadBalancingPolicyTest {
51 protected:
52 class ConfigBuilder {
53 public:
54 ConfigBuilder() {
55 SetChildPolicy(Json::Object{{"round_robin", Json::Object()}});
56 }
57
58 ConfigBuilder& SetInterval(Duration duration) {
59 json_["interval"] = duration.ToJsonString();
60 return *this;
61 }
62 ConfigBuilder& SetBaseEjectionTime(Duration duration) {
63 json_["baseEjectionTime"] = duration.ToJsonString();
64 return *this;
65 }
66 ConfigBuilder& SetMaxEjectionTime(Duration duration) {
67 json_["maxEjectionTime"] = duration.ToJsonString();
68 return *this;
69 }
70 ConfigBuilder& SetMaxEjectionPercent(uint32_t value) {
71 json_["maxEjectionPercent"] = value;
72 return *this;
73 }
74 ConfigBuilder& SetChildPolicy(Json::Object child_policy) {
75 json_["childPolicy"] = Json::Array{std::move(child_policy)};
76 return *this;
77 }
78
79 ConfigBuilder& SetSuccessRateStdevFactor(uint32_t value) {
80 GetSuccessRate()["stdevFactor"] = value;
81 return *this;
82 }
83 ConfigBuilder& SetSuccessRateEnforcementPercentage(uint32_t value) {
84 GetSuccessRate()["enforcementPercentage"] = value;
85 return *this;
86 }
87 ConfigBuilder& SetSuccessRateMinHosts(uint32_t value) {
88 GetSuccessRate()["minimumHosts"] = value;
89 return *this;
90 }
91 ConfigBuilder& SetSuccessRateRequestVolume(uint32_t value) {
92 GetSuccessRate()["requestVolume"] = value;
93 return *this;
94 }
95
96 ConfigBuilder& SetFailurePercentageThreshold(uint32_t value) {
97 GetFailurePercentage()["threshold"] = value;
98 return *this;
99 }
100 ConfigBuilder& SetFailurePercentageEnforcementPercentage(uint32_t value) {
101 GetFailurePercentage()["enforcementPercentage"] = value;
102 return *this;
103 }
104 ConfigBuilder& SetFailurePercentageMinimumHosts(uint32_t value) {
105 GetFailurePercentage()["minimumHosts"] = value;
106 return *this;
107 }
108 ConfigBuilder& SetFailurePercentageRequestVolume(uint32_t value) {
109 GetFailurePercentage()["requestVolume"] = value;
110 return *this;
111 }
112
113 RefCountedPtr<LoadBalancingPolicy::Config> Build() {
114 Json config =
115 Json::Array{Json::Object{{"outlier_detection_experimental", json_}}};
116 return MakeConfig(config);
117 }
118
119 private:
120 Json::Object& GetSuccessRate() {
121 auto it = json_.emplace("successRateEjection", Json::Object()).first;
122 return *it->second.mutable_object();
123 }
124
125 Json::Object& GetFailurePercentage() {
126 auto it =
127 json_.emplace("failurePercentageEjection", Json::Object()).first;
128 return *it->second.mutable_object();
129 }
130
131 Json::Object json_;
132 };
133
134 OutlierDetectionTest()
135 : lb_policy_(MakeLbPolicy("outlier_detection_experimental")) {}
136
137 OrphanablePtr<LoadBalancingPolicy> lb_policy_;
138 };
139
140 TEST_F(OutlierDetectionTest, Basic) {
141 constexpr absl::string_view kAddressUri = "ipv4:127.0.0.1:443";
142 const grpc_resolved_address address = MakeAddress(kAddressUri);
143 // Send an update containing one address.
144 LoadBalancingPolicy::UpdateArgs update_args;
145 update_args.config = ConfigBuilder().Build();
146 update_args.addresses.emplace();
147 update_args.addresses->emplace_back(address, ChannelArgs());
148 absl::Status status = ApplyUpdate(std::move(update_args), lb_policy_.get());
149 EXPECT_TRUE(status.ok()) << status;
150 // LB policy should have reported CONNECTING state.
151 auto picker = ExpectState(GRPC_CHANNEL_CONNECTING);
152 ExpectPickQueued(picker.get());
153 // LB policy should have created a subchannel for the address.
154 SubchannelKey key(address, ChannelArgs());
155 auto it = subchannel_pool_.find(key);
156 ASSERT_NE(it, subchannel_pool_.end());
157 auto& subchannel_state = it->second;
158 // LB policy should have requested a connection on this subchannel.
159 EXPECT_TRUE(subchannel_state.ConnectionRequested());
160 // Tell subchannel to report CONNECTING.
161 subchannel_state.SetConnectivityState(GRPC_CHANNEL_CONNECTING,
162 absl::OkStatus());
163 // LB policy should again report CONNECTING.
164 picker = ExpectState(GRPC_CHANNEL_CONNECTING);
165 ExpectPickQueued(picker.get());
166 // Tell subchannel to report READY.
167 subchannel_state.SetConnectivityState(GRPC_CHANNEL_READY, absl::OkStatus());
168 // LB policy should eventually report READY.
169 picker = WaitForConnected();
170 ASSERT_NE(picker, nullptr);
171 // Picker should return the same subchannel repeatedly.
172 for (size_t i = 0; i < 3; ++i) {
173 ExpectPickComplete(picker.get(), kAddressUri);
174 }
175 }
176
177 } // namespace
178 } // namespace testing
179 } // namespace grpc_core
180
181 int main(int argc, char** argv) {
182 ::testing::InitGoogleTest(&argc, argv);
183 grpc::testing::TestEnvironment env(&argc, argv);
184 grpc_init();
185 int ret = RUN_ALL_TESTS();
186 grpc_shutdown();
187 return ret;
188 }