Skip to content

Commit accb44b

Browse files
author
Zhen
committed
Adding test for the buffer change
1 parent 7572f5c commit accb44b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

driver/src/main/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ else if ( buffer.remaining() >= 2 )
398398
* @param buffer The buffer to read into
399399
* @throws IOException
400400
*/
401-
private static void readNextPacket( ReadableByteChannel channel, ByteBuffer buffer ) throws IOException
401+
static void readNextPacket( ReadableByteChannel channel, ByteBuffer buffer ) throws IOException
402402
{
403403
assert !buffer.hasRemaining();
404404

driver/src/test/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInputTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.hamcrest.CoreMatchers.equalTo;
3737
import static org.hamcrest.MatcherAssert.assertThat;
3838
import static org.junit.Assert.assertEquals;
39+
import static org.junit.Assert.assertFalse;
3940
import static org.junit.Assert.fail;
4041
import static org.mockito.Matchers.any;
4142
import static org.mockito.Mockito.mock;
@@ -491,6 +492,28 @@ public void shouldFailNicelyOnClosedConnections() throws IOException
491492
input.readByte();
492493
}
493494

495+
496+
@Test
497+
public void shouldKeepBufferCorrectWhenError() throws Throwable
498+
{
499+
// Given
500+
ReadableByteChannel channel = mock( ReadableByteChannel.class );
501+
when( channel.read( any( ByteBuffer.class ) ) ).thenReturn( -1 );
502+
ByteBuffer buffer = ByteBuffer.allocate( 8 );
503+
buffer.limit(0);
504+
505+
//Expect
506+
exception.expect( ClientException.class );
507+
exception.expectMessage( "Connection terminated while receiving data. This can happen due to network " +
508+
"instabilities, or due to restarts of the database." );
509+
// When
510+
BufferingChunkedInput.readNextPacket( channel, buffer );
511+
assertEquals( buffer.position(), 0 );
512+
assertEquals( buffer.limit(), 0 );
513+
assertEquals( buffer.capacity(), 8 );
514+
assertFalse( channel.isOpen() );
515+
}
516+
494517
private ReadableByteChannel fillPacket( int size, int value )
495518
{
496519
int[] ints = new int[size];
@@ -541,4 +564,4 @@ public void close() throws IOException
541564
};
542565
}
543566

544-
}
567+
}

0 commit comments

Comments
 (0)