21
21
import org .junit .Test ;
22
22
import org .mockito .InOrder ;
23
23
24
- import java .util .HashSet ;
24
+ import java .util .Collections ;
25
+ import java .util .Set ;
25
26
import java .util .concurrent .atomic .AtomicInteger ;
26
27
28
+ import org .neo4j .driver .internal .RoutingTransaction ;
27
29
import org .neo4j .driver .internal .net .BoltServerAddress ;
28
30
import org .neo4j .driver .internal .spi .Connection ;
29
31
import org .neo4j .driver .internal .spi .ConnectionPool ;
32
+ import org .neo4j .driver .v1 .AccessMode ;
33
+ import org .neo4j .driver .v1 .Transaction ;
34
+ import org .neo4j .driver .v1 .exceptions .ServiceUnavailableException ;
35
+ import org .neo4j .driver .v1 .exceptions .SessionExpiredException ;
30
36
31
- import static java .util .Arrays .asList ;
32
37
import static org .hamcrest .MatcherAssert .assertThat ;
38
+ import static org .hamcrest .Matchers .instanceOf ;
33
39
import static org .hamcrest .core .IsEqual .equalTo ;
40
+ import static org .junit .Assert .assertNotNull ;
41
+ import static org .junit .Assert .fail ;
34
42
import static org .mockito .Matchers .any ;
43
+ import static org .mockito .Mockito .doThrow ;
35
44
import static org .mockito .Mockito .inOrder ;
36
45
import static org .mockito .Mockito .mock ;
37
46
import static org .mockito .Mockito .spy ;
@@ -49,27 +58,28 @@ public void ensureRoutingShouldUpdateRoutingTableAndPurgeConnectionPoolWhenStale
49
58
RoutingTable routingTable = mock ( RoutingTable .class );
50
59
Rediscovery rediscovery = mock ( Rediscovery .class );
51
60
when ( routingTable .isStale () ).thenReturn ( true );
52
- HashSet <BoltServerAddress > set = new HashSet <>( asList ( new BoltServerAddress ( "abc" , 12 ) ) );
61
+ Set <BoltServerAddress > set = Collections . singleton ( new BoltServerAddress ( "abc" , 12 ) );
53
62
when ( routingTable .update ( any ( ClusterComposition .class ) ) ).thenReturn ( set );
54
63
55
64
// when
56
- LoadBalancer balancer = new LoadBalancer ( DEV_NULL_LOGGER , conns , routingTable , rediscovery );
65
+ LoadBalancer balancer = new LoadBalancer ( routingTable , conns , rediscovery , DEV_NULL_LOGGER );
57
66
58
67
// then
68
+ assertNotNull ( balancer );
59
69
InOrder inOrder = inOrder ( rediscovery , routingTable , conns );
60
70
inOrder .verify ( rediscovery ).lookupRoutingTable ( conns , routingTable );
61
71
inOrder .verify ( routingTable ).update ( any ( ClusterComposition .class ) );
62
72
inOrder .verify ( conns ).purge ( new BoltServerAddress ( "abc" , 12 ) );
63
73
}
64
74
65
-
66
75
@ Test
67
76
public void shouldEnsureRoutingOnInitialization () throws Exception
68
77
{
69
78
// given & when
70
79
final AtomicInteger ensureRoutingCounter = new AtomicInteger ( 0 );
71
- LoadBalancer balancer = new LoadBalancer ( DEV_NULL_LOGGER , mock ( ConnectionPool .class ),
72
- mock ( RoutingTable .class ), mock ( Rediscovery .class ) ) {
80
+ LoadBalancer balancer = new LoadBalancer ( mock ( RoutingTable .class ), mock ( ConnectionPool .class ),
81
+ mock ( Rediscovery .class ), DEV_NULL_LOGGER )
82
+ {
73
83
@ Override
74
84
public void ensureRouting ()
75
85
{
@@ -78,6 +88,7 @@ public void ensureRouting()
78
88
};
79
89
80
90
// then
91
+ assertNotNull ( balancer );
81
92
assertThat ( ensureRoutingCounter .get (), equalTo ( 1 ) );
82
93
}
83
94
@@ -129,9 +140,36 @@ private LoadBalancer setupLoadBalancer( Connection writerConn, Connection readCo
129
140
when ( routingTable .readers () ).thenReturn ( readerAddrs );
130
141
when ( routingTable .writers () ).thenReturn ( writerAddrs );
131
142
132
- LoadBalancer balancer = new LoadBalancer ( DEV_NULL_LOGGER , connPool ,
133
- routingTable , mock ( Rediscovery . class ) ) ;
143
+ return new LoadBalancer ( routingTable , connPool , mock ( Rediscovery . class ), DEV_NULL_LOGGER );
144
+ }
134
145
135
- return balancer ;
146
+ @ Test
147
+ public void shouldForgetAddressAndItsConnectionsOnServiceUnavailable ()
148
+ {
149
+ Transaction tx = mock ( Transaction .class );
150
+ RoutingTable routingTable = mock ( RoutingTable .class );
151
+ ConnectionPool connectionPool = mock ( ConnectionPool .class );
152
+ Rediscovery rediscovery = mock ( Rediscovery .class );
153
+ LoadBalancer loadBalancer = new LoadBalancer ( routingTable , connectionPool , rediscovery , DEV_NULL_LOGGER );
154
+ BoltServerAddress address = new BoltServerAddress ( "host" , 42 );
155
+
156
+ RoutingTransaction routingTx = new RoutingTransaction ( tx , AccessMode .WRITE , address , loadBalancer );
157
+
158
+ ServiceUnavailableException txCloseError = new ServiceUnavailableException ( "Oh!" );
159
+ doThrow ( txCloseError ).when ( tx ).close ();
160
+
161
+ try
162
+ {
163
+ routingTx .close ();
164
+ fail ( "Exception expected" );
165
+ }
166
+ catch ( Exception e )
167
+ {
168
+ assertThat ( e , instanceOf ( SessionExpiredException .class ) );
169
+ assertThat ( e .getCause (), instanceOf ( ServiceUnavailableException .class ) );
170
+ }
171
+
172
+ verify ( routingTable ).forget ( address );
173
+ verify ( connectionPool ).purge ( address );
136
174
}
137
175
}
0 commit comments