36
36
import static org .hamcrest .CoreMatchers .equalTo ;
37
37
import static org .hamcrest .MatcherAssert .assertThat ;
38
38
import static org .junit .Assert .assertEquals ;
39
+ import static org .junit .Assert .assertFalse ;
39
40
import static org .junit .Assert .fail ;
40
41
import static org .mockito .Matchers .any ;
41
42
import static org .mockito .Mockito .mock ;
@@ -491,6 +492,28 @@ public void shouldFailNicelyOnClosedConnections() throws IOException
491
492
input .readByte ();
492
493
}
493
494
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
+
494
517
private ReadableByteChannel fillPacket ( int size , int value )
495
518
{
496
519
int [] ints = new int [size ];
@@ -541,4 +564,4 @@ public void close() throws IOException
541
564
};
542
565
}
543
566
544
- }
567
+ }
0 commit comments