private void testConnector( IoConnector connector, boolean useLocalAddress ) throws Exception
{
EchoConnectorHandler handler = new EchoConnectorHandler();
ByteBuffer readBuf = handler.readBuf;
IoSession session = null;
if( !useLocalAddress )
{
session = connector.connect(
new InetSocketAddress( "localhost", port ),
handler );
}
else
{
int clientPort = port;
for( int i = 0; i < 65536; i ++ )
{
clientPort = AvailablePortFinder.getNextAvailable( clientPort + 1 );
try
{
session = connector.connect(
new InetSocketAddress( "localhost", port ),
new InetSocketAddress( clientPort ),
handler );
break;
}
catch( BindException e )
{
// Try again until we succeed to bind.
}
}
if( session == null )
{
Assert.fail( "Failed to find out an appropriate local address." );
}
}
for( int i = 0; i < 10; i ++ )
{
ByteBuffer buf = ByteBuffer.allocate( 16 );
buf.limit( 16 );
fillWriteBuffer( buf, i );
buf.flip();
Object marker;
if( ( i & 1 ) == 0 )
{
marker = new Integer( i );
}
else
{
marker = null;
}
session.write( buf, marker );
// This will align message arrival order in UDP
for( int j = 0; j < 100; j ++ )
{
if( readBuf.position() == ( i + 1 ) * 16 )
{
break;
}
Thread.sleep( 10 );
}
}
for( int i = 0; i < 100; i++ ) {
if( readBuf.position() == 160 )
{
break;
}
else
{
Thread.sleep( 10 );
}
}
session.close( true );
Assert.assertEquals( 160, readBuf.position() );
readBuf.flip();
ByteBuffer expectedBuf = ByteBuffer.allocate( 160 );