REGISTER_TESTS_END

// TestOneServerMultipleClients
//------------------------------------------------------------------------------
void TestTestTCPConnectionPool::TestOneServerMultipleClients() const
{
    const uint16_t testPort( TEST_PORT );

    for ( uint32_t i=0; i<NUM_TEST_PASSES; ++i )
    {
        // listen like a server
        TCPConnectionPool server;
        TEST_ASSERT( server.Listen( testPort ) );

        // connect like a client
        TCPConnectionPool clientA;
        TEST_ASSERT( clientA.Connect( AStackString<>( "127.0.0.1" ), testPort ) );

        // connect like a client
        TCPConnectionPool clientB;
        TEST_ASSERT( clientB.Connect( AStackString<>( "127.0.0.1" ), testPort ) );

        // connect like a client
        TCPConnectionPool clientC;
        TEST_ASSERT( clientC.Connect( AStackString<>( "127.0.0.1" ), testPort ) );

        // connect like a client
        TCPConnectionPool clientD;
        TEST_ASSERT( clientD.Connect( AStackString<>( "127.0.0.1" ), testPort ) );
    }
}
/*static*/ uint32_t TestTestTCPConnectionPool::TestConnectionStuckDuringSend_ThreadFunc( void * userData )
{
    const ConnectionInfo * ci = (const ConnectionInfo *)userData;
    TCPConnectionPool & client = ci->GetTCPConnectionPool();
    // send lots of data to slow server
    AutoPtr< char > mem( FNEW( char[ 10 * MEGABYTE ] ) );
    for ( size_t i=0; i<1000; ++i )
    {
        if ( !client.Send( ci, mem.Get(), 10 * MEGABYTE ) )
        {
            break;
        }
    }
    return 0;
}
// TestMultipleServersOneClient
//------------------------------------------------------------------------------
void TestTestTCPConnectionPool::TestMultipleServersOneClient() const
{
    const uint16_t testPort( TEST_PORT );

    for ( uint32_t i=0; i<NUM_TEST_PASSES; ++i )
    {
        // multiple servers
        TCPConnectionPool serverA;
        TEST_ASSERT( serverA.Listen( testPort ) );
        TCPConnectionPool serverB;
        TEST_ASSERT( serverB.Listen( testPort + 1 ) );
        TCPConnectionPool serverC;
        TEST_ASSERT( serverC.Listen( testPort + 2 ) );
        TCPConnectionPool serverD;
        TEST_ASSERT( serverD.Listen( testPort + 3 ) );

        // connect client to multiple servers
        TCPConnectionPool clientA;
        TEST_ASSERT( clientA.Connect( AStackString<>( "127.0.0.1" ), testPort ) );
        TEST_ASSERT( clientA.Connect( AStackString<>( "127.0.0.1" ), testPort + 1 ) );
        TEST_ASSERT( clientA.Connect( AStackString<>( "127.0.0.1" ), testPort + 2 ) );
        TEST_ASSERT( clientA.Connect( AStackString<>( "127.0.0.1" ), testPort + 3 ) );
    }
}
// TestConnectionCount
//------------------------------------------------------------------------------
void TestTestTCPConnectionPool::TestConnectionCount() const
{
    const uint16_t testPort( TEST_PORT );

    for ( uint32_t i=0; i<NUM_TEST_PASSES; ++i )
    {
        // multiple servers
        TCPConnectionPool serverA;
        TEST_ASSERT( serverA.Listen( testPort ) );
        TEST_ASSERT( serverA.GetNumConnections() == 0 );
        TCPConnectionPool serverB;
        TEST_ASSERT( serverB.Listen( testPort + 1 ) );
        TEST_ASSERT( serverB.GetNumConnections() == 0 );

        // connect client to multiple servers
        {
            TCPConnectionPool clientA;
            TEST_ASSERT( clientA.Connect( AStackString<>( "127.0.0.1" ), testPort ) );
            TEST_ASSERT( clientA.Connect( AStackString<>( "127.0.0.1" ), testPort + 1 ) );

            WAIT_UNTIL_WITH_TIMEOUT( serverA.GetNumConnections() == 1 );
            WAIT_UNTIL_WITH_TIMEOUT( serverB.GetNumConnections() == 1 );
            WAIT_UNTIL_WITH_TIMEOUT( clientA.GetNumConnections() == 2 );
        }
        WAIT_UNTIL_WITH_TIMEOUT( serverA.GetNumConnections() == 0 );
        WAIT_UNTIL_WITH_TIMEOUT( serverB.GetNumConnections() == 0 );
    }
}
    const uint16_t testPort( TEST_PORT );

    // a big piece of data, initialized to some known pattern
    const size_t maxSendSize( 1024 * 1024 * 10 );
    AutoPtr< char > data( (char *)ALLOC( maxSendSize ) );
    for ( size_t i=0; i< maxSendSize; ++i )
    {
        data.Get()[ i ] = (char)i;
    }

    TestServer server;
    TEST_ASSERT( server.Listen( testPort ) );

    // client
    TCPConnectionPool client;
    const ConnectionInfo * ci = client.Connect( AStackString<>( "127.0.0.1" ), testPort );
    TEST_ASSERT( ci );

    size_t sendSize = 31;
    while ( sendSize <= maxSendSize )
    {
        server.m_ReceivedBytes = 0;
        server.m_DataSize = sendSize;

        Timer timer;

        size_t totalSent = 0;
        while ( timer.GetElapsed() < 0.1f )
        {
            // client sends some know data to the server