| 1 |
// |
| 2 |
// Copyright 2017 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <stddef.h> |
| 18 |
|
| 19 |
#include <functional> |
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
#include <thread> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "absl/strings/str_format.h" |
| 26 |
#include "gtest/gtest.h" |
| 27 |
|
| 28 |
#include <grpc/grpc.h> |
| 29 |
#include <grpcpp/channel.h> |
| 30 |
#include <grpcpp/create_channel.h> |
| 31 |
#include <grpcpp/security/credentials.h> |
| 32 |
#include <grpcpp/support/channel_arguments.h> |
| 33 |
|
| 34 |
#include "src/core/lib/gprpp/env.h" |
| 35 |
#include "test/core/util/fake_udp_and_tcp_server.h" |
| 36 |
#include "test/core/util/port.h" |
| 37 |
#include "test/core/util/test_config.h" |
| 38 |
|
| 39 |
namespace { |
| 40 |
|
| 41 |
void TryConnectAndDestroy(const char* fake_metadata_server_address) { |
| 42 |
grpc::ChannelArguments args; |
| 43 |
std::string target = "google-c2p:///servername_not_used"; |
| 44 |
args.SetInt("grpc.testing.google_c2p_resolver_pretend_running_on_gcp", 1); |
| 45 |
args.SetString("grpc.testing.google_c2p_resolver_metadata_server_override", |
| 46 |
fake_metadata_server_address); |
| 47 |
auto channel = grpc::CreateCustomChannel( |
| 48 |
target, grpc::InsecureChannelCredentials(), args); |
| 49 |
// Start connecting, and give some time for the google-c2p resolver to begin |
| 50 |
// resolution and start trying to contact the metadata server. |
| 51 |
channel->GetState(true /* try_to_connect */); |
| 52 |
ASSERT_FALSE( |
| 53 |
channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(100))); |
| 54 |
channel.reset(); |
| 55 |
}; |
| 56 |
|
| 57 |
// Exercise the machinery involved with shutting down the C2P resolver while |
| 58 |
// it's waiting for its initial metadata server queries to finish. |
| 59 |
TEST(DestroyGoogleC2pChannelWithActiveConnectStressTest, |
| 60 |
LoopTryConnectAndDestroyWithHangingMetadataServer) { |
| 61 |
// Create a fake metadata server which hangs. |
| 62 |
grpc_core::testing::FakeUdpAndTcpServer fake_metadata_server( |
| 63 |
grpc_core::testing::FakeUdpAndTcpServer::AcceptMode:: |
| 64 |
kWaitForClientToSendFirstBytes, |
| 65 |
grpc_core::testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer); |
| 66 |
std::vector<std::unique_ptr<std::thread>> threads; |
| 67 |
const int kNumThreads = 10; |
| 68 |
threads.reserve(kNumThreads); |
| 69 |
for (int i = 0; i < kNumThreads; i++) { |
| 70 |
threads.emplace_back( |
| 71 |
new std::thread(TryConnectAndDestroy, fake_metadata_server.address())); |
| 72 |
} |
| 73 |
for (size_t i = 0; i < threads.size(); i++) { |
| 74 |
threads[i]->join(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Exercise the machinery involved with shutting down the C2P resolver while |
| 79 |
// it's waiting for its initial metadata server queries to finish. |
| 80 |
TEST(DestroyGoogleC2pChannelWithActiveConnectStressTest, |
| 81 |
LoopTryConnectAndDestroyWithFastFailingMetadataServer) { |
| 82 |
// Create a fake metadata server address which rejects connections |
| 83 |
int port = grpc_pick_unused_port_or_die(); |
| 84 |
std::string address = absl::StrFormat("[::1]:%d", port); |
| 85 |
std::vector<std::unique_ptr<std::thread>> threads; |
| 86 |
const int kNumThreads = 10; |
| 87 |
threads.reserve(kNumThreads); |
| 88 |
for (int i = 0; i < kNumThreads; i++) { |
| 89 |
threads.emplace_back( |
| 90 |
new std::thread(TryConnectAndDestroy, address.c_str())); |
| 91 |
} |
| 92 |
for (size_t i = 0; i < threads.size(); i++) { |
| 93 |
threads[i]->join(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
} // namespace |
| 98 |
|
| 99 |
int main(int argc, char** argv) { |
| 100 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 101 |
grpc_core::SetEnv("GRPC_EXPERIMENTAL_GOOGLE_C2P_RESOLVER", "true"); |
| 102 |
::testing::InitGoogleTest(&argc, argv); |
| 103 |
grpc_init(); |
| 104 |
auto result = RUN_ALL_TESTS(); |
| 105 |
grpc_shutdown(); |
| 106 |
return result; |
| 107 |
} |