Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / test / cpp / client / destroy_grpclb_channel_with_active_connect_stress_test.cc
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/resolver/fake/fake_resolver.h"
42 #include "src/core/ext/filters/client_channel/server_address.h"
43 #include "src/core/lib/address_utils/parse_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 #include "test/core/util/port.h"
48 #include "test/core/util/test_config.h"
49
50 namespace {
51
52 void TryConnectAndDestroy() {
53   auto response_generator =
54       grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
55   // Return a grpclb address with an IP address on the IPv6 discard prefix
56   // (https://tools.ietf.org/html/rfc6666). This is important because
57   // the behavior we want in this test is for a TCP connect attempt to "freeze",
58   // i.e. we want to send SYN, and then *not* receive SYN-ACK or RST.
59   // The precise behavior is dependant on the test runtime environment though,
60   // since connect() attempts on this address may unfortunately result in
61   // "network unreachable" errors in some test runtime environments.
62   absl::StatusOr<grpc_core::URI> lb_uri =
63       grpc_core::URI::Parse("ipv6:[0100::1234]:443");
64   ASSERT_TRUE(lb_uri.ok());
65   grpc_resolved_address address;
66   ASSERT_TRUE(grpc_parse_uri(*lb_uri, &address));
67   grpc_core::ServerAddressList addresses;
68   addresses.emplace_back(address.addr, address.len, nullptr);
69   grpc_core::Resolver::Result lb_address_result;
70   grpc_error_handle error = GRPC_ERROR_NONE;
71   lb_address_result.service_config = grpc_core::ServiceConfig::Create(
72       nullptr, "{\"loadBalancingConfig\":[{\"grpclb\":{}}]}", &error);
73   ASSERT_EQ(error, GRPC_ERROR_NONE) << grpc_error_std_string(error);
74   grpc_arg arg = grpc_core::CreateGrpclbBalancerAddressesArg(&addresses);
75   lb_address_result.args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
76   response_generator->SetResponse(lb_address_result);
77   grpc::ChannelArguments args;
78   args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
79                   response_generator.get());
80   // Explicitly set the connect deadline to the same amount of
81   // time as the WaitForConnected time. The goal is to get the
82   // connect timeout code to run at about the same time as when
83   // the channel gets destroyed, to try to reproduce a race.
84   args.SetInt("grpc.testing.fixed_reconnect_backoff_ms",
85               grpc_test_slowdown_factor() * 100);
86   std::ostringstream uri;
87   uri << "fake:///servername_not_used";
88   auto channel = ::grpc::CreateCustomChannel(
89       uri.str(), grpc::InsecureChannelCredentials(), args);
90   // Start connecting, and give some time for the TCP connection attempt to the
91   // unreachable balancer to begin. The connection should never become ready
92   // because the LB we're trying to connect to is unreachable.
93   channel->GetState(true /* try_to_connect */);
94   ASSERT_FALSE(
95       channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(100)));
96   ASSERT_EQ("grpclb", channel->GetLoadBalancingPolicyName());
97   channel.reset();
98 };
99
100 TEST(DestroyGrpclbChannelWithActiveConnectStressTest,
101      LoopTryConnectAndDestroy) {
102   grpc_init();
103   std::vector<std::unique_ptr<std::thread>> threads;
104   // 100 is picked for number of threads just
105   // because it's enough to reproduce a certain crash almost 100%
106   // at this time of writing.
107   const int kNumThreads = 100;
108   threads.reserve(kNumThreads);
109   for (int i = 0; i < kNumThreads; i++) {
110     threads.emplace_back(new std::thread(TryConnectAndDestroy));
111   }
112   for (size_t i = 0; i < threads.size(); i++) {
113     threads[i]->join();
114   }
115   grpc_shutdown();
116 }
117
118 }  // namespace
119
120 int main(int argc, char** argv) {
121   grpc::testing::TestEnvironment env(argc, argv);
122   ::testing::InitGoogleTest(&argc, argv);
123   auto result = RUN_ALL_TESTS();
124   return result;
125 }