• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 #include <gmock/gmock.h>
17 #include <grpc/event_engine/endpoint_config.h>
18 #include <grpcpp/support/status.h>
19 #include <gtest/gtest.h>
20 #include <unistd.h>
21 
22 #include <cstddef>
23 #include <optional>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include "absl/strings/str_cat.h"
29 #include "envoy/config/cluster/v3/cluster.pb.h"
30 #include "envoy/extensions/load_balancing_policies/pick_first/v3/pick_first.pb.h"
31 #include "src/core/client_channel/backup_poller.h"
32 #include "src/core/config/config_vars.h"
33 #include "src/core/lib/address_utils/sockaddr_utils.h"
34 #include "src/core/load_balancing/xds/xds_channel_args.h"
35 #include "src/core/resolver/fake/fake_resolver.h"
36 #include "src/core/util/env.h"
37 #include "test/core/test_util/test_config.h"
38 #include "test/cpp/end2end/connection_attempt_injector.h"
39 #include "test/cpp/end2end/xds/xds_end2end_test_lib.h"
40 
41 namespace grpc {
42 namespace testing {
43 namespace {
44 
45 using ::envoy::extensions::load_balancing_policies::pick_first::v3::PickFirst;
46 
47 class PickFirstTest : public XdsEnd2endTest {
48  protected:
49   // Sends RPCs until one of them lands on a backend in the specified range, in
50   // which case it returns the index of that backend. Returns an error status if
51   // any of the RPCs fails.
WaitForAnyBackendHit(size_t start,size_t end)52   absl::StatusOr<size_t> WaitForAnyBackendHit(size_t start, size_t end) {
53     absl::StatusOr<size_t> output;
54     SendRpcsUntil(DEBUG_LOCATION, [&](const RpcResult& result) -> bool {
55       if (!result.status.ok()) {
56         output = absl::Status(
57             static_cast<absl::StatusCode>(result.status.error_code()),
58             result.status.error_message());
59         return false;
60       }
61       for (size_t i = start; i < end; ++i) {
62         if (backends_[i]->backend_service()->request_count() > 0) {
63           backends_[i]->backend_service()->ResetCounters();
64           output = i;
65           return false;
66         }
67       }
68       return true;
69     });
70     return output;
71   }
72 };
73 
74 // Run both with and without load reporting, just for test coverage.
75 INSTANTIATE_TEST_SUITE_P(XdsTest, PickFirstTest,
76                          ::testing::Values(XdsTestType()), &XdsTestType::Name);
77 
TEST_P(PickFirstTest,PickFirstConfigurationIsPropagated)78 TEST_P(PickFirstTest, PickFirstConfigurationIsPropagated) {
79   CreateAndStartBackends(6);
80   // Change cluster to use pick_first with shuffle option.
81   auto cluster = default_cluster_;
82   PickFirst pick_first;
83   pick_first.set_shuffle_address_list(true);
84   cluster.mutable_load_balancing_policy()
85       ->add_policies()
86       ->mutable_typed_extension_config()
87       ->mutable_typed_config()
88       ->PackFrom(pick_first);
89   balancer_->ads_service()->SetCdsResource(cluster);
90   size_t start_index = 0;
91   for (size_t i = 0; i < 100; ++i) {
92     // Update EDS resource.  This will send a new address list update to the LB
93     // policy.
94     balancer_->ads_service()->SetEdsResource(BuildEdsResource(
95         EdsResourceArgs({{"locality0", CreateEndpointsForBackends(
96                                            start_index, start_index + 3)}})));
97     auto result = WaitForAnyBackendHit(start_index, start_index + 3);
98     ASSERT_TRUE(result.ok()) << result.status();
99     if (*result != start_index) return;
100     // Toggle between backends 0-2 and 3-5
101     start_index = 3 - start_index;
102   }
103   FAIL() << "did not choose a different backend";
104 }
105 }  // namespace
106 }  // namespace testing
107 }  // namespace grpc
108 
main(int argc,char ** argv)109 int main(int argc, char** argv) {
110   grpc::testing::TestEnvironment env(&argc, argv);
111   ::testing::InitGoogleTest(&argc, argv);
112   // Make the backup poller poll very frequently in order to pick up
113   // updates from all the subchannels's FDs.
114   grpc_core::ConfigVars::Overrides overrides;
115   overrides.client_channel_backup_poll_interval_ms = 1;
116   grpc_core::ConfigVars::SetOverrides(overrides);
117 #if TARGET_OS_IPHONE
118   // Workaround Apple CFStream bug
119   grpc_core::SetEnv("grpc_cfstream", "0");
120 #endif
121   grpc_init();
122   grpc::testing::ConnectionAttemptInjector::Init();
123   const auto result = RUN_ALL_TESTS();
124   grpc_shutdown();
125   return result;
126 }
127