3636import static org .hamcrest .CoreMatchers .equalTo ;
3737import static org .hamcrest .MatcherAssert .assertThat ;
3838import static org .junit .Assert .assertEquals ;
39+ import static org .junit .Assert .assertFalse ;
3940import static org .junit .Assert .fail ;
4041import static org .mockito .Matchers .any ;
4142import 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