You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by as...@apache.org on 2017/09/12 18:07:17 UTC

qpid-proton git commit: PROTON-1566 PROTON-1567: [c++ binding] test for reconnect/failover

Repository: qpid-proton
Updated Branches:
  refs/heads/master 564e0ca4c -> ec3c8f659


PROTON-1566 PROTON-1567: [c++ binding] test for reconnect/failover


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/ec3c8f65
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/ec3c8f65
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/ec3c8f65

Branch: refs/heads/master
Commit: ec3c8f659b6488c60f5391fc38e567d7060ccace
Parents: 564e0ca
Author: Andrew Stitcher <as...@apache.org>
Authored: Fri Sep 8 18:13:10 2017 -0400
Committer: Andrew Stitcher <as...@apache.org>
Committed: Tue Sep 12 12:23:35 2017 -0400

----------------------------------------------------------------------
 proton-c/bindings/cpp/CMakeLists.txt            |   1 +
 .../cpp/src/include/proactor_container_impl.hpp |   1 -
 .../cpp/src/proactor_container_impl.cpp         |   2 +-
 proton-c/bindings/cpp/src/reconnect_test.cpp    | 169 +++++++++++++++++++
 4 files changed, 171 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/ec3c8f65/proton-c/bindings/cpp/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/CMakeLists.txt b/proton-c/bindings/cpp/CMakeLists.txt
index ef5f96b..e0bcac9 100644
--- a/proton-c/bindings/cpp/CMakeLists.txt
+++ b/proton-c/bindings/cpp/CMakeLists.txt
@@ -177,3 +177,4 @@ add_cpp_test(scalar_test)
 add_cpp_test(value_test)
 add_cpp_test(container_test)
 add_cpp_test(url_test)
+add_cpp_test(reconnect_test)

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/ec3c8f65/proton-c/bindings/cpp/src/include/proactor_container_impl.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/include/proactor_container_impl.hpp b/proton-c/bindings/cpp/src/include/proactor_container_impl.hpp
index 804908a..1669c45 100644
--- a/proton-c/bindings/cpp/src/include/proactor_container_impl.hpp
+++ b/proton-c/bindings/cpp/src/include/proactor_container_impl.hpp
@@ -145,7 +145,6 @@ class container::impl {
     proton::sender_options sender_options_;
     proton::receiver_options receiver_options_;
     error_condition disconnect_error_;
-    int retries_;
 
     bool auto_stop_;
     bool stopping_;

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/ec3c8f65/proton-c/bindings/cpp/src/proactor_container_impl.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/proactor_container_impl.cpp b/proton-c/bindings/cpp/src/proactor_container_impl.cpp
index e3468d2..533a13c 100644
--- a/proton-c/bindings/cpp/src/proactor_container_impl.cpp
+++ b/proton-c/bindings/cpp/src/proactor_container_impl.cpp
@@ -563,7 +563,7 @@ bool container::impl::handle(pn_event_t* event) {
             pn_connection_close(c);
             return false;
         }
-
+        break;
     }
     case PN_TRANSPORT_CLOSED: {
         // If reconnect is turned on then handle closed on error here with reconnect attempt

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/ec3c8f65/proton-c/bindings/cpp/src/reconnect_test.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/reconnect_test.cpp b/proton-c/bindings/cpp/src/reconnect_test.cpp
new file mode 100644
index 0000000..461e873
--- /dev/null
+++ b/proton-c/bindings/cpp/src/reconnect_test.cpp
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#include "test_bits.hpp"
+#include "proton/error_condition.hpp"
+#include "proton/connection.hpp"
+#include "proton/connection_options.hpp"
+#include "proton/container.hpp"
+#include "proton/delivery.hpp"
+#include "proton/message.hpp"
+#include "proton/messaging_handler.hpp"
+#include "proton/listener.hpp"
+#include "proton/listen_handler.hpp"
+#include "proton/reconnect_options.hpp"
+
+#include "proton/internal/pn_unique_ptr.hpp"
+
+#include <cstdlib>
+#include <ctime>
+#include <string>
+#include <cstdio>
+#include <sstream>
+
+namespace {
+
+static std::string int2string(int n) {
+    std::ostringstream strm;
+    strm << n;
+    return strm.str();
+}
+
+class server_connection_handler : public proton::messaging_handler {
+    proton::listener listener_;
+    std::string url_;
+    int messages_;
+    int expect_;
+    bool closing_;
+    bool done_;
+
+    void close (proton::connection &c) {
+        if (closing_) return;
+
+        c.close(proton::error_condition("amqp:connection:forced", "Failover testing"));
+        closing_ = true;
+    }
+
+    void listen_on_random_port(proton::container& c, proton::messaging_handler& h) {
+        int p;
+        // I'm going to hell for this (on more than one count!):
+        static struct once { once() {std::srand((unsigned int)time(0));} } x;
+        while (true) {
+            p = 20000 + (std::rand() % 30000);
+            try {
+                listener_ = c.listen("0.0.0.0:" + int2string(p), proton::connection_options().handler(h));
+                break;
+            } catch (...) {
+                // keep trying
+            }
+        }
+        url_ = "127.0.0.1:" + int2string(p);
+    }
+
+  public:
+    server_connection_handler(proton::container& c, int e)
+        : messages_(0), expect_(e), closing_(false), done_(false)
+    {
+        listen_on_random_port(c, *this);
+    }
+
+    std::string url() const { return url_; }
+
+    void on_connection_open(proton::connection &c) PN_CPP_OVERRIDE {
+        // Only listen for a single connection
+        listener_.stop();
+        if (messages_==expect_) close(c);
+        else c.open();
+    }
+
+    void on_message(proton::delivery & d, proton::message & m) PN_CPP_OVERRIDE {
+        ++messages_;
+        proton::connection c = d.connection();
+        if (messages_==expect_) close(c);
+    }
+
+    void on_connection_close(proton::connection & c) PN_CPP_OVERRIDE {
+        done_ = true;
+    }
+};
+
+class tester : public proton::messaging_handler {
+  public:
+    tester() :
+        container_(*this, "reconnect_server")
+    {
+    }
+
+    void on_container_start(proton::container &c) PN_CPP_OVERRIDE {
+        // Server that fails upon connection
+        s1.reset(new server_connection_handler(c, 0));
+        std::string url1 = s1->url();
+        // Server that fails on first message
+        s2.reset(new server_connection_handler(c, 1));
+        std::string url2 = s2->url();
+        // server that doesn't fail in this test
+        s3.reset(new server_connection_handler(c, 100));
+        std::string url3 = s3->url();
+
+        std::vector<std::string> urls;
+        urls.push_back(url2);
+        urls.push_back(url3);
+        c.connect(url1, proton::connection_options().reconnect(proton::reconnect_options().failover_urls(urls)));
+    }
+
+    void on_connection_open(proton::connection& c) PN_CPP_OVERRIDE {
+        c.open_sender("messages");
+    }
+
+    void on_sendable(proton::sender& s) PN_CPP_OVERRIDE {
+        proton::message m;
+        m.body("hello");
+        s.send(m);
+    }
+
+    void on_tracker_accept(proton::tracker& d) PN_CPP_OVERRIDE {
+        d.connection().close();
+    }
+
+    void run() {
+        container_.run();
+    }
+
+  private:
+    proton::internal::pn_unique_ptr<server_connection_handler> s1;
+    proton::internal::pn_unique_ptr<server_connection_handler> s2;
+    proton::internal::pn_unique_ptr<server_connection_handler> s3;
+    proton::container container_;
+};
+
+int test_failover_simple() {
+    tester().run();
+    return 0;
+}
+
+
+}
+
+int main(int, char**) {
+    int failed = 0;
+    RUN_TEST(failed, test_failover_simple());
+    return failed;
+}
+


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org