From 0c025e8f52096f6c9f39b80909b5b1aebf335b03 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Wed, 6 Oct 2010 17:10:36 +0000 Subject: [PATCH] THRIFT-929. cpp: Update TransportTest to test TSocket Made the TSocket(int fd) constructor public, so TransportTest can create a connected pair of TSocket transports using socketpair(). git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1005158 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/test/TransportTest.cpp | 96 +++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 12 deletions(-) diff --git a/lib/cpp/test/TransportTest.cpp b/lib/cpp/test/TransportTest.cpp index fdc5c637..baaf2bd9 100644 --- a/lib/cpp/test/TransportTest.cpp +++ b/lib/cpp/test/TransportTest.cpp @@ -37,6 +37,7 @@ #include #include #include +#include using namespace apache::thrift::transport; @@ -114,6 +115,14 @@ class GenericSizeGenerator : public SizeGenerator { * Classes to set up coupled transports **************************************************************************/ +/** + * Helper class to represent a coupled pair of transports. + * + * Data written to the out transport can be read from the in transport. + * + * This is used as the base class for the various coupled transport + * implementations. It shouldn't be instantiated directly. + */ template class CoupledTransports { public: @@ -129,6 +138,9 @@ class CoupledTransports { CoupledTransports &operator=(const CoupledTransports&); }; +/** + * Coupled TMemoryBuffers + */ class CoupledMemoryBuffers : public CoupledTransports { public: CoupledMemoryBuffers() { @@ -139,6 +151,11 @@ class CoupledMemoryBuffers : public CoupledTransports { TMemoryBuffer buf; }; +/** + * Coupled TBufferedTransports. + * + * Uses a TMemoryBuffer as the underlying transport. + */ class CoupledBufferedTransports : public CoupledTransports { public: @@ -156,6 +173,11 @@ class CoupledBufferedTransports : boost::shared_ptr buf; }; +/** + * Coupled TFramedTransports. + * + * Uses a TMemoryBuffer as the underlying transport. + */ class CoupledFramedTransports : public CoupledTransports { public: CoupledFramedTransports() : @@ -172,6 +194,9 @@ class CoupledFramedTransports : public CoupledTransports { boost::shared_ptr buf; }; +/** + * Coupled TZlibTransports. + */ class CoupledZlibTransports : public CoupledTransports { public: CoupledZlibTransports() : @@ -188,6 +213,9 @@ class CoupledZlibTransports : public CoupledTransports { boost::shared_ptr buf; }; +/** + * Coupled TFDTransports. + */ class CoupledFDTransports : public CoupledTransports { public: CoupledFDTransports() { @@ -207,6 +235,25 @@ class CoupledFDTransports : public CoupledTransports { } }; +/** + * Coupled TSockets + */ +class CoupledSocketTransports : public CoupledTransports { + public: + CoupledSocketTransports() { + int sockets[2]; + if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != 0) { + return; + } + + in = new TSocket(sockets[0]); + out = new TSocket(sockets[1]); + } +}; + +/** + * Coupled TFileTransports + */ class CoupledFileTransports : public CoupledTransports { public: CoupledFileTransports() { @@ -239,6 +286,14 @@ class CoupledFileTransports : public CoupledTransports { int fd; }; +/** + * Wrapper around another CoupledTransports implementation that exposes the + * transports as TTransport pointers. + * + * This is used since accessing a transport via a "TTransport*" exercises a + * different code path than using the base pointer class. As part of the + * template code changes, most transport methods are no longer virtual. + */ template class CoupledTTransports : public CoupledTransports { public: @@ -250,6 +305,13 @@ class CoupledTTransports : public CoupledTransports { CoupledTransports_ transports; }; +/** + * Wrapper around another CoupledTransports implementation that exposes the + * transports as TBufferBase pointers. + * + * This can only be instantiated with a transport type that is a subclass of + * TBufferBase. + */ template class CoupledBufferBases : public CoupledTransports { public: @@ -261,18 +323,6 @@ class CoupledBufferBases : public CoupledTransports { CoupledTransports_ transports; }; -/* - * TODO: It would be nice to test TSocket, too. - * Unfortunately, TSocket/TServerSocket currently don't provide a low-level - * API that would allow us to create a connected socket pair. - * - * TODO: It would be nice to test TZlibTransport, too. - * However, TZlibTransport doesn't conform to quite the same semantics as other - * transports. No new data can be written to a TZlibTransport after flush() is - * called, since flush() terminates the zlib data stream. In the future maybe - * we should make TZlibTransport behave more like the other transports. - */ - /************************************************************************** * Main testing function **************************************************************************/ @@ -504,6 +554,28 @@ class TransportTestGen { TEST_RW(CoupledFDTransports, 1024*16, 1, 1, rand4k, rand4k, fd_max_outstanding); + // TSocket tests + uint32_t socket_max_outstanding = 4096; + TEST_RW(CoupledSocketTransports, 1024*1024, 0, 0, + 0, 0, socket_max_outstanding); + TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k, + 0, 0, socket_max_outstanding); + TEST_RW(CoupledSocketTransports, 1024*256, 167, 163, + 0, 0, socket_max_outstanding); + // Doh. Apparently writing to a socket has some additional overhead for + // each send() call. If we have more than ~400 outstanding 1-byte write + // requests, additional send() calls start blocking. + TEST_RW(CoupledSocketTransports, 1024*16, 1, 1, + 0, 0, 400); + TEST_RW(CoupledSocketTransports, 1024*256, 0, 0, + rand4k, rand4k, socket_max_outstanding); + TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k, + rand4k, rand4k, socket_max_outstanding); + TEST_RW(CoupledSocketTransports, 1024*256, 167, 163, + rand4k, rand4k, socket_max_outstanding); + TEST_RW(CoupledSocketTransports, 1024*16, 1, 1, + rand4k, rand4k, 400); + // TFileTransport tests // We use smaller buffer sizes here, since TFileTransport is fairly slow. // -- 2.17.1