• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/impl/channel_arg_names.h>
18 #include <grpc/support/port_platform.h>
19 
20 #include <memory>
21 
22 #include "absl/types/optional.h"
23 #include "gtest/gtest.h"
24 #include "src/core/client_channel/subchannel.h"
25 #include "src/core/client_channel/subchannel_pool_interface.h"
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/resolver/endpoint_addresses.h"
28 #include "test/core/test_util/test_config.h"
29 
30 namespace grpc_core {
31 namespace testing {
32 namespace {
33 
TEST(MakeSubchannelArgs,UsesChannelDefaultAuthorityByDefault)34 TEST(MakeSubchannelArgs, UsesChannelDefaultAuthorityByDefault) {
35   ChannelArgs args = Subchannel::MakeSubchannelArgs(
36       ChannelArgs(), ChannelArgs(), nullptr, "foo.example.com");
37   EXPECT_EQ(args.GetString(GRPC_ARG_DEFAULT_AUTHORITY), "foo.example.com");
38 }
39 
TEST(MakeSubchannelArgs,DefaultAuthorityFromChannelArgs)40 TEST(MakeSubchannelArgs, DefaultAuthorityFromChannelArgs) {
41   ChannelArgs args = Subchannel::MakeSubchannelArgs(
42       ChannelArgs().Set(GRPC_ARG_DEFAULT_AUTHORITY, "bar.example.com"),
43       ChannelArgs(), nullptr, "foo.example.com");
44   EXPECT_EQ(args.GetString(GRPC_ARG_DEFAULT_AUTHORITY), "bar.example.com");
45 }
46 
TEST(MakeSubchannelArgs,DefaultAuthorityFromResolver)47 TEST(MakeSubchannelArgs, DefaultAuthorityFromResolver) {
48   ChannelArgs args = Subchannel::MakeSubchannelArgs(
49       ChannelArgs(),
50       ChannelArgs().Set(GRPC_ARG_DEFAULT_AUTHORITY, "bar.example.com"), nullptr,
51       "foo.example.com");
52   EXPECT_EQ(args.GetString(GRPC_ARG_DEFAULT_AUTHORITY), "bar.example.com");
53 }
54 
TEST(MakeSubchannelArgs,DefaultAuthorityFromChannelArgsOverridesValueFromResolver)55 TEST(MakeSubchannelArgs,
56      DefaultAuthorityFromChannelArgsOverridesValueFromResolver) {
57   ChannelArgs args = Subchannel::MakeSubchannelArgs(
58       ChannelArgs().Set(GRPC_ARG_DEFAULT_AUTHORITY, "bar.example.com"),
59       ChannelArgs().Set(GRPC_ARG_DEFAULT_AUTHORITY, "baz.example.com"), nullptr,
60       "foo.example.com");
61   EXPECT_EQ(args.GetString(GRPC_ARG_DEFAULT_AUTHORITY), "bar.example.com");
62 }
63 
TEST(MakeSubchannelArgs,ArgsFromChannelTrumpPerAddressArgs)64 TEST(MakeSubchannelArgs, ArgsFromChannelTrumpPerAddressArgs) {
65   ChannelArgs args = Subchannel::MakeSubchannelArgs(ChannelArgs().Set("foo", 1),
66                                                     ChannelArgs().Set("foo", 2),
67                                                     nullptr, "foo.example.com");
68   EXPECT_EQ(args.GetInt("foo"), 1);
69 }
70 
TEST(MakeSubchannelArgs,StripsOutNoSubchannelArgs)71 TEST(MakeSubchannelArgs, StripsOutNoSubchannelArgs) {
72   ChannelArgs args = Subchannel::MakeSubchannelArgs(
73       ChannelArgs().Set(GRPC_ARG_NO_SUBCHANNEL_PREFIX "foo", 1),
74       ChannelArgs().Set(GRPC_ARG_NO_SUBCHANNEL_PREFIX "bar", 1), nullptr,
75       "foo.example.com");
76   EXPECT_EQ(args.GetString(GRPC_ARG_NO_SUBCHANNEL_PREFIX "foo"), absl::nullopt);
77   EXPECT_EQ(args.GetString(GRPC_ARG_NO_SUBCHANNEL_PREFIX "bar"), absl::nullopt);
78 }
79 
80 }  // namespace
81 }  // namespace testing
82 }  // namespace grpc_core
83 
main(int argc,char ** argv)84 int main(int argc, char** argv) {
85   ::testing::InitGoogleTest(&argc, argv);
86   grpc::testing::TestEnvironment env(&argc, argv);
87   auto result = RUN_ALL_TESTS();
88   return result;
89 }
90