You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2007/12/10 18:57:52 UTC

svn commit: r602980 - in /incubator/qpid/trunk/qpid/cpp/src: qpid/client/Connector.h tests/BrokerFixture.h tests/ClientSessionTest.cpp tests/Makefile.am tests/SocketProxy.h tests/exception_test.cpp

Author: aconway
Date: Mon Dec 10 09:57:49 2007
New Revision: 602980

URL: http://svn.apache.org/viewvc?rev=602980&view=rev
Log:

src/tests/SocketProxy.h: proxy between local client & server to simulate network disconnect.
src/qpid/client/Connector.h: remove friend hack for previous flawed disconnect approach.
src/tests/BrokerFixture.h:   ""
src/tests/ClientSessionTest.cpp, exception_test.cpp: use ProxyConnection

Added:
    incubator/qpid/trunk/qpid/cpp/src/tests/SocketProxy.h   (with props)
Modified:
    incubator/qpid/trunk/qpid/cpp/src/qpid/client/Connector.h
    incubator/qpid/trunk/qpid/cpp/src/tests/BrokerFixture.h
    incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp
    incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am
    incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/client/Connector.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/client/Connector.h?rev=602980&r1=602979&r2=602980&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/client/Connector.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/client/Connector.h Mon Dec 10 09:57:49 2007
@@ -88,7 +88,6 @@
     void eof(qpid::sys::AsynchIO&);
     
   friend class Channel;
-  friend class TestConnector;
 
   public:
     Connector(framing::ProtocolVersion pVersion,

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/BrokerFixture.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/BrokerFixture.h?rev=602980&r1=602979&r2=602980&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/BrokerFixture.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/BrokerFixture.h Mon Dec 10 09:57:49 2007
@@ -29,16 +29,6 @@
 #include "qpid/client/Session_0_10.h"
 #include "qpid/client/SubscriptionManager.h"
 
-namespace qpid { namespace client {
-/** Back door into private Connector stuff */
-struct TestConnector {
-    static void disconnect(qpid::client::Connector& c) {
-        c.socket.close();
-        c.handleClosed();
-    }
-};
-}}
-
 /**
  * A fixture to create an in-process broker and connect to it for tests.
  */
@@ -85,16 +75,6 @@
     /** Open a connection to the local broker */
     void open(qpid::client::Connection& c) {
         c.open("localhost", broker->getPort());
-    }
-
-    /** Close a connection's socket */ 
-    static void disconnect(qpid::client::Connection& c) {
-        struct Expose : public qpid::client::Connection {
-            void disconnect() {
-                qpid::client::TestConnector::disconnect(*impl->getConnector());
-            }
-        };
-        static_cast<Expose&>(c).disconnect();
     }
 };
 

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp?rev=602980&r1=602979&r2=602980&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp Mon Dec 10 09:57:49 2007
@@ -20,6 +20,7 @@
  */
 #include "qpid_test_plugin.h"
 #include "BrokerFixture.h"
+#include "SocketProxy.h"
 #include "qpid/client/Dispatcher.h"
 #include "qpid/client/Session_0_10.h"
 #include "qpid/framing/TransferContent.h"
@@ -181,14 +182,15 @@
     }
 
     void testDisconnectResume() {
-        session =connection.newSession(60);
-        session.queueDeclare(queue="before");
+        ProxyConnection c(broker->getPort());
+        Session_0_10 s = c.session;
+        s.queueDeclare(queue="before");
         CPPUNIT_ASSERT(queueExists("before"));
-        session.queueDeclare(queue=string("after"));
-        disconnect(connection);
+        s.queueDeclare(queue=string("after"));
+        c.proxy.client.close();       // Disconnect the client.
         Connection c2;
         open(c2);
-        c2.resume(session);
+        c2.resume(s);
         CPPUNIT_ASSERT(queueExists("after"));
         c2.close();
     }

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am?rev=602980&r1=602979&r2=602980&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am Mon Dec 10 09:57:49 2007
@@ -28,7 +28,8 @@
 check_PROGRAMS+=unit_test
 unit_test_LDADD=-lboost_unit_test_framework -lboost_regex \
 	$(lib_client) $(lib_broker) 
-unit_test_SOURCES= unit_test.cpp unit_test.h BrokerFixture.h \
+unit_test_SOURCES= unit_test.cpp unit_test.h
+	BrokerFixture.h SocketProxy.h \
 	exception_test.cpp \
 	RefCounted.cpp RefCountedMap.cpp \
 	SessionState.cpp Blob.cpp logging.cpp \

Added: incubator/qpid/trunk/qpid/cpp/src/tests/SocketProxy.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/SocketProxy.h?rev=602980&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/SocketProxy.h (added)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/SocketProxy.h Mon Dec 10 09:57:49 2007
@@ -0,0 +1,80 @@
+#ifndef SOCKETPROXY_H
+#define SOCKETPROXY_H
+
+/*
+ * 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 "qpid/sys/Socket.h"
+#include "qpid/sys/Runnable.h"
+#include "qpid/sys/Thread.h"
+
+/**
+ * A simple socket proxy that forwards to another socket. Used between
+ * client & broker to simulate network failures.
+ */
+struct SocketProxy : public qpid::sys::Runnable
+{
+    int port;             // Port bound to server socket.
+    qpid::sys::Socket client, server; // Client & server sockets.
+
+    SocketProxy(const std::string& host, int port) { init(host,port); }
+    SocketProxy(int port) { init("localhost",port); }
+
+    ~SocketProxy() { client.close(); server.close(); thread.join(); }
+    
+  private:
+
+    void init(const std::string& host, int port) {
+        client.connect(host,port);
+        port = server.listen();
+        thread=qpid::sys::Thread(this);
+    }
+
+    void run() {
+        do {
+            ssize_t recv = server.recv(buffer, sizeof(buffer));
+            if (recv <= 0) return;
+            ssize_t sent=client.send(buffer, recv);
+            if (sent < 0) return;
+            assert(sent == recv); // Assumes we can send as we receive.
+        } while (true);
+    }
+
+    qpid::sys::Thread thread;
+    char buffer[64*1024];
+};
+
+/** A local client connection via a socket proxy. */
+struct ProxyConnection : public qpid::client::Connection {
+    SocketProxy proxy;
+    qpid::client::Session_0_10 session;
+    
+    ProxyConnection(const std::string& host, int port) : proxy(port) {
+        open(host, proxy.port);
+        session=newSession();
+    }
+
+    ProxyConnection(int port) : proxy(port) {
+        open("localhost", proxy.port);
+        session=newSession();
+    }
+};
+
+#endif

Propchange: incubator/qpid/trunk/qpid/cpp/src/tests/SocketProxy.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/qpid/trunk/qpid/cpp/src/tests/SocketProxy.h
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp?rev=602980&r1=602979&r2=602980&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp Mon Dec 10 09:57:49 2007
@@ -21,6 +21,7 @@
 
 #include "unit_test.h"
 #include "BrokerFixture.h"
+#include "SocketProxy.h"
 #include "qpid/client/SubscriptionManager.h"
 #include "qpid/sys/Runnable.h"
 #include "qpid/sys/Thread.h"
@@ -71,31 +72,33 @@
 };
 
 BOOST_FIXTURE_TEST_CASE(DisconnectedGet, BrokerFixture) {
-    Catcher<ClosedException> get(bind(&Session_0_10::get, session));
-    disconnect(connection);
+    ProxyConnection c(broker->getPort());
+    Catcher<ClosedException> get(bind(&Session_0_10::get, c.session));
+    c.proxy.client.close();           // Close the client side.
     BOOST_CHECK(get.join());
 }
 
 BOOST_FIXTURE_TEST_CASE(DisconnectedPop, BrokerFixture) {
-    session.queueDeclare(arg::queue="q");
+    ProxyConnection c(broker->getPort());
+    c.session.queueDeclare(arg::queue="q");
     subs.subscribe(lq, "q");
     Catcher<ClosedException> pop(bind(&LocalQueue::pop, boost::ref(lq)));
-    disconnect(connection);
+    c.proxy.client.close();
     BOOST_CHECK(pop.join());
 }
 
-// FIXME aconway 2007-12-07: This test hangs sporadically at t.join
-// BOOST_FIXTURE_TEST_CASE(DisconnectedListen, BrokerFixture) {
-//     struct NullListener : public MessageListener {
-//         void received(Message&) { BOOST_FAIL("Unexpected message"); }
-//     } l;
-//     session.queueDeclare(arg::queue="q");
-//     subs.subscribe(l, "q");
-//     Thread t(subs);
-//     disconnect(connection);
-//     t.join();
-//     BOOST_CHECK_THROW(session.close(), InternalErrorException);    
-// }
+BOOST_FIXTURE_TEST_CASE(DisconnectedListen, BrokerFixture) {
+    struct NullListener : public MessageListener {
+        void received(Message&) { BOOST_FAIL("Unexpected message"); }
+    } l;
+    ProxyConnection c;
+    c.session.queueDeclare(arg::queue="q");
+    subs.subscribe(l, "q");
+    Thread t(subs);
+    c.proxy.client.close();
+    t.join();
+    BOOST_CHECK_THROW(c.session.close(), InternalErrorException);    
+}
 
 BOOST_FIXTURE_TEST_CASE(NoSuchQueueTest, BrokerFixture) {
     BOOST_CHECK_THROW(subs.subscribe(lq, "no such queue").sync(), NotFoundException);