• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2017 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <gmock/gmock.h>
20 #include <grpc/grpc.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/string_util.h>
23 #include <grpc/support/time.h>
24 #include <grpcpp/channel.h>
25 #include <grpcpp/client_context.h>
26 #include <grpcpp/create_channel.h>
27 #include <grpcpp/impl/sync.h>
28 #include <grpcpp/server.h>
29 #include <grpcpp/server_builder.h>
30 
31 #include <atomic>
32 #include <memory>
33 #include <mutex>
34 #include <random>
35 #include <sstream>
36 #include <thread>
37 
38 #include "src/core/lib/address_utils/parse_address.h"
39 #include "src/core/lib/channel/channel_args.h"
40 #include "src/core/lib/iomgr/sockaddr.h"
41 #include "src/core/load_balancing/grpclb/grpclb_balancer_addresses.h"
42 #include "src/core/resolver/endpoint_addresses.h"
43 #include "src/core/resolver/fake/fake_resolver.h"
44 #include "src/core/service_config/service_config_impl.h"
45 #include "src/core/util/crash.h"
46 #include "src/core/util/ref_counted_ptr.h"
47 #include "src/core/util/thd.h"
48 #include "test/core/test_util/port.h"
49 #include "test/core/test_util/test_config.h"
50 
51 namespace {
52 
TryConnectAndDestroy()53 void TryConnectAndDestroy() {
54   auto response_generator =
55       grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
56   // Return a grpclb address with an IP address on the IPv6 discard prefix
57   // (https://tools.ietf.org/html/rfc6666). This is important because
58   // the behavior we want in this test is for a TCP connect attempt to "freeze",
59   // i.e. we want to send SYN, and then *not* receive SYN-ACK or RST.
60   // The precise behavior is dependant on the test runtime environment though,
61   // since connect() attempts on this address may unfortunately result in
62   // "network unreachable" errors in some test runtime environments.
63   absl::StatusOr<grpc_core::URI> lb_uri =
64       grpc_core::URI::Parse("ipv6:[0100::1234]:443");
65   ASSERT_TRUE(lb_uri.ok());
66   grpc_resolved_address address;
67   ASSERT_TRUE(grpc_parse_uri(*lb_uri, &address));
68   grpc_core::EndpointAddressesList addresses;
69   addresses.emplace_back(address, grpc_core::ChannelArgs());
70   grpc_core::Resolver::Result lb_address_result;
71   lb_address_result.service_config = grpc_core::ServiceConfigImpl::Create(
72       grpc_core::ChannelArgs(), "{\"loadBalancingConfig\":[{\"grpclb\":{}}]}");
73   ASSERT_TRUE(lb_address_result.service_config.ok())
74       << lb_address_result.service_config.status();
75   lb_address_result.args = grpc_core::SetGrpcLbBalancerAddresses(
76       grpc_core::ChannelArgs(), addresses);
77   response_generator->SetResponseAsync(lb_address_result);
78   grpc::ChannelArguments args;
79   args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
80                   response_generator.get());
81   // Explicitly set the connect deadline to the same amount of
82   // time as the WaitForConnected time. The goal is to get the
83   // connect timeout code to run at about the same time as when
84   // the channel gets destroyed, to try to reproduce a race.
85   args.SetInt("grpc.testing.fixed_reconnect_backoff_ms",
86               grpc_test_slowdown_factor() * 100);
87   std::ostringstream uri;
88   uri << "fake:///servername_not_used";
89   auto channel = grpc::CreateCustomChannel(
90       uri.str(), grpc::InsecureChannelCredentials(), args);
91   // Start connecting, and give some time for the TCP connection attempt to the
92   // unreachable balancer to begin. The connection should never become ready
93   // because the LB we're trying to connect to is unreachable.
94   channel->GetState(true /* try_to_connect */);
95   ASSERT_FALSE(
96       channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(100)));
97   ASSERT_EQ("grpclb", channel->GetLoadBalancingPolicyName());
98   channel.reset();
99 };
100 
TEST(DestroyGrpclbChannelWithActiveConnectStressTest,LoopTryConnectAndDestroy)101 TEST(DestroyGrpclbChannelWithActiveConnectStressTest,
102      LoopTryConnectAndDestroy) {
103   grpc_init();
104   std::vector<std::unique_ptr<std::thread>> threads;
105   const int kNumThreads = 10;
106   threads.reserve(kNumThreads);
107   for (int i = 0; i < kNumThreads; i++) {
108     threads.emplace_back(new std::thread(TryConnectAndDestroy));
109   }
110   for (size_t i = 0; i < threads.size(); i++) {
111     threads[i]->join();
112   }
113   grpc_shutdown();
114 }
115 
116 }  // namespace
117 
main(int argc,char ** argv)118 int main(int argc, char** argv) {
119   grpc::testing::TestEnvironment env(&argc, argv);
120   ::testing::InitGoogleTest(&argc, argv);
121   auto result = RUN_ALL_TESTS();
122   return result;
123 }
124