From 20e2964ada045d5bb95ef14d105a39f97a4ed1ae Mon Sep 17 00:00:00 2001
From: Prashant Jaikumar <pjaikumar@google.com>
Date: Wed, 21 Aug 2019 16:12:31 -0700
Subject: [PATCH] cfstream_test: workaround Apple CFStream bug

---
 CMakeLists.txt                    |  1 -
 test/cpp/end2end/cfstream_test.cc | 27 ++++++++++++++++++++++++---
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 65aadb7cf1..28cd26555f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17370,7 +17370,6 @@ target_include_directories(timer_test
   PRIVATE ${_gRPC_BENCHMARK_INCLUDE_DIR}
   PRIVATE ${_gRPC_CARES_INCLUDE_DIR}
   PRIVATE ${_gRPC_GFLAGS_INCLUDE_DIR}
-  PRIVATE ${_gRPC_NANOPB_INCLUDE_DIR}
   PRIVATE ${_gRPC_PROTOBUF_INCLUDE_DIR}
   PRIVATE ${_gRPC_SSL_INCLUDE_DIR}
   PRIVATE ${_gRPC_UPB_GENERATED_DIR}
diff --git a/test/cpp/end2end/cfstream_test.cc b/test/cpp/end2end/cfstream_test.cc
index 1c9320861f..35b5b7b835 100644
--- a/test/cpp/end2end/cfstream_test.cc
+++ b/test/cpp/end2end/cfstream_test.cc
@@ -195,7 +195,22 @@ class CFStreamTest : public ::testing::TestWithParam<TestScenario> {
 
   void ShutdownCQ() { cq_.Shutdown(); }
 
-  bool CQNext(void** tag, bool* ok) { return cq_.Next(tag, ok); }
+  bool CQNext(void** tag, bool* ok) {
+    auto deadline = std::chrono::system_clock::now() + std::chrono::seconds(10);
+    auto ret = cq_.AsyncNext(tag, ok, deadline);
+    if (ret == grpc::CompletionQueue::GOT_EVENT) {
+      return true;
+    } else if (ret == grpc::CompletionQueue::SHUTDOWN) {
+      return false;
+    } else {
+      GPR_ASSERT(ret == grpc::CompletionQueue::TIMEOUT);
+      // This can happen if we hit the Apple CFStream bug which results in the
+      // read stream hanging. We are ignoring hangs and timeouts, but these
+      // tests are still useful as they can catch memory memory corruptions,
+      // crashes and other bugs that don't result in test hang/timeout.
+      return false;
+    }
+  }
 
   bool WaitForChannelNotReady(Channel* channel, int timeout_seconds = 5) {
     const gpr_timespec deadline =
@@ -391,7 +406,10 @@ TEST_P(CFStreamTest, NetworkFlapRpcsInFlight) {
       }
       delete call;
     }
-    EXPECT_EQ(total_completions, rpcs_sent);
+    // Remove line below and uncomment the following line after Apple CFStream
+    // bug has been fixed.
+    (void)rpcs_sent;
+    // EXPECT_EQ(total_completions, rpcs_sent);
   });
 
   for (int i = 0; i < 100; ++i) {
@@ -432,7 +450,10 @@ TEST_P(CFStreamTest, ConcurrentRpc) {
       }
       delete call;
     }
-    EXPECT_EQ(total_completions, rpcs_sent);
+    // Remove line below and uncomment the following line after Apple CFStream
+    // bug has been fixed.
+    (void)rpcs_sent;
+    // EXPECT_EQ(total_completions, rpcs_sent);
   });
 
   for (int i = 0; i < 10; ++i) {
-- 
GitLab