| 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/lib/address_utils/parse_address.h" |
| 43 |
#include "src/core/lib/channel/channel_args.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 "src/core/lib/resolver/server_address.h" |
| 48 |
#include "src/core/lib/service_config/service_config_impl.h" |
| 49 |
#include "test/core/util/port.h" |
| 50 |
#include "test/core/util/test_config.h" |
| 51 |
|
| 52 |
namespace { |
| 53 |
|
| 54 |
void TryConnectAndDestroy() { |
| 55 |
auto response_generator = |
| 56 |
grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>(); |
| 57 |
// Return a grpclb address with an IP address on the IPv6 discard prefix |
| 58 |
// (https://tools.ietf.org/html/rfc6666). This is important because |
| 59 |
// the behavior we want in this test is for a TCP connect attempt to "freeze", |
| 60 |
// i.e. we want to send SYN, and then *not* receive SYN-ACK or RST. |
| 61 |
// The precise behavior is dependant on the test runtime environment though, |
| 62 |
// since connect() attempts on this address may unfortunately result in |
| 63 |
// "network unreachable" errors in some test runtime environments. |
| 64 |
absl::StatusOr<grpc_core::URI> lb_uri = |
| 65 |
grpc_core::URI::Parse("ipv6:[0100::1234]:443"); |
| 66 |
ASSERT_TRUE(lb_uri.ok()); |
| 67 |
grpc_resolved_address address; |
| 68 |
ASSERT_TRUE(grpc_parse_uri(*lb_uri, &address)); |
| 69 |
grpc_core::ServerAddressList addresses; |
| 70 |
addresses.emplace_back(address.addr, address.len, grpc_core::ChannelArgs()); |
| 71 |
grpc_core::Resolver::Result lb_address_result; |
| 72 |
lb_address_result.service_config = grpc_core::ServiceConfigImpl::Create( |
| 73 |
grpc_core::ChannelArgs(), "{\"loadBalancingConfig\":[{\"grpclb\":{}}]}"); |
| 74 |
ASSERT_TRUE(lb_address_result.service_config.ok()) |
| 75 |
<< lb_address_result.service_config.status(); |
| 76 |
lb_address_result.args = grpc_core::SetGrpcLbBalancerAddresses( |
| 77 |
grpc_core::ChannelArgs(), addresses); |
| 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 |
|
| 102 |
TEST(DestroyGrpclbChannelWithActiveConnectStressTest, |
| 103 |
LoopTryConnectAndDestroy) { |
| 104 |
grpc_init(); |
| 105 |
std::vector<std::unique_ptr<std::thread>> threads; |
| 106 |
const int kNumThreads = 10; |
| 107 |
threads.reserve(kNumThreads); |
| 108 |
for (int i = 0; i < kNumThreads; i++) { |
| 109 |
threads.emplace_back(new std::thread(TryConnectAndDestroy)); |
| 110 |
} |
| 111 |
for (size_t i = 0; i < threads.size(); i++) { |
| 112 |
threads[i]->join(); |
| 113 |
} |
| 114 |
grpc_shutdown(); |
| 115 |
} |
| 116 |
|
| 117 |
} // namespace |
| 118 |
|
| 119 |
int main(int argc, char** argv) { |
| 120 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 121 |
::testing::InitGoogleTest(&argc, argv); |
| 122 |
auto result = RUN_ALL_TESTS(); |
| 123 |
return result; |
| 124 |
} |