Improvements to TestPeer and PeerTest classes

This commit is contained in:
Vinnie Falco
2013-08-14 10:20:47 -07:00
parent a7cb7f23a4
commit ab0b610424
9 changed files with 150 additions and 27 deletions

View File

@@ -247,3 +247,71 @@ Socket::async_shutdown (BOOST_ASIO_MOVE_ARG(ErrorCall) handler)
#endif #endif
} }
//------------------------------------------------------------------------------
#if 0
/* Stream, SyncReadStream, AsyncReadStream, WriteStream, AsyncWriteStream */
// Note, missing std::future<> returns
class Stream
{
public:
// Stream
typedef typename remove_reference<Stream>::type next_layer_type;
typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
next_layer_type& next_layer()
next_layer_type const& next_layer() const
lowest_layer_type& lowest_layer()
const lowest_layer_type& lowest_layer() const
boost::asio::io_service& get_io_service()
void close()
boost::system::error_code close(boost::system::error_code& ec)
// SyncWriteStream
template <typename ConstBufferSequence>
std::size_t write_some (const ConstBufferSequence& buffers)
template <typename ConstBufferSequence>
std::size_t write_some (const ConstBufferSequence& buffers, boost::system::error_code& ec)
// AsyncWriteStream
template <typename ConstBufferSequence, typename WriteHandler>
void async_write_some (const ConstBufferSequence& buffers, WriteHandler handler)
// ReadStream
template <typename MutableBufferSequence>
std::size_t read_some (const MutableBufferSequence& buffers)
template <typename MutableBufferSequence>
std::size_t read_some (const MutableBufferSequence& buffers, boost::system::error_code& ec)
// AsyncReadStream
template <typename MutableBufferSequence, typename ReadHandler>
void async_read_some (const MutableBufferSequence& buffers, ReadHandler handler)
};
#endif
/* members, and the most common base class in which they appear:
basic_io_object
io_service& get_io_service ()
basic_socket <Protocol> : basic_io_object
typedef protocol_type
typedef lowest_layer_type
lowest_layer_type& lowest_layer ()
native_handle () // Socket::native_handle() would return void* and we'd use the templates to do the static_cast
cancel ()
shutdon (shutdown_type)
close ()
<various>
basic_socket_acceptor <Protocol> : basic_io_object
typedef protocol_type
native_handle ()
listen ()
accept ()
async_accept ()
cancel ()
close ()
basic_stream_socket <Protocol> : basic_socket <Protocol>
*/

View File

@@ -644,4 +644,15 @@ public:
Object* m_impl; Object* m_impl;
}; };
//------------------------------------------------------------------------------
/** Dynamically allocate a SocketWrapper.
This deduces the template arguments for convenience.
*/
template <typename Object>
SocketWrapper <Object>* newSocketWrapper (Object& object)
{
return new SocketWrapper <Object> (object);
}
#endif #endif

View File

@@ -76,6 +76,30 @@ protected:
typedef value type; typedef value type;
}; };
// Specialization for boost::asio::buffered_stream
template <typename Stream>
struct InterfacesOf <boost::asio::buffered_stream <Stream> >
{
struct value : SocketInterface::Stream { };
typedef value type;
};
// Specialization for boost::asio::buffered_read_stream
template <typename Stream>
struct InterfacesOf <boost::asio::buffered_read_stream <Stream> >
{
struct value : SocketInterface::Stream { };
typedef value type;
};
// Specialization for boost::asio::buffered_write_stream
template <typename Stream>
struct InterfacesOf <boost::asio::buffered_write_stream <Stream> >
{
struct value : SocketInterface::Stream { };
typedef value type;
};
// Specialization for boost::asio::ssl::stream // Specialization for boost::asio::ssl::stream
template <typename Stream> template <typename Stream>
struct InterfacesOf <boost::asio::ssl::stream <Stream> > struct InterfacesOf <boost::asio::ssl::stream <Stream> >

View File

@@ -26,7 +26,7 @@ PeerTest::Result::Result ()
PeerTest::Result::Result (boost::system::error_code const& ec, String const& prefix) PeerTest::Result::Result (boost::system::error_code const& ec, String const& prefix)
: m_ec (ec) : m_ec (ec)
, m_message ((prefix == String::empty) ? ec.message () , m_message ((prefix == String::empty) ? ec.message ()
: prefix + " " + ec.message ()) : prefix + ": " + ec.message ())
{ {
} }

View File

@@ -105,27 +105,25 @@ public:
/** Test two peers and return the results. /** Test two peers and return the results.
*/ */
template <typename Details, typename ServerLogic, typename ClientLogic, class Arg> template <typename Details, typename ClientLogic, typename ServerLogic, typename ClientArg, typename ServerArg>
static Results run (Arg const& arg, int timeoutSeconds = defaultTimeoutSeconds) static Results run (ClientArg const& clientArg, ServerArg const& serverArg, int timeoutSeconds = defaultTimeoutSeconds)
{ {
Results results; Results results;
if (Process::isRunningUnderDebugger ()) if (Process::isRunningUnderDebugger ())
timeoutSeconds = -1; timeoutSeconds = -1;
results.name = Details::getArgName (arg);
try try
{ {
TestPeerType <ServerLogic, Details> server (arg); TestPeerType <ServerLogic, Details> server (serverArg);
results.name << " / " << server.name (); results.name = server.name () + Details::getArgName (serverArg);
try try
{ {
TestPeerType <ClientLogic, Details> client (arg); TestPeerType <ClientLogic, Details> client (clientArg);
results.name << " / " << client.name (); results.name << " / " + client.name () + Details::getArgName (clientArg);
try try
{ {
@@ -179,6 +177,12 @@ public:
return results; return results;
} }
template <typename Details, typename ClientLogic, typename ServerLogic, class Arg>
static Results run (Arg const& arg, int timeoutSeconds = defaultTimeoutSeconds)
{
return run <Details, ClientLogic, ServerLogic, Arg, Arg> (arg, arg, timeoutSeconds);
}
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** Reports tests of Details for all known asynchronous logic combinations to a UnitTest. /** Reports tests of Details for all known asynchronous logic combinations to a UnitTest.
@@ -188,7 +192,7 @@ public:
int timeoutSeconds = defaultTimeoutSeconds, int timeoutSeconds = defaultTimeoutSeconds,
bool beginTestCase = true) bool beginTestCase = true)
{ {
run <Details, TestPeerLogicAsyncServer, TestPeerLogicAsyncClient> run <Details, TestPeerLogicAsyncClient, TestPeerLogicAsyncServer>
(arg, timeoutSeconds).report (test, beginTestCase); (arg, timeoutSeconds).report (test, beginTestCase);
} }
@@ -199,13 +203,13 @@ public:
int timeoutSeconds = defaultTimeoutSeconds, int timeoutSeconds = defaultTimeoutSeconds,
bool beginTestCase = true) bool beginTestCase = true)
{ {
run <Details, TestPeerLogicSyncServer, TestPeerLogicSyncClient> run <Details, TestPeerLogicSyncClient, TestPeerLogicSyncServer>
(arg, timeoutSeconds).report (test, beginTestCase); (arg, timeoutSeconds).report (test, beginTestCase);
run <Details, TestPeerLogicSyncServer, TestPeerLogicAsyncClient> run <Details, TestPeerLogicAsyncClient, TestPeerLogicSyncServer>
(arg, timeoutSeconds).report (test, beginTestCase); (arg, timeoutSeconds).report (test, beginTestCase);
run <Details, TestPeerLogicAsyncServer, TestPeerLogicSyncClient> run <Details, TestPeerLogicSyncClient, TestPeerLogicAsyncServer>
(arg, timeoutSeconds).report (test, beginTestCase); (arg, timeoutSeconds).report (test, beginTestCase);
report_async <Details> (test, arg, timeoutSeconds, beginTestCase); report_async <Details> (test, arg, timeoutSeconds, beginTestCase);

View File

@@ -48,10 +48,10 @@ public:
static String getArgName (arg_type arg) static String getArgName (arg_type arg)
{ {
if (arg == protocol_type::v4 ()) if (arg == protocol_type::v4 ())
return "tcpv4"; return ".tcpv4";
else if (arg == protocol_type::v6 ()) else if (arg == protocol_type::v6 ())
return "tcpv6"; return ".tcpv6";
return "tcp?"; return ".tcp?";
} }
String name () String name ()

View File

@@ -135,10 +135,18 @@ void TestPeerLogicAsyncClient::on_shutdown (error_code const& ec)
{ {
if (success (error (ec), true)) if (success (error (ec), true))
{ {
if (success (socket ().close (error ()))) if (socket ().requires_handshake ())
{ {
// doing nothing here is intended, socket ().shutdown (Socket::shutdown_both, error ());
// as the calls to success() may set error() }
if (! error ())
{
if (success (socket ().close (error ())))
{
// doing nothing here is intended,
// as the calls to success() may set error()
}
} }
} }
} }

View File

@@ -100,6 +100,11 @@ void TestPeerLogicAsyncServer::on_shutdown (error_code const& ec)
{ {
if (success (error (ec), true)) if (success (error (ec), true))
{ {
if (socket ().requires_handshake ())
{
socket ().shutdown (Socket::shutdown_both, error ());
}
if (success (socket ().close (error ()))) if (success (socket ().close (error ())))
{ {
// doing nothing here is intended, // doing nothing here is intended,

View File

@@ -22,14 +22,6 @@
class TestPeerUnitTests : public UnitTest class TestPeerUnitTests : public UnitTest
{ {
public: public:
enum
{
timeoutSeconds = 3
};
TestPeerUnitTests () : UnitTest ("TestPeer", "beast", runManual)
{
}
template <typename Details, typename Arg > template <typename Details, typename Arg >
void testDetails (Arg const& arg = Arg ()) void testDetails (Arg const& arg = Arg ())
@@ -43,6 +35,17 @@ public:
testDetails <TcpDetails, TcpDetails::arg_type> (protocol::v4 ()); testDetails <TcpDetails, TcpDetails::arg_type> (protocol::v4 ());
testDetails <TcpDetails, TcpDetails::arg_type> (protocol::v6 ()); testDetails <TcpDetails, TcpDetails::arg_type> (protocol::v6 ());
} }
//--------------------------------------------------------------------------
enum
{
timeoutSeconds = 10
};
TestPeerUnitTests () : UnitTest ("TestPeer", "beast", runManual)
{
}
}; };
static TestPeerUnitTests testPeerUnitTests; static TestPeerUnitTests testPeerUnitTests;