3 * Copyright 2019 The gRPC Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "src/core/lib/iomgr/port.h"
27 #include <grpc/grpc.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/atm.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/health_check_service_interface.h>
37 #include <grpcpp/server.h>
38 #include <grpcpp/server_builder.h>
39 #include <gtest/gtest.h>
41 #include "src/core/lib/backoff/backoff.h"
42 #include "src/core/lib/gpr/env.h"
44 #include "src/proto/grpc/testing/echo.grpc.pb.h"
45 #include "test/core/util/port.h"
46 #include "test/core/util/test_config.h"
47 #include "test/cpp/end2end/test_service_impl.h"
48 #include "test/cpp/util/test_credentials_provider.h"
51 using grpc::ClientAsyncResponseReader;
52 using grpc::testing::EchoRequest;
53 using grpc::testing::EchoResponse;
54 using grpc::testing::RequestParams;
55 using std::chrono::system_clock;
62 TestScenario(const std::string& creds_type, const std::string& content)
63 : credentials_type(creds_type), message_content(content) {}
64 const std::string credentials_type;
65 const std::string message_content;
68 class CFStreamTest : public ::testing::TestWithParam<TestScenario> {
71 : server_host_("grpctest"),
73 ipv4_address_("10.0.0.1") {}
76 std::ostringstream cmd;
77 // Add DNS entry for server_host_ in /etc/hosts
78 cmd << "echo '" << ipv4_address_ << " " << server_host_
79 << " ' | sudo tee -a /etc/hosts";
80 std::system(cmd.str().c_str());
84 std::ostringstream cmd;
85 // Remove DNS entry for server_host_ in /etc/hosts
86 cmd << "sudo sed -i '.bak' '/" << server_host_ << "/d' /etc/hosts";
87 std::system(cmd.str().c_str());
91 std::ostringstream cmd;
92 cmd << "sudo /sbin/ifconfig " << interface_ << " alias " << ipv4_address_;
93 std::system(cmd.str().c_str());
96 void InterfaceDown() {
97 std::ostringstream cmd;
98 cmd << "sudo /sbin/ifconfig " << interface_ << " -alias " << ipv4_address_;
99 std::system(cmd.str().c_str());
103 gpr_log(GPR_DEBUG, "Bringing network up");
109 gpr_log(GPR_DEBUG, "Bringing network down");
114 void SetUp() override {
120 void TearDown() override {
127 port_ = grpc_pick_unused_port_or_die();
128 server_.reset(new ServerData(port_, GetParam().credentials_type));
129 server_->Start(server_host_);
131 void StopServer() { server_->Shutdown(); }
133 std::unique_ptr<grpc::testing::EchoTestService::Stub> BuildStub(
134 const std::shared_ptr<Channel>& channel) {
135 return grpc::testing::EchoTestService::NewStub(channel);
138 std::shared_ptr<Channel> BuildChannel() {
139 std::ostringstream server_address;
140 server_address << server_host_ << ":" << port_;
141 ChannelArguments args;
142 auto channel_creds = GetCredentialsProvider()->GetChannelCredentials(
143 GetParam().credentials_type, &args);
144 return CreateCustomChannel(server_address.str(), channel_creds, args);
148 const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub,
149 bool expect_success = false) {
150 auto response = std::unique_ptr<EchoResponse>(new EchoResponse());
152 auto& msg = GetParam().message_content;
153 request.set_message(msg);
154 ClientContext context;
155 Status status = stub->Echo(&context, request, response.get());
157 gpr_log(GPR_DEBUG, "RPC with succeeded");
158 EXPECT_EQ(msg, response->message());
160 gpr_log(GPR_DEBUG, "RPC failed: %s", status.error_message().c_str());
162 if (expect_success) {
163 EXPECT_TRUE(status.ok());
167 const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub,
168 RequestParams param = RequestParams()) {
170 request.set_message(GetParam().message_content);
171 *request.mutable_param() = std::move(param);
172 AsyncClientCall* call = new AsyncClientCall;
174 call->response_reader =
175 stub->PrepareAsyncEcho(&call->context, request, &cq_);
177 call->response_reader->StartCall();
178 call->response_reader->Finish(&call->reply, &call->status, (void*)call);
181 void ShutdownCQ() { cq_.Shutdown(); }
183 bool CQNext(void** tag, bool* ok) {
184 auto deadline = std::chrono::system_clock::now() + std::chrono::seconds(10);
185 auto ret = cq_.AsyncNext(tag, ok, deadline);
186 if (ret == grpc::CompletionQueue::GOT_EVENT) {
188 } else if (ret == grpc::CompletionQueue::SHUTDOWN) {
191 GPR_ASSERT(ret == grpc::CompletionQueue::TIMEOUT);
192 // This can happen if we hit the Apple CFStream bug which results in the
193 // read stream freezing. We are ignoring hangs and timeouts, but these
194 // tests are still useful as they can catch memory memory corruptions,
195 // crashes and other bugs that don't result in test freeze/timeout.
200 bool WaitForChannelNotReady(Channel* channel, int timeout_seconds = 5) {
201 const gpr_timespec deadline =
202 grpc_timeout_seconds_to_deadline(timeout_seconds);
203 grpc_connectivity_state state;
204 while ((state = channel->GetState(false /* try_to_connect */)) ==
205 GRPC_CHANNEL_READY) {
206 if (!channel->WaitForStateChange(state, deadline)) return false;
211 bool WaitForChannelReady(Channel* channel, int timeout_seconds = 10) {
212 const gpr_timespec deadline =
213 grpc_timeout_seconds_to_deadline(timeout_seconds);
214 grpc_connectivity_state state;
215 while ((state = channel->GetState(true /* try_to_connect */)) !=
216 GRPC_CHANNEL_READY) {
217 if (!channel->WaitForStateChange(state, deadline)) return false;
222 struct AsyncClientCall {
224 ClientContext context;
226 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader;
232 const std::string creds_;
233 std::unique_ptr<Server> server_;
234 TestServiceImpl service_;
235 std::unique_ptr<std::thread> thread_;
236 bool server_ready_ = false;
238 ServerData(int port, const std::string& creds)
239 : port_(port), creds_(creds) {}
241 void Start(const std::string& server_host) {
242 gpr_log(GPR_INFO, "starting server on port %d", port_);
244 std::unique_lock<std::mutex> lock(mu);
245 std::condition_variable cond;
246 thread_.reset(new std::thread(
247 std::bind(&ServerData::Serve, this, server_host, &mu, &cond)));
248 cond.wait(lock, [this] { return server_ready_; });
249 server_ready_ = false;
250 gpr_log(GPR_INFO, "server startup complete");
253 void Serve(const std::string& server_host, std::mutex* mu,
254 std::condition_variable* cond) {
255 std::ostringstream server_address;
256 server_address << server_host << ":" << port_;
257 ServerBuilder builder;
259 GetCredentialsProvider()->GetServerCredentials(creds_);
260 builder.AddListeningPort(server_address.str(), server_creds);
261 builder.RegisterService(&service_);
262 server_ = builder.BuildAndStart();
263 std::lock_guard<std::mutex> lock(*mu);
264 server_ready_ = true;
268 void Shutdown(bool join = true) {
269 server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0));
270 if (join) thread_->join();
275 const std::string server_host_;
276 const std::string interface_;
277 const std::string ipv4_address_;
278 std::unique_ptr<ServerData> server_;
282 std::vector<TestScenario> CreateTestScenarios() {
283 std::vector<TestScenario> scenarios;
284 std::vector<std::string> credentials_types;
285 std::vector<std::string> messages;
287 credentials_types.push_back(kInsecureCredentialsType);
288 auto sec_list = GetCredentialsProvider()->GetSecureCredentialsTypeList();
289 for (auto sec = sec_list.begin(); sec != sec_list.end(); sec++) {
290 credentials_types.push_back(*sec);
293 messages.push_back("🖖");
294 for (size_t k = 1; k < GRPC_DEFAULT_MAX_RECV_MESSAGE_LENGTH / 1024; k *= 32) {
296 for (size_t i = 0; i < k * 1024; ++i) {
297 char c = 'a' + (i % 26);
300 messages.push_back(big_msg);
302 for (auto cred = credentials_types.begin(); cred != credentials_types.end();
304 for (auto msg = messages.begin(); msg != messages.end(); msg++) {
305 scenarios.emplace_back(*cred, *msg);
312 INSTANTIATE_TEST_SUITE_P(CFStreamTest, CFStreamTest,
313 ::testing::ValuesIn(CreateTestScenarios()));
315 // gRPC should automatically detech network flaps (without enabling keepalives)
316 // when CFStream is enabled
317 TEST_P(CFStreamTest, NetworkTransition) {
318 auto channel = BuildChannel();
319 auto stub = BuildStub(channel);
320 // Channel should be in READY state after we send an RPC
321 SendRpc(stub, /*expect_success=*/true);
322 EXPECT_EQ(channel->GetState(false), GRPC_CHANNEL_READY);
324 std::atomic_bool shutdown{false};
325 std::thread sender = std::thread([this, &stub, &shutdown]() {
327 if (shutdown.load()) {
331 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
335 // bring down network
338 // network going down should be detected by cfstream
339 EXPECT_TRUE(WaitForChannelNotReady(channel.get()));
341 // bring network interface back up
342 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
345 // channel should reconnect
346 EXPECT_TRUE(WaitForChannelReady(channel.get()));
347 EXPECT_EQ(channel->GetState(false), GRPC_CHANNEL_READY);
348 shutdown.store(true);
352 // Network flaps while RPCs are in flight
353 TEST_P(CFStreamTest, NetworkFlapRpcsInFlight) {
354 auto channel = BuildChannel();
355 auto stub = BuildStub(channel);
356 std::atomic_int rpcs_sent{0};
358 // Channel should be in READY state after we send some RPCs
359 for (int i = 0; i < 10; ++i) {
361 param.set_skip_cancelled_check(true);
362 SendAsyncRpc(stub, param);
365 EXPECT_TRUE(WaitForChannelReady(channel.get()));
367 // Bring down the network
370 std::thread thd = std::thread([this, &rpcs_sent]() {
373 bool network_down = true;
374 int total_completions = 0;
376 while (CQNext(&got_tag, &ok)) {
379 AsyncClientCall* call = static_cast<AsyncClientCall*>(got_tag);
380 if (!call->status.ok()) {
381 gpr_log(GPR_DEBUG, "RPC failed with error: %s",
382 call->status.error_message().c_str());
383 // Bring network up when RPCs start failing
386 network_down = false;
389 gpr_log(GPR_DEBUG, "RPC succeeded");
393 // Remove line below and uncomment the following line after Apple CFStream
394 // bug has been fixed.
396 // EXPECT_EQ(total_completions, rpcs_sent);
399 for (int i = 0; i < 100; ++i) {
401 param.set_skip_cancelled_check(true);
402 SendAsyncRpc(stub, param);
403 std::this_thread::sleep_for(std::chrono::milliseconds(10));
412 // Send a bunch of RPCs, some of which are expected to fail.
413 // We should get back a response for all RPCs
414 TEST_P(CFStreamTest, ConcurrentRpc) {
415 auto channel = BuildChannel();
416 auto stub = BuildStub(channel);
417 std::atomic_int rpcs_sent{0};
418 std::thread thd = std::thread([this, &rpcs_sent]() {
421 int total_completions = 0;
423 while (CQNext(&got_tag, &ok)) {
426 AsyncClientCall* call = static_cast<AsyncClientCall*>(got_tag);
427 if (!call->status.ok()) {
428 gpr_log(GPR_DEBUG, "RPC failed with error: %s",
429 call->status.error_message().c_str());
430 // Bring network up when RPCs start failing
432 gpr_log(GPR_DEBUG, "RPC succeeded");
436 // Remove line below and uncomment the following line after Apple CFStream
437 // bug has been fixed.
439 // EXPECT_EQ(total_completions, rpcs_sent);
442 for (int i = 0; i < 10; ++i) {
445 ErrorStatus* error = param.mutable_expected_error();
446 error->set_code(StatusCode::INTERNAL);
447 error->set_error_message("internal error");
448 SendAsyncRpc(stub, param);
449 } else if (i % 5 == 0) {
451 param.set_echo_metadata(true);
452 DebugInfo* info = param.mutable_debug_info();
453 info->add_stack_entries("stack_entry1");
454 info->add_stack_entries("stack_entry2");
455 info->set_detail("detailed debug info");
456 SendAsyncRpc(stub, param);
469 } // namespace testing
471 #endif // GRPC_CFSTREAM
473 int main(int argc, char** argv) {
474 ::testing::InitGoogleTest(&argc, argv);
475 grpc::testing::TestEnvironment env(argc, argv);
476 gpr_setenv("grpc_cfstream", "1");
477 const auto result = RUN_ALL_TESTS();