Skip to content

Commit

Permalink
Couple more unit tests for HandshakeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
lutovich committed Nov 20, 2017
1 parent 385b291 commit 12896b9
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public void exceptionCaught( ChannelHandlerContext ctx, Throwable error )
{
failed = true;

// todo: test this unwrapping and SSLHandshakeException propagation
Throwable cause = error instanceof DecoderException ? error.getCause() : error;
if ( cause instanceof SSLHandshakeException )
{
Expand Down Expand Up @@ -140,7 +139,7 @@ protected void decode( ChannelHandlerContext ctx, ByteBuf in, List<Object> out )

private void fail( ChannelHandlerContext ctx, Throwable error )
{
ctx.close().addListener( future -> handshakeCompletedPromise.setFailure( error ) );
ctx.close().addListener( future -> handshakeCompletedPromise.tryFailure( error ) );
}

private static Throwable protocolNoSupportedByServerError()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@

import io.netty.channel.ChannelPromise;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.DecoderException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import javax.net.ssl.SSLHandshakeException;

import org.neo4j.driver.internal.async.inbound.ChunkDecoder;
import org.neo4j.driver.internal.async.inbound.InboundMessageDispatcher;
import org.neo4j.driver.internal.async.inbound.InboundMessageHandler;
import org.neo4j.driver.internal.async.inbound.MessageDecoder;
import org.neo4j.driver.internal.async.outbound.OutboundMessageHandler;
import org.neo4j.driver.internal.util.ErrorUtil;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.exceptions.SecurityException;
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;

import static io.netty.buffer.Unpooled.copyInt;
import static org.hamcrest.Matchers.instanceOf;
Expand Down Expand Up @@ -87,6 +94,93 @@ public void shouldFailGivenPromiseWhenExceptionCaught()
assertNull( await( channel.closeFuture() ) );
}

@Test
public void shouldFailGivenPromiseWhenMultipleExceptionsCaught()
{
ChannelPromise handshakeCompletedPromise = channel.newPromise();
HandshakeHandler handler = newHandler( handshakeCompletedPromise );
channel.pipeline().addLast( handler );

RuntimeException error1 = new RuntimeException( "Error 1" );
RuntimeException error2 = new RuntimeException( "Error 2" );
channel.pipeline().fireExceptionCaught( error1 );
channel.pipeline().fireExceptionCaught( error2 );

try
{
// promise should fail
await( handshakeCompletedPromise );
fail( "Exception expected" );
}
catch ( RuntimeException e )
{
assertEquals( error1, e );
}

// channel should be closed
assertNull( await( channel.closeFuture() ) );

try
{
channel.checkException();
fail( "Exception expected" );
}
catch ( RuntimeException e )
{
assertEquals( error2, e );
}
}

@Test
public void shouldUnwrapDecoderException()
{
ChannelPromise handshakeCompletedPromise = channel.newPromise();
HandshakeHandler handler = newHandler( handshakeCompletedPromise );
channel.pipeline().addLast( handler );

IOException cause = new IOException( "Error!" );
channel.pipeline().fireExceptionCaught( new DecoderException( cause ) );

try
{
// promise should fail
await( handshakeCompletedPromise );
fail( "Exception expected" );
}
catch ( Exception e )
{
assertEquals( cause, e );
}

// channel should be closed
assertNull( await( channel.closeFuture() ) );
}

@Test
public void shouldTranslateSSLHandshakeException()
{
ChannelPromise handshakeCompletedPromise = channel.newPromise();
HandshakeHandler handler = newHandler( handshakeCompletedPromise );
channel.pipeline().addLast( handler );

SSLHandshakeException error = new SSLHandshakeException( "Invalid certificate" );
channel.pipeline().fireExceptionCaught( error );

try
{
// promise should fail
await( handshakeCompletedPromise );
fail( "Exception expected" );
}
catch ( SecurityException e )
{
assertEquals( error, e.getCause() );
}

// channel should be closed
assertNull( await( channel.closeFuture() ) );
}

@Test
public void shouldSelectProtocolV1WhenServerSuggests()
{
Expand Down Expand Up @@ -129,6 +223,30 @@ public void shouldFailGivenPromiseWhenServerSuggestsUnknownProtocol()
testFailure( 42, "Protocol error" );
}

@Test
public void shouldFailGivenPromiseWhenChannelInactive()
{
ChannelPromise handshakeCompletedPromise = channel.newPromise();
HandshakeHandler handler = newHandler( handshakeCompletedPromise );
channel.pipeline().addLast( handler );

channel.pipeline().fireChannelInactive();

try
{
// promise should fail
await( handshakeCompletedPromise );
fail( "Exception expected" );
}
catch ( ServiceUnavailableException e )
{
assertEquals( ErrorUtil.newConnectionTerminatedError().getMessage(), e.getMessage() );
}

// channel should be closed
assertNull( await( channel.closeFuture() ) );
}

private void testFailure( int serverSuggestedVersion, String expectedMessagePrefix )
{
ChannelPromise handshakeCompletedPromise = channel.newPromise();
Expand Down

0 comments on commit 12896b9

Please sign in to comment.