2222import org .mockito .invocation .InvocationOnMock ;
2323import org .mockito .stubbing .Answer ;
2424
25+ import java .io .IOException ;
2526import java .net .URI ;
2627import java .util .ArrayList ;
2728import java .util .Iterator ;
29+ import java .util .Queue ;
2830
2931import org .neo4j .driver .internal .messaging .Message ;
3032import org .neo4j .driver .internal .messaging .SuccessMessage ;
3133import org .neo4j .driver .internal .summary .InternalServerInfo ;
32- import org .neo4j .driver .v1 .Logger ;
3334import org .neo4j .driver .v1 .Values ;
35+ import org .neo4j .driver .v1 .exceptions .ServiceUnavailableException ;
3436import org .neo4j .driver .v1 .summary .ServerInfo ;
3537
3638import static org .hamcrest .CoreMatchers .equalTo ;
3739import static org .hamcrest .CoreMatchers .instanceOf ;
40+ import static org .junit .Assert .assertSame ;
3841import static org .junit .Assert .assertThat ;
3942import static org .junit .Assert .fail ;
4043import static org .mockito .Matchers .any ;
4548import static org .mockito .Mockito .times ;
4649import static org .mockito .Mockito .verify ;
4750import static org .mockito .Mockito .when ;
51+ import static org .neo4j .driver .internal .logging .DevNullLogger .DEV_NULL_LOGGER ;
52+ import static org .neo4j .driver .internal .net .BoltServerAddress .LOCAL_DEFAULT ;
4853import static org .neo4j .driver .v1 .Values .parameters ;
4954
5055public class SocketConnectionTest
5156{
57+ private static final InternalServerInfo SERVER_INFO = new InternalServerInfo ( LOCAL_DEFAULT , "test" );
58+
5259 @ Test
5360 public void shouldReceiveServerInfoAfterInit () throws Throwable
5461 {
5562 // Given
5663 SocketClient socket = mock ( SocketClient .class );
57- SocketConnection conn = new SocketConnection ( socket , mock ( InternalServerInfo . class ), mock ( Logger . class ) );
64+ SocketConnection conn = new SocketConnection ( socket , SERVER_INFO , DEV_NULL_LOGGER );
5865
5966 when ( socket .address () ).thenReturn ( BoltServerAddress .from ( URI .create ( "http://neo4j.com:9000" ) ) );
6067
@@ -98,7 +105,7 @@ public void shouldCloseConnectionIfFailedToCreate() throws Throwable
98105 // Then
99106 try
100107 {
101- SocketConnection conn = new SocketConnection ( socket , mock ( InternalServerInfo . class ), mock ( Logger . class ) );
108+ new SocketConnection ( socket , SERVER_INFO , DEV_NULL_LOGGER );
102109 fail ( "should have failed with the provided exception" );
103110 }
104111 catch ( Throwable e )
@@ -108,4 +115,26 @@ public void shouldCloseConnectionIfFailedToCreate() throws Throwable
108115 }
109116 verify ( socket , times ( 1 ) ).stop ();
110117 }
118+
119+ @ Test
120+ @ SuppressWarnings ( "unchecked" )
121+ public void flushThrowsWhenSocketIsBroken () throws Exception
122+ {
123+ SocketClient socket = mock ( SocketClient .class );
124+ IOException sendError = new IOException ( "Unable to send" );
125+ doThrow ( sendError ).when ( socket ).send ( any ( Queue .class ) );
126+
127+ SocketConnection connection = new SocketConnection ( socket , SERVER_INFO , DEV_NULL_LOGGER );
128+
129+ try
130+ {
131+ connection .flush ();
132+ fail ( "Exception expected" );
133+ }
134+ catch ( Exception e )
135+ {
136+ assertThat ( e , instanceOf ( ServiceUnavailableException .class ) );
137+ assertSame ( sendError , e .getCause () );
138+ }
139+ }
111140}
0 commit comments