diff options
| author | Alan Conway <aconway@apache.org> | 2007-12-10 17:57:49 +0000 |
|---|---|---|
| committer | Alan Conway <aconway@apache.org> | 2007-12-10 17:57:49 +0000 |
| commit | 22ef0be441cd0f12b1ca6402f397deac390ce4d0 (patch) | |
| tree | c124a8348ad9f0517116e2c638884c6e0adb50d1 /cpp/src/tests/SocketProxy.h | |
| parent | e997eeb68fdb7f975f9e70848832f2b0d7612683 (diff) | |
| download | qpid-python-22ef0be441cd0f12b1ca6402f397deac390ce4d0.tar.gz | |
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
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@602980 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/tests/SocketProxy.h')
| -rw-r--r-- | cpp/src/tests/SocketProxy.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/cpp/src/tests/SocketProxy.h b/cpp/src/tests/SocketProxy.h new file mode 100644 index 0000000000..03d9b6ad35 --- /dev/null +++ b/cpp/src/tests/SocketProxy.h @@ -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 |
