Skip to content

Fixed expect strust but get xx bug #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions driver/src/main/java/org/neo4j/driver/internal/InternalSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,38 @@ public boolean isOpen()
public void close()
{
// Use atomic operation to protect from closing the connection twice (putting back to the pool twice).
if( !isOpen.compareAndSet( true, false ) )
if ( !isOpen.compareAndSet( true, false ) )
{
throw new ClientException( "This session has already been closed." );
}
else

if( !connection.isOpen() )
{
// the socket connection is already closed due to some error, cannot send more data
connection.close();
return;
}

if ( currentTransaction != null )
{
if ( currentTransaction != null )
{
try
{
currentTransaction.close();
}
catch ( Throwable e )
{
// Best-effort
}
}
try
{
connection.sync();
currentTransaction.close();
}
finally
catch ( Throwable e )
{
connection.close();
// Best-effort
}
}
try
{
connection.sync();
}
finally
{
connection.close();
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ else if ( buffer.remaining() >= 2 )
* @param buffer The buffer to read into
* @throws IOException
*/
private static void readNextPacket( ReadableByteChannel channel, ByteBuffer buffer ) throws IOException
static void readNextPacket( ReadableByteChannel channel, ByteBuffer buffer ) throws IOException
{
assert !buffer.hasRemaining();

Expand All @@ -408,11 +408,18 @@ private static void readNextPacket( ReadableByteChannel channel, ByteBuffer buff
int read = channel.read( buffer );
if ( read == -1 )
{
try
{
channel.close();
}
catch ( IOException e )
{
// best effort
}
throw new ClientException(
"Connection terminated while receiving data. This can happen due to network " +
"instabilities, or due to restarts of the database." );
}
buffer.flip();
}
catch ( ClosedByInterruptException e )
{
Expand All @@ -429,6 +436,10 @@ private static void readNextPacket( ReadableByteChannel channel, ByteBuffer buff
throw new ClientException(
"Unable to process request: " + message + " buffer: \n" + BytePrinter.hex( buffer ), e );
}
finally
{
buffer.flip();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public static void blockingRead(ByteChannel channel, ByteBuffer buf) throws IOEx
{
if (channel.read( buf ) < 0)
{
try
{
channel.close();
}
catch ( IOException e )
{
// best effort
}
String bufStr = BytePrinter.hex( buf ).trim();
throw new ClientException( String.format(
"Connection terminated while receiving data. This can happen due to network " +
Expand All @@ -56,6 +64,14 @@ public static void blockingWrite(ByteChannel channel, ByteBuffer buf) throws IOE
{
if (channel.write( buf ) < 0)
{
try
{
channel.close();
}
catch ( IOException e )
{
// best effort
}
String bufStr = BytePrinter.hex( buf ).trim();
throw new ClientException( String.format(
"Connection terminated while sending data. This can happen due to network " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -491,6 +492,28 @@ public void shouldFailNicelyOnClosedConnections() throws IOException
input.readByte();
}


@Test
public void shouldKeepBufferCorrectWhenError() throws Throwable
{
// Given
ReadableByteChannel channel = mock( ReadableByteChannel.class );
when( channel.read( any( ByteBuffer.class ) ) ).thenReturn( -1 );
ByteBuffer buffer = ByteBuffer.allocate( 8 );
buffer.limit(0);

//Expect
exception.expect( ClientException.class );
exception.expectMessage( "Connection terminated while receiving data. This can happen due to network " +
"instabilities, or due to restarts of the database." );
// When
BufferingChunkedInput.readNextPacket( channel, buffer );
assertEquals( buffer.position(), 0 );
assertEquals( buffer.limit(), 0 );
assertEquals( buffer.capacity(), 8 );
assertFalse( channel.isOpen() );
}

private ReadableByteChannel fillPacket( int size, int value )
{
int[] ints = new int[size];
Expand Down Expand Up @@ -541,4 +564,4 @@ public void close() throws IOException
};
}

}
}