22
22
import org .mockito .invocation .InvocationOnMock ;
23
23
import org .mockito .stubbing .Answer ;
24
24
25
+ import java .io .IOException ;
25
26
import java .net .URI ;
26
27
import java .util .ArrayList ;
27
28
import java .util .Iterator ;
29
+ import java .util .Queue ;
28
30
29
31
import org .neo4j .driver .internal .messaging .Message ;
30
32
import org .neo4j .driver .internal .messaging .SuccessMessage ;
31
33
import org .neo4j .driver .internal .summary .InternalServerInfo ;
32
- import org .neo4j .driver .v1 .Logger ;
33
34
import org .neo4j .driver .v1 .Values ;
35
+ import org .neo4j .driver .v1 .exceptions .ServiceUnavailableException ;
34
36
import org .neo4j .driver .v1 .summary .ServerInfo ;
35
37
36
38
import static org .hamcrest .CoreMatchers .equalTo ;
37
39
import static org .hamcrest .CoreMatchers .instanceOf ;
40
+ import static org .junit .Assert .assertSame ;
38
41
import static org .junit .Assert .assertThat ;
39
42
import static org .junit .Assert .fail ;
40
43
import static org .mockito .Matchers .any ;
45
48
import static org .mockito .Mockito .times ;
46
49
import static org .mockito .Mockito .verify ;
47
50
import 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 ;
48
53
import static org .neo4j .driver .v1 .Values .parameters ;
49
54
50
55
public class SocketConnectionTest
51
56
{
57
+ private static final InternalServerInfo SERVER_INFO = new InternalServerInfo ( LOCAL_DEFAULT , "test" );
58
+
52
59
@ Test
53
60
public void shouldReceiveServerInfoAfterInit () throws Throwable
54
61
{
55
62
// Given
56
63
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 );
58
65
59
66
when ( socket .address () ).thenReturn ( BoltServerAddress .from ( URI .create ( "http://neo4j.com:9000" ) ) );
60
67
@@ -98,7 +105,7 @@ public void shouldCloseConnectionIfFailedToCreate() throws Throwable
98
105
// Then
99
106
try
100
107
{
101
- SocketConnection conn = new SocketConnection ( socket , mock ( InternalServerInfo . class ), mock ( Logger . class ) );
108
+ new SocketConnection ( socket , SERVER_INFO , DEV_NULL_LOGGER );
102
109
fail ( "should have failed with the provided exception" );
103
110
}
104
111
catch ( Throwable e )
@@ -108,4 +115,26 @@ public void shouldCloseConnectionIfFailedToCreate() throws Throwable
108
115
}
109
116
verify ( socket , times ( 1 ) ).stop ();
110
117
}
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
+ }
111
140
}
0 commit comments