google_c2p_resolver_test.cc
Go to the documentation of this file.
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 <atomic>
18 #include <memory>
19 #include <mutex>
20 #include <random>
21 #include <sstream>
22 #include <thread>
23 
24 #include <gmock/gmock.h>
25 
26 #include "absl/strings/str_format.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 
40 #include "src/core/lib/gpr/env.h"
41 #include "src/core/lib/gprpp/thd.h"
43 #include "test/core/util/port.h"
45 
46 namespace {
47 
48 void TryConnectAndDestroy(const char* fake_metadata_server_address) {
50  std::string target = "google-c2p-experimental:///servername_not_used";
51  args.SetInt("grpc.testing.google_c2p_resolver_pretend_running_on_gcp", 1);
52  args.SetString("grpc.testing.google_c2p_resolver_metadata_server_override",
53  fake_metadata_server_address);
56  // Start connecting, and give some time for the google-c2p resolver to begin
57  // resolution and start trying to contact the metadata server.
58  channel->GetState(true /* try_to_connect */);
60  channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(100)));
61  channel.reset();
62 };
63 
64 // Exercise the machinery involved with shutting down the C2P resolver while
65 // it's waiting for its initial metadata server queries to finish.
66 TEST(DestroyGoogleC2pChannelWithActiveConnectStressTest,
67  LoopTryConnectAndDestroyWithHangingMetadataServer) {
68  // Create a fake metadata server which hangs.
69  grpc_core::testing::FakeUdpAndTcpServer fake_metadata_server(
71  kWaitForClientToSendFirstBytes,
73  std::vector<std::unique_ptr<std::thread>> threads;
74  const int kNumThreads = 100;
75  threads.reserve(kNumThreads);
76  for (int i = 0; i < kNumThreads; i++) {
77  threads.emplace_back(
78  new std::thread(TryConnectAndDestroy, fake_metadata_server.address()));
79  }
80  for (size_t i = 0; i < threads.size(); i++) {
81  threads[i]->join();
82  }
83 }
84 
85 // Exercise the machinery involved with shutting down the C2P resolver while
86 // it's waiting for its initial metadata server queries to finish.
87 TEST(DestroyGoogleC2pChannelWithActiveConnectStressTest,
88  LoopTryConnectAndDestroyWithFastFailingMetadataServer) {
89  // Create a fake metadata server address which rejects connections
91  std::string address = absl::StrFormat("[::1]:%d", port);
92  std::vector<std::unique_ptr<std::thread>> threads;
93  const int kNumThreads = 100;
94  threads.reserve(kNumThreads);
95  for (int i = 0; i < kNumThreads; i++) {
96  threads.emplace_back(
97  new std::thread(TryConnectAndDestroy, address.c_str()));
98  }
99  for (size_t i = 0; i < threads.size(); i++) {
100  threads[i]->join();
101  }
102 }
103 
104 } // namespace
105 
106 int main(int argc, char** argv) {
107  grpc::testing::TestEnvironment env(&argc, argv);
108  gpr_setenv("GRPC_EXPERIMENTAL_GOOGLE_C2P_RESOLVER", "true");
109  ::testing::InitGoogleTest(&argc, argv);
110  grpc_init();
111  auto result = RUN_ALL_TESTS();
112  grpc_shutdown();
113  return result;
114 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
log.h
port.h
generate.env
env
Definition: generate.py:37
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
env.h
time.h
threads
static uv_thread_t * threads
Definition: threadpool.c:38
string_util.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::testing::FakeUdpAndTcpServer
Definition: fake_udp_and_tcp_server.h:72
grpc.h
channel.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::testing::FakeUdpAndTcpServer::AcceptMode
AcceptMode
Definition: fake_udp_and_tcp_server.h:79
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
fake_udp_and_tcp_server.h
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
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
kNumThreads
const int kNumThreads
Definition: thread_stress_test.cc:46
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::CreateCustomChannel
std::shared_ptr< Channel > CreateCustomChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
thd.h
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1976
grpc_core::testing::FakeUdpAndTcpServer::CloseSocketUponCloseFromPeer
static ProcessReadResult CloseSocketUponCloseFromPeer(int bytes_received_size, int read_error, int s)
Definition: fake_udp_and_tcp_server.cc:179
main
int main(int argc, char **argv)
Definition: google_c2p_resolver_test.cc:106
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_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
server_builder.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
create_channel.h
gpr_setenv
void gpr_setenv(const char *name, const char *value)


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