destroy_grpclb_channel_with_active_connect_stress_test.cc
Go to the documentation of this file.
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>
32 #include <grpc/support/time.h>
33 #include <grpcpp/channel.h>
34 #include <grpcpp/client_context.h>
35 #include <grpcpp/create_channel.h>
37 #include <grpcpp/server.h>
38 #include <grpcpp/server_builder.h>
39 
44 #include "src/core/lib/gprpp/thd.h"
48 #include "test/core/util/port.h"
50 
51 namespace {
52 
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.
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));
69  addresses.emplace_back(address.addr, address.len, nullptr);
70  grpc_core::Resolver::Result lb_address_result;
73  nullptr, "{\"loadBalancingConfig\":[{\"grpclb\":{}}]}", &error);
76  lb_address_result.args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
77  response_generator->SetResponse(lb_address_result);
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",
87  std::ostringstream uri;
88  uri << "fake:///servername_not_used";
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 */);
96  channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(100)));
97  ASSERT_EQ("grpclb", channel->GetLoadBalancingPolicyName());
98  channel.reset();
99 };
100 
101 TEST(DestroyGrpclbChannelWithActiveConnectStressTest,
102  LoopTryConnectAndDestroy) {
103  grpc_init();
104  std::vector<std::unique_ptr<std::thread>> threads;
105  // 100 is picked for number of threads just
106  // because it's enough to reproduce a certain crash almost 100%
107  // at this time of writing.
108  const int kNumThreads = 100;
109  threads.reserve(kNumThreads);
110  for (int i = 0; i < kNumThreads; i++) {
111  threads.emplace_back(new std::thread(TryConnectAndDestroy));
112  }
113  for (size_t i = 0; i < threads.size(); i++) {
114  threads[i]->join();
115  }
116  grpc_shutdown();
117 }
118 
119 } // namespace
120 
121 int main(int argc, char** argv) {
122  grpc::testing::TestEnvironment env(&argc, argv);
123  ::testing::InitGoogleTest(&argc, argv);
124  auto result = RUN_ALL_TESTS();
125  return result;
126 }
grpc_arg
Definition: grpc_types.h:103
grpc_core::Resolver::Result::args
const grpc_channel_args * args
Definition: resolver/resolver.h:70
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
port.h
generate.env
env
Definition: generate.py:37
error
grpc_error_handle error
Definition: retry_filter.cc:499
fake_resolver.h
grpc_resolved_address
Definition: resolved_address.h:34
grpc_core::Resolver::Result::service_config
absl::StatusOr< RefCountedPtr< ServiceConfig > > service_config
A service config, or an error.
Definition: resolver/resolver.h:60
time.h
grpc_core::URI::Parse
static absl::StatusOr< URI > Parse(absl::string_view uri_text)
Definition: uri_parser.cc:209
sockaddr.h
threads
static uv_thread_t * threads
Definition: threadpool.c:38
grpc_core::CreateGrpclbBalancerAddressesArg
grpc_arg CreateGrpclbBalancerAddressesArg(const ServerAddressList *address_list)
Definition: grpclb_balancer_addresses.cc:66
grpc_parse_uri
bool grpc_parse_uri(const grpc_core::URI &uri, grpc_resolved_address *resolved_addr)
Definition: parse_address.cc:293
string_util.h
parse_address.h
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
sync.h
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
grpc_timeout_milliseconds_to_deadline
gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms)
Definition: test/core/util/test_config.cc:89
grpc_core::Resolver::Result
Results returned by the resolver.
Definition: resolver/resolver.h:56
GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR
#define GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR
Definition: fake_resolver.h:31
grpc.h
grpc_test_slowdown_factor
int64_t grpc_test_slowdown_factor()
Definition: test/core/util/test_config.cc:76
channel.h
arg
Definition: cmdline.cc:40
server_address.h
grpc_resolved_address::len
socklen_t len
Definition: resolved_address.h:36
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::ServerAddressList
std::vector< ServerAddress > ServerAddressList
Definition: server_address.h:120
grpc_core::ServiceConfigImpl::Create
static RefCountedPtr< ServiceConfig > Create(const grpc_channel_args *args, absl::string_view json_string, grpc_error_handle *error)
Definition: service_config_impl.cc:41
grpclb_balancer_addresses.h
test_config.h
absl::StatusOr::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: abseil-cpp/absl/status/statusor.h:491
grpc::ChannelArguments
Definition: grpcpp/support/channel_arguments.h:39
client_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
kNumThreads
const int kNumThreads
Definition: thread_stress_test.cc:46
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
service_config_impl.h
grpc::CreateCustomChannel
std::shared_ptr< Channel > CreateCustomChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
thd.h
main
int main(int argc, char **argv)
Definition: destroy_grpclb_channel_with_active_connect_stress_test.cc:121
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1976
ref_counted_ptr.h
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
server.h
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
grpc_resolved_address::addr
char addr[GRPC_MAX_SOCKADDR_SIZE]
Definition: resolved_address.h:35
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
server_builder.h
grpc_channel_args_copy_and_add
grpc_channel_args * grpc_channel_args_copy_and_add(const grpc_channel_args *src, const grpc_arg *to_add, size_t num_to_add)
Definition: channel_args.cc:224
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
create_channel.h
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:16