Debian Codegraph @

test/core/client_channel/lb_policy/pick_first_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
19 #include <algorithm>
20 #include <map>
21 #include <memory>
22 #include <utility>
23 #include <vector>
24
25 #include "absl/status/status.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/string_view.h"
28 #include "gtest/gtest.h"
29
30 #include <grpc/grpc.h>
31
32 #include "src/core/ext/filters/client_channel/subchannel_pool_interface.h"
33 #include "src/core/lib/channel/channel_args.h"
34 #include "src/core/lib/gprpp/orphanable.h"
35 #include "src/core/lib/iomgr/resolved_address.h"
36 #include "src/core/lib/load_balancing/lb_policy.h"
37 #include "src/core/lib/resolver/server_address.h"
38 #include "test/core/client_channel/lb_policy/lb_policy_test_lib.h"
39 #include "test/core/util/test_config.h"
40
41 namespace grpc_core {
42 namespace testing {
43 namespace {
44
45 class PickFirstTest : public LoadBalancingPolicyTest {
46 protected:
47 PickFirstTest() : lb_policy_(MakeLbPolicy("pick_first")) {}
48
49 OrphanablePtr<LoadBalancingPolicy> lb_policy_;
50 };
51
52 TEST_F(PickFirstTest, Basic) {
53 constexpr absl::string_view kAddressUri = "ipv4:127.0.0.1:443";
54 const grpc_resolved_address address = MakeAddress(kAddressUri);
55 // Send an update containing one address.
56 LoadBalancingPolicy::UpdateArgs update_args;
57 update_args.addresses.emplace();
58 update_args.addresses->emplace_back(address, ChannelArgs());
59 absl::Status status = ApplyUpdate(std::move(update_args), lb_policy_.get());
60 EXPECT_TRUE(status.ok()) << status;
61 // LB policy should have reported CONNECTING state.
62 auto picker = ExpectState(GRPC_CHANNEL_CONNECTING);
63 ExpectPickQueued(picker.get());
64 // LB policy should have created a subchannel for the address with the
65 // GRPC_ARG_INHIBIT_HEALTH_CHECKING channel arg.
66 SubchannelKey key(address,
67 ChannelArgs().Set(GRPC_ARG_INHIBIT_HEALTH_CHECKING, true));
68 auto it = subchannel_pool_.find(key);
69 ASSERT_NE(it, subchannel_pool_.end());
70 auto& subchannel_state = it->second;
71 // LB policy should have requested a connection on this subchannel.
72 EXPECT_TRUE(subchannel_state.ConnectionRequested());
73 // Tell subchannel to report CONNECTING.
74 subchannel_state.SetConnectivityState(GRPC_CHANNEL_CONNECTING,
75 absl::OkStatus());
76 // LB policy should again report CONNECTING.
77 picker = ExpectState(GRPC_CHANNEL_CONNECTING);
78 ExpectPickQueued(picker.get());
79 // Tell subchannel to report READY.
80 subchannel_state.SetConnectivityState(GRPC_CHANNEL_READY, absl::OkStatus());
81 // LB policy should report READY.
82 picker = ExpectState(GRPC_CHANNEL_READY);
83 // Picker should return the same subchannel repeatedly.
84 for (size_t i = 0; i < 3; ++i) {
85 ExpectPickComplete(picker.get(), kAddressUri);
86 }
87 }
88
89 } // namespace
90 } // namespace testing
91 } // namespace grpc_core
92
93 int main(int argc, char** argv) {
94 ::testing::InitGoogleTest(&argc, argv);
95 grpc::testing::TestEnvironment env(&argc, argv);
96 grpc_init();
97 int ret = RUN_ALL_TESTS();
98 grpc_shutdown();
99 return ret;
100 }