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/sync.h>
37 #include <grpcpp/server.h>
38 #include <grpcpp/server_builder.h>
39
40 #include "src/core/lib/address_utils/parse_address.h"
41 #include "src/core/lib/channel/channel_args.h"
42 #include "src/core/lib/gprpp/crash.h"
43 #include "src/core/lib/gprpp/ref_counted_ptr.h"
44 #include "src/core/lib/gprpp/thd.h"
45 #include "src/core/lib/iomgr/sockaddr.h"
46 #include "src/core/load_balancing/grpclb/grpclb_balancer_addresses.h"
47 #include "src/core/resolver/endpoint_addresses.h"
48 #include "src/core/resolver/fake/fake_resolver.h"
49 #include "src/core/service_config/service_config_impl.h"
50 #include "test/core/util/port.h"
51 #include "test/core/util/test_config.h"
52
53 namespace {
54
TryConnectAndDestroy()55 void TryConnectAndDestroy() {
56 auto response_generator =
57 grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
58 // Return a grpclb address with an IP address on the IPv6 discard prefix
59 // (https://tools.ietf.org/html/rfc6666). This is important because
60 // the behavior we want in this test is for a TCP connect attempt to "freeze",
61 // i.e. we want to send SYN, and then *not* receive SYN-ACK or RST.
62 // The precise behavior is dependant on the test runtime environment though,
63 // since connect() attempts on this address may unfortunately result in
64 // "network unreachable" errors in some test runtime environments.
65 absl::StatusOr<grpc_core::URI> lb_uri =
66 grpc_core::URI::Parse("ipv6:[0100::1234]:443");
67 ASSERT_TRUE(lb_uri.ok());
68 grpc_resolved_address address;
69 ASSERT_TRUE(grpc_parse_uri(*lb_uri, &address));
70 grpc_core::EndpointAddressesList addresses;
71 addresses.emplace_back(address, grpc_core::ChannelArgs());
72 grpc_core::Resolver::Result lb_address_result;
73 lb_address_result.service_config = grpc_core::ServiceConfigImpl::Create(
74 grpc_core::ChannelArgs(), "{\"loadBalancingConfig\":[{\"grpclb\":{}}]}");
75 ASSERT_TRUE(lb_address_result.service_config.ok())
76 << lb_address_result.service_config.status();
77 lb_address_result.args = grpc_core::SetGrpcLbBalancerAddresses(
78 grpc_core::ChannelArgs(), addresses);
79 response_generator->SetResponseAsync(lb_address_result);
80 grpc::ChannelArguments args;
81 args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
82 response_generator.get());
83 // Explicitly set the connect deadline to the same amount of
84 // time as the WaitForConnected time. The goal is to get the
85 // connect timeout code to run at about the same time as when
86 // the channel gets destroyed, to try to reproduce a race.
87 args.SetInt("grpc.testing.fixed_reconnect_backoff_ms",
88 grpc_test_slowdown_factor() * 100);
89 std::ostringstream uri;
90 uri << "fake:///servername_not_used";
91 auto channel = grpc::CreateCustomChannel(
92 uri.str(), grpc::InsecureChannelCredentials(), args);
93 // Start connecting, and give some time for the TCP connection attempt to the
94 // unreachable balancer to begin. The connection should never become ready
95 // because the LB we're trying to connect to is unreachable.
96 channel->GetState(true /* try_to_connect */);
97 ASSERT_FALSE(
98 channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(100)));
99 ASSERT_EQ("grpclb", channel->GetLoadBalancingPolicyName());
100 channel.reset();
101 };
102
TEST(DestroyGrpclbChannelWithActiveConnectStressTest,LoopTryConnectAndDestroy)103 TEST(DestroyGrpclbChannelWithActiveConnectStressTest,
104 LoopTryConnectAndDestroy) {
105 grpc_init();
106 std::vector<std::unique_ptr<std::thread>> threads;
107 const int kNumThreads = 10;
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
main(int argc,char ** argv)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 }
126