You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2020/11/10 07:02:37 UTC

[pulsar] 04/04: [C++] Catch exception thrown by remote_endpoint (#8486)

This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch branch-2.6
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 7f04056c441a2cdb74817c4d666875b5660ebfcb
Author: Yunze Xu <xy...@163.com>
AuthorDate: Tue Nov 10 14:44:46 2020 +0800

    [C++] Catch exception thrown by remote_endpoint (#8486)
    
    ### Motivation
    
    Boost asio `socket::remote_point` may throw `boost::system::system_error` on failure. If the C++ client library was compiled with some low version boost, like 1.54, even if `async_connect` success, the server could still be unreachable and an exception would be thrown by `socket_->remote_endpoint()`.
    
    ### Modifications
    
    - Catch the exception for  `socket_->remote_endpoint()`
    - Add tests for when client connects to a unreachable service url
    - Fix header dependency because some boost *.hpp file doesn't include assert.h
    
    ### Verifying this change
    
    - [ ] Make sure that the change passes the CI checks.
    
    This change added tests and can be verified as follows:
    
      - Run `testServerConnectError`.
    
    (cherry picked from commit 53cd2bbbe173dadc08dc07f7f3e88c6762e87814)
---
 pulsar-client-cpp/lib/BlockingQueue.h     |  1 +
 pulsar-client-cpp/lib/ClientConnection.cc | 12 +++++++++---
 pulsar-client-cpp/tests/ClientTest.cc     | 13 +++++++++++++
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/pulsar-client-cpp/lib/BlockingQueue.h b/pulsar-client-cpp/lib/BlockingQueue.h
index 1cdaf93..5e466bd 100644
--- a/pulsar-client-cpp/lib/BlockingQueue.h
+++ b/pulsar-client-cpp/lib/BlockingQueue.h
@@ -19,6 +19,7 @@
 #ifndef LIB_BLOCKINGQUEUE_H_
 #define LIB_BLOCKINGQUEUE_H_
 
+#include <assert.h>
 #include <mutex>
 #include <condition_variable>
 #include <boost/circular_buffer.hpp>
diff --git a/pulsar-client-cpp/lib/ClientConnection.cc b/pulsar-client-cpp/lib/ClientConnection.cc
index ae44b52..959d64b 100644
--- a/pulsar-client-cpp/lib/ClientConnection.cc
+++ b/pulsar-client-cpp/lib/ClientConnection.cc
@@ -337,9 +337,15 @@ void ClientConnection::handleTcpConnected(const boost::system::error_code& err,
                                           tcp::resolver::iterator endpointIterator) {
     if (!err) {
         std::stringstream cnxStringStream;
-        cnxStringStream << "[" << socket_->local_endpoint() << " -> " << socket_->remote_endpoint() << "] ";
-        cnxString_ = cnxStringStream.str();
-
+        try {
+            cnxStringStream << "[" << socket_->local_endpoint() << " -> " << socket_->remote_endpoint()
+                            << "] ";
+            cnxString_ = cnxStringStream.str();
+        } catch (const boost::system::system_error& e) {
+            LOG_ERROR("Failed to get endpoint: " << e.what());
+            close();
+            return;
+        }
         if (logicalAddress_ == physicalAddress_) {
             LOG_INFO(cnxString_ << "Connected to broker");
         } else {
diff --git a/pulsar-client-cpp/tests/ClientTest.cc b/pulsar-client-cpp/tests/ClientTest.cc
index d4eaca7..91232dc 100644
--- a/pulsar-client-cpp/tests/ClientTest.cc
+++ b/pulsar-client-cpp/tests/ClientTest.cc
@@ -73,3 +73,16 @@ TEST(ClientTest, testSwHwChecksum) {
     ASSERT_EQ(hwIncrementalChecksum, hwDoubleChecksum);
     ASSERT_EQ(hwIncrementalChecksum, swIncrementalChecksum);
 }
+
+TEST(ClientTest, testServerConnectError) {
+    const std::string topic = "test-server-connect-error";
+    Client client("pulsar://localhost:65535");
+    Producer producer;
+    ASSERT_EQ(ResultConnectError, client.createProducer(topic, producer));
+    Consumer consumer;
+    ASSERT_EQ(ResultConnectError, client.subscribe(topic, "sub", consumer));
+    Reader reader;
+    ReaderConfiguration readerConf;
+    ASSERT_EQ(ResultConnectError, client.createReader(topic, MessageId::earliest(), readerConf, reader));
+    client.close();
+}