• 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 <atomic>
20 #include <memory>
21 #include <mutex>
22 #include <random>
23 #include <sstream>
24 #include <thread>
25 
26 #include <gmock/gmock.h>
27 
28 #include <grpc/grpc.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/string_util.h>
32 #include <grpc/support/time.h>
33 #include <grpcpp/channel.h>
34 #include <grpcpp/client_context.h>
35 #include <grpcpp/create_channel.h>
36 #include <grpcpp/impl/codegen/sync.h>
37 #include <grpcpp/server.h>
38 #include <grpcpp/server_builder.h>
39 
40 #include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h"
41 #include "src/core/ext/filters/client_channel/parse_address.h"
42 #include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
43 #include "src/core/ext/filters/client_channel/server_address.h"
44 #include "src/core/lib/gprpp/ref_counted_ptr.h"
45 #include "src/core/lib/gprpp/thd.h"
46 #include "src/core/lib/iomgr/sockaddr.h"
47 
48 #include "test/core/util/port.h"
49 #include "test/core/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 "hang",
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   const char* uri_str = "ipv6:[0100::1234]:443";
64   grpc_uri* lb_uri = grpc_uri_parse(uri_str, true);
65   ASSERT_NE(lb_uri, nullptr);
66   grpc_resolved_address address;
67   ASSERT_TRUE(grpc_parse_uri(lb_uri, &address));
68   grpc_uri_destroy(lb_uri);
69   grpc_core::ServerAddressList addresses;
70   addresses.emplace_back(address.addr, address.len, nullptr);
71   grpc_core::Resolver::Result lb_address_result;
72   grpc_error* error = GRPC_ERROR_NONE;
73   lb_address_result.service_config = grpc_core::ServiceConfig::Create(
74       "{\"loadBalancingConfig\":[{\"grpclb\":{}}]}", &error);
75   ASSERT_EQ(error, GRPC_ERROR_NONE) << grpc_error_string(error);
76   grpc_arg arg = grpc_core::CreateGrpclbBalancerAddressesArg(&addresses);
77   lb_address_result.args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
78   response_generator->SetResponse(lb_address_result);
79   grpc::ChannelArguments args;
80   args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
81                   response_generator.get());
82   // Explicitly set the connect deadline to the same amount of
83   // time as the WaitForConnected time. The goal is to get the
84   // connect timeout code to run at about the same time as when
85   // the channel gets destroyed, to try to reproduce a race.
86   args.SetInt("grpc.testing.fixed_reconnect_backoff_ms",
87               grpc_test_slowdown_factor() * 100);
88   std::ostringstream uri;
89   uri << "fake:///servername_not_used";
90   auto channel = ::grpc::CreateCustomChannel(
91       uri.str(), grpc::InsecureChannelCredentials(), args);
92   // Start connecting, and give some time for the TCP connection attempt to the
93   // unreachable balancer to begin. The connection should never become ready
94   // because the LB we're trying to connect to is unreachable.
95   channel->GetState(true /* try_to_connect */);
96   ASSERT_FALSE(
97       channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(100)));
98   ASSERT_EQ("grpclb", channel->GetLoadBalancingPolicyName());
99   channel.reset();
100 };
101 
TEST(DestroyGrpclbChannelWithActiveConnectStressTest,LoopTryConnectAndDestroy)102 TEST(DestroyGrpclbChannelWithActiveConnectStressTest,
103      LoopTryConnectAndDestroy) {
104   grpc_init();
105   std::vector<std::unique_ptr<std::thread>> threads;
106   // 100 is picked for number of threads just
107   // because it's enough to reproduce a certain crash almost 100%
108   // at this time of writing.
109   const int kNumThreads = 100;
110   threads.reserve(kNumThreads);
111   for (int i = 0; i < kNumThreads; i++) {
112     threads.emplace_back(new std::thread(TryConnectAndDestroy));
113   }
114   for (int i = 0; i < threads.size(); i++) {
115     threads[i]->join();
116   }
117   grpc_shutdown();
118 }
119 
120 }  // namespace
121 
main(int argc,char ** argv)122 int main(int argc, char** argv) {
123   grpc::testing::TestEnvironment env(argc, argv);
124   ::testing::InitGoogleTest(&argc, argv);
125   auto result = RUN_ALL_TESTS();
126   return result;
127 }
128