19
19
package org .neo4j .driver .internal .async .pool ;
20
20
21
21
import io .netty .bootstrap .Bootstrap ;
22
- import io .netty .channel .Channel ;
23
- import io .netty .channel .pool .ChannelPool ;
24
- import io .netty .util .concurrent .ImmediateEventExecutor ;
25
22
import org .junit .jupiter .api .AfterEach ;
26
23
import org .junit .jupiter .api .BeforeEach ;
27
24
import org .junit .jupiter .api .Test ;
28
25
import org .junit .jupiter .api .extension .RegisterExtension ;
29
26
30
- import java .util .HashMap ;
31
- import java .util .HashSet ;
32
- import java .util .Map ;
33
-
34
27
import org .neo4j .driver .internal .BoltServerAddress ;
35
28
import org .neo4j .driver .internal .ConnectionSettings ;
36
29
import org .neo4j .driver .internal .async .BootstrapFactory ;
43
36
import org .neo4j .driver .v1 .util .DatabaseExtension ;
44
37
import org .neo4j .driver .v1 .util .ParallelizableIT ;
45
38
46
- import static java . util . Arrays . asList ;
47
- import static java . util . Collections . singleton ;
39
+ import static org . hamcrest . Matchers . containsString ;
40
+ import static org . hamcrest . Matchers . instanceOf ;
48
41
import static org .hamcrest .Matchers .startsWith ;
49
42
import static org .hamcrest .junit .MatcherAssert .assertThat ;
50
43
import static org .junit .jupiter .api .Assertions .assertNotNull ;
51
44
import static org .junit .jupiter .api .Assertions .assertNull ;
52
45
import static org .junit .jupiter .api .Assertions .assertThrows ;
53
46
import static org .junit .jupiter .api .Assertions .assertTrue ;
54
- import static org .mockito .Mockito .doReturn ;
55
- import static org .mockito .Mockito .mock ;
56
- import static org .mockito .Mockito .never ;
57
- import static org .mockito .Mockito .verify ;
58
- import static org .mockito .Mockito .verifyZeroInteractions ;
59
- import static org .mockito .Mockito .when ;
60
- import static org .neo4j .driver .internal .BoltServerAddress .LOCAL_DEFAULT ;
61
47
import static org .neo4j .driver .internal .logging .DevNullLogging .DEV_NULL_LOGGING ;
62
48
import static org .neo4j .driver .internal .metrics .InternalAbstractMetrics .DEV_NULL_METRICS ;
63
49
import static org .neo4j .driver .v1 .util .TestUtil .await ;
64
50
65
51
@ ParallelizableIT
66
52
class ConnectionPoolImplIT
67
53
{
68
- private static final BoltServerAddress ADDRESS_1 = new BoltServerAddress ( "server:1" );
69
- private static final BoltServerAddress ADDRESS_2 = new BoltServerAddress ( "server:2" );
70
- private static final BoltServerAddress ADDRESS_3 = new BoltServerAddress ( "server:3" );
71
-
72
54
@ RegisterExtension
73
55
static final DatabaseExtension neo4j = new DatabaseExtension ();
74
56
@@ -132,71 +114,16 @@ void shouldNotCloseWhenClosed()
132
114
}
133
115
134
116
@ Test
135
- void shouldDoNothingWhenRetainOnEmptyPool ()
136
- {
137
- NettyChannelTracker nettyChannelTracker = mock ( NettyChannelTracker .class );
138
- TestConnectionPool pool = new TestConnectionPool ( nettyChannelTracker );
139
-
140
- pool .retainAll ( singleton ( LOCAL_DEFAULT ) );
141
-
142
- verifyZeroInteractions ( nettyChannelTracker );
143
- }
144
-
145
- @ Test
146
- void shouldRetainSpecifiedAddresses ()
147
- {
148
- NettyChannelTracker nettyChannelTracker = mock ( NettyChannelTracker .class );
149
- TestConnectionPool pool = new TestConnectionPool ( nettyChannelTracker );
150
-
151
- pool .acquire ( ADDRESS_1 );
152
- pool .acquire ( ADDRESS_2 );
153
- pool .acquire ( ADDRESS_3 );
154
-
155
- pool .retainAll ( new HashSet <>( asList ( ADDRESS_1 , ADDRESS_2 , ADDRESS_3 ) ) );
156
- for ( ChannelPool channelPool : pool .channelPoolsByAddress .values () )
157
- {
158
- verify ( channelPool , never () ).close ();
159
- }
160
- }
161
-
162
- @ Test
163
- void shouldClosePoolsWhenRetaining ()
164
- {
165
- NettyChannelTracker nettyChannelTracker = mock ( NettyChannelTracker .class );
166
- TestConnectionPool pool = new TestConnectionPool ( nettyChannelTracker );
167
-
168
- pool .acquire ( ADDRESS_1 );
169
- pool .acquire ( ADDRESS_2 );
170
- pool .acquire ( ADDRESS_3 );
171
-
172
- when ( nettyChannelTracker .inUseChannelCount ( ADDRESS_1 ) ).thenReturn ( 2 );
173
- when ( nettyChannelTracker .inUseChannelCount ( ADDRESS_2 ) ).thenReturn ( 0 );
174
- when ( nettyChannelTracker .inUseChannelCount ( ADDRESS_3 ) ).thenReturn ( 3 );
175
-
176
- pool .retainAll ( new HashSet <>( asList ( ADDRESS_1 , ADDRESS_3 ) ) );
177
- verify ( pool .getPool ( ADDRESS_1 ), never () ).close ();
178
- verify ( pool .getPool ( ADDRESS_2 ) ).close ();
179
- verify ( pool .getPool ( ADDRESS_3 ), never () ).close ();
180
- }
181
-
182
- @ Test
183
- void shouldNotClosePoolsWithActiveConnectionsWhenRetaining ()
117
+ void shouldFailToAcquireConnectionWhenPoolIsClosed ()
184
118
{
185
- NettyChannelTracker nettyChannelTracker = mock ( NettyChannelTracker .class );
186
- TestConnectionPool pool = new TestConnectionPool ( nettyChannelTracker );
187
-
188
- pool .acquire ( ADDRESS_1 );
189
- pool .acquire ( ADDRESS_2 );
190
- pool .acquire ( ADDRESS_3 );
191
-
192
- when ( nettyChannelTracker .inUseChannelCount ( ADDRESS_1 ) ).thenReturn ( 1 );
193
- when ( nettyChannelTracker .inUseChannelCount ( ADDRESS_2 ) ).thenReturn ( 42 );
194
- when ( nettyChannelTracker .inUseChannelCount ( ADDRESS_3 ) ).thenReturn ( 0 );
195
-
196
- pool .retainAll ( singleton ( ADDRESS_2 ) );
197
- verify ( pool .getPool ( ADDRESS_1 ), never () ).close ();
198
- verify ( pool .getPool ( ADDRESS_2 ), never () ).close ();
199
- verify ( pool .getPool ( ADDRESS_3 ) ).close ();
119
+ await ( pool .acquire ( neo4j .address () ) );
120
+ ExtendedChannelPool channelPool = this .pool .getPool ( neo4j .address () );
121
+ channelPool .close ();
122
+ ServiceUnavailableException error =
123
+ assertThrows ( ServiceUnavailableException .class , () -> await ( pool .acquire ( neo4j .address () ) ) );
124
+ assertThat ( error .getMessage (), containsString ( "closed while acquiring a connection" ) );
125
+ assertThat ( error .getCause (), instanceOf ( IllegalStateException .class ) );
126
+ assertThat ( error .getCause ().getMessage (), containsString ( "FixedChannelPooled was closed" ) );
200
127
}
201
128
202
129
private ConnectionPoolImpl newPool () throws Exception
@@ -209,37 +136,8 @@ private ConnectionPoolImpl newPool() throws Exception
209
136
Bootstrap bootstrap = BootstrapFactory .newBootstrap ( 1 );
210
137
return new ConnectionPoolImpl ( connector , bootstrap , poolSettings , DEV_NULL_METRICS , DEV_NULL_LOGGING , clock );
211
138
}
212
-
213
139
private static PoolSettings newSettings ()
214
140
{
215
141
return new PoolSettings ( 10 , 5000 , -1 , -1 );
216
142
}
217
-
218
- private static class TestConnectionPool extends ConnectionPoolImpl
219
- {
220
- final Map <BoltServerAddress ,ChannelPool > channelPoolsByAddress = new HashMap <>();
221
-
222
- TestConnectionPool ( NettyChannelTracker nettyChannelTracker )
223
- {
224
- super ( mock ( ChannelConnector .class ), mock ( Bootstrap .class ), nettyChannelTracker , newSettings (),
225
- DEV_NULL_METRICS , DEV_NULL_LOGGING , new FakeClock () );
226
- }
227
-
228
- ChannelPool getPool ( BoltServerAddress address )
229
- {
230
- ChannelPool pool = channelPoolsByAddress .get ( address );
231
- assertNotNull ( pool );
232
- return pool ;
233
- }
234
-
235
- @ Override
236
- ChannelPool newPool ( BoltServerAddress address )
237
- {
238
- ChannelPool channelPool = mock ( ChannelPool .class );
239
- Channel channel = mock ( Channel .class );
240
- doReturn ( ImmediateEventExecutor .INSTANCE .newSucceededFuture ( channel ) ).when ( channelPool ).acquire ();
241
- channelPoolsByAddress .put ( address , channelPool );
242
- return channelPool ;
243
- }
244
- }
245
143
}
0 commit comments