22
22
import org .mockito .Mockito ;
23
23
24
24
import java .io .IOException ;
25
+ import java .util .HashMap ;
25
26
import java .util .concurrent .BlockingQueue ;
26
27
import java .util .concurrent .atomic .AtomicBoolean ;
27
28
28
29
import org .neo4j .driver .internal .spi .Connection ;
30
+ import org .neo4j .driver .internal .spi .StreamCollector ;
29
31
import org .neo4j .driver .internal .util .Clock ;
30
32
import org .neo4j .driver .internal .util .Consumers ;
31
33
import org .neo4j .driver .v1 .Config ;
34
+ import org .neo4j .driver .v1 .Value ;
32
35
import org .neo4j .driver .v1 .exceptions .ClientException ;
33
36
import org .neo4j .driver .v1 .exceptions .Neo4jException ;
34
37
import org .neo4j .driver .v1 .exceptions .TransientException ;
38
41
import static org .hamcrest .MatcherAssert .assertThat ;
39
42
import static org .junit .Assert .assertTrue ;
40
43
import static org .junit .Assert .fail ;
44
+ import static org .mockito .Matchers .any ;
45
+ import static org .mockito .Matchers .anyMap ;
46
+ import static org .mockito .Matchers .anyString ;
47
+ import static org .mockito .Matchers .eq ;
41
48
import static org .mockito .Mockito .doThrow ;
42
49
import static org .mockito .Mockito .mock ;
43
50
import static org .mockito .Mockito .never ;
@@ -57,7 +64,8 @@ public class ConnectionInvalidationTest
57
64
public void shouldInvalidateConnectionThatIsOld () throws Throwable
58
65
{
59
66
// Given a connection that's broken
60
- Mockito .doThrow ( new ClientException ( "That didn't work" ) ).when ( delegate ).sync ();
67
+ Mockito .doThrow ( new ClientException ( "That didn't work" ) )
68
+ .when ( delegate ).run ( anyString (), anyMap (), any ( StreamCollector .class ) );
61
69
Config config = Config .defaultConfig ();
62
70
when ( clock .millis () ).thenReturn ( 0L , config .idleTimeBeforeConnectionTest () + 1L );
63
71
PooledConnection conn = new PooledConnection ( delegate , Consumers .<PooledConnection >noOp (), clock );
@@ -76,7 +84,8 @@ public void shouldInvalidateConnectionThatIsOld() throws Throwable
76
84
public void shouldNotInvalidateConnectionThatIsNotOld () throws Throwable
77
85
{
78
86
// Given a connection that's broken
79
- Mockito .doThrow ( new ClientException ( "That didn't work" ) ).when ( delegate ).sync ();
87
+ Mockito .doThrow ( new ClientException ( "That didn't work" ) )
88
+ .when ( delegate ).run ( anyString (), anyMap (), any ( StreamCollector .class ) );
80
89
Config config = Config .defaultConfig ();
81
90
when ( clock .millis () ).thenReturn ( 0L , config .idleTimeBeforeConnectionTest () - 1L );
82
91
PooledConnection conn = new PooledConnection ( delegate , Consumers .<PooledConnection >noOp (), clock );
@@ -90,6 +99,23 @@ public void shouldNotInvalidateConnectionThatIsNotOld() throws Throwable
90
99
verify ( queue ).offer ( conn );
91
100
}
92
101
102
+ @ Test
103
+ public void shouldInvalidConnectionIfFailedToReset () throws Throwable
104
+ {
105
+ // Given a connection that's broken
106
+ Mockito .doThrow ( new ClientException ( "That didn't work" ) ).when ( delegate ).reset ( any ( StreamCollector .class ) );
107
+ Config config = Config .defaultConfig ();
108
+ PooledConnection conn = new PooledConnection ( delegate , Consumers .<PooledConnection >noOp (), clock );
109
+
110
+ // When/Then
111
+ BlockingQueue <PooledConnection > queue = mock ( BlockingQueue .class );
112
+ PooledConnectionReleaseConsumer consumer =
113
+ new PooledConnectionReleaseConsumer ( queue , new AtomicBoolean ( false ), config );
114
+ consumer .accept ( conn );
115
+
116
+ verify ( queue , never () ).add ( conn );
117
+ }
118
+
93
119
@ Test
94
120
public void shouldInvalidateOnUnrecoverableProblems () throws Throwable
95
121
{
@@ -115,12 +141,13 @@ public void shouldInvalidateOnProtocolViolationExceptions() throws Throwable
115
141
@ SuppressWarnings ( "unchecked" )
116
142
private void assertUnrecoverable ( Neo4jException exception )
117
143
{
118
- doThrow ( exception ).when ( delegate ).sync ();
144
+ doThrow ( exception ).when ( delegate )
145
+ .run ( eq ("assert unrecoverable" ), anyMap (), any ( StreamCollector .class ) );
119
146
120
147
// When
121
148
try
122
149
{
123
- conn .sync ( );
150
+ conn .run ( "assert unrecoverable" , new HashMap < String , Value >( ), StreamCollector . NO_OP );
124
151
fail ( "Should've rethrown exception" );
125
152
}
126
153
catch ( Neo4jException e )
@@ -141,12 +168,12 @@ private void assertUnrecoverable( Neo4jException exception )
141
168
@ SuppressWarnings ( "unchecked" )
142
169
private void assertRecoverable ( Neo4jException exception )
143
170
{
144
- doThrow ( exception ).when ( delegate ).sync ( );
171
+ doThrow ( exception ).when ( delegate ).run ( eq ( "assert recoverable" ), anyMap (), any ( StreamCollector . class ) );
145
172
146
173
// When
147
174
try
148
175
{
149
- conn .sync ( );
176
+ conn .run ( "assert recoverable" , new HashMap < String , Value >( ), StreamCollector . NO_OP );
150
177
fail ( "Should've rethrown exception" );
151
178
}
152
179
catch ( Neo4jException e )
0 commit comments