5050import org .apache .hc .client5 .http .io .ManagedHttpClientConnection ;
5151import org .apache .hc .client5 .http .protocol .HttpClientContext ;
5252import org .apache .hc .client5 .http .ssl .TlsSocketStrategy ;
53+ import org .apache .hc .core5 .concurrent .FutureCallback ;
5354import org .apache .hc .core5 .http .HttpHost ;
5455import org .apache .hc .core5 .http .config .Lookup ;
5556import org .apache .hc .core5 .http .io .SocketConfig ;
6061import org .junit .jupiter .api .Assertions ;
6162import org .junit .jupiter .api .BeforeEach ;
6263import org .junit .jupiter .api .Test ;
64+ import org .mockito .ArgumentCaptor ;
6365import org .mockito .Mock ;
6466import org .mockito .Mockito ;
6567import org .mockito .MockitoAnnotations ;
@@ -112,10 +114,10 @@ void testLeaseRelease() throws Exception {
112114 Mockito .when (conn .isConsistent ()).thenReturn (true );
113115 Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenReturn (entry );
114116 Mockito .when (pool .lease (
115- Mockito .eq (route ),
116- Mockito .eq (null ),
117- Mockito .any (),
118- Mockito .eq ( null )))
117+ Mockito .eq (route ),
118+ Mockito .eq (null ),
119+ Mockito .any (),
120+ Mockito .any ( FutureCallback . class )))
119121 .thenReturn (future );
120122
121123 final LeaseRequest connRequest1 = mgr .lease ("some-id" , route , null );
@@ -137,10 +139,10 @@ void testReleaseRouteIncomplete() throws Exception {
137139
138140 Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenReturn (entry );
139141 Mockito .when (pool .lease (
140- Mockito .eq (route ),
141- Mockito .eq (null ),
142- Mockito .any (),
143- Mockito .eq ( null )))
142+ Mockito .eq (route ),
143+ Mockito .eq (null ),
144+ Mockito .any (),
145+ Mockito .any ( FutureCallback . class )))
144146 .thenReturn (future );
145147
146148 final LeaseRequest connRequest1 = mgr .lease ("some-id" , route , null );
@@ -160,10 +162,10 @@ void testLeaseFutureTimeout() throws Exception {
160162
161163 Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenThrow (new TimeoutException ());
162164 Mockito .when (pool .lease (
163- Mockito .eq (route ),
164- Mockito .eq (null ),
165- Mockito .any (),
166- Mockito .eq ( null )))
165+ Mockito .eq (route ),
166+ Mockito .eq (null ),
167+ Mockito .any (),
168+ Mockito .any ( FutureCallback . class )))
167169 .thenReturn (future );
168170
169171 final LeaseRequest connRequest1 = mgr .lease ("some-id" , route , null );
@@ -177,23 +179,27 @@ void testLeaseFutureTimeoutLateLeaseReleased() throws Exception {
177179 final HttpRoute route = new HttpRoute (target );
178180 final PoolEntry <HttpRoute , ManagedHttpClientConnection > entry = new PoolEntry <>(route , TimeValue .NEG_ONE_MILLISECOND );
179181
180- Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenThrow (new TimeoutException ());
181- Mockito .when (future .cancel (true )).thenReturn (false );
182- Mockito .when (future .get (0L , TimeUnit .MILLISECONDS )).thenReturn (entry );
182+ final ArgumentCaptor <FutureCallback > callbackCaptor = ArgumentCaptor .forClass (FutureCallback .class );
183+
183184 Mockito .when (pool .lease (
184- Mockito .eq (route ),
185- Mockito .eq (null ),
186- Mockito .any (),
187- Mockito . eq ( null )))
185+ Mockito .eq (route ),
186+ Mockito .eq (null ),
187+ Mockito .any (),
188+ callbackCaptor . capture ( )))
188189 .thenReturn (future );
190+ Mockito .when (future .cancel (true )).thenReturn (false );
191+ Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenAnswer (invocation -> {
192+ callbackCaptor .getValue ().completed (entry );
193+ throw new TimeoutException ();
194+ });
189195
190196 final LeaseRequest connRequest1 = mgr .lease ("some-id" , route , null );
191197 Assertions .assertThrows (TimeoutException .class , () ->
192198 connRequest1 .get (Timeout .ofSeconds (1 )));
193199
194200 Mockito .verify (future ).cancel (true );
195- Mockito .verify (future ).get (0L , TimeUnit .MILLISECONDS );
196201 Mockito .verify (pool ).release (entry , false );
202+ Mockito .verify (future , Mockito .never ()).get (0L , TimeUnit .MILLISECONDS );
197203 }
198204
199205 @ Test
@@ -202,23 +208,27 @@ void testLeaseFutureInterruptedLateLeaseReleased() throws Exception {
202208 final HttpRoute route = new HttpRoute (target );
203209 final PoolEntry <HttpRoute , ManagedHttpClientConnection > entry = new PoolEntry <>(route , TimeValue .NEG_ONE_MILLISECOND );
204210
205- Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenThrow (new InterruptedException ());
206- Mockito .when (future .cancel (true )).thenReturn (false );
207- Mockito .when (future .get (0L , TimeUnit .MILLISECONDS )).thenReturn (entry );
211+ final ArgumentCaptor <FutureCallback > callbackCaptor = ArgumentCaptor .forClass (FutureCallback .class );
212+
208213 Mockito .when (pool .lease (
209- Mockito .eq (route ),
210- Mockito .eq (null ),
211- Mockito .any (),
212- Mockito . eq ( null )))
214+ Mockito .eq (route ),
215+ Mockito .eq (null ),
216+ Mockito .any (),
217+ callbackCaptor . capture ( )))
213218 .thenReturn (future );
219+ Mockito .when (future .cancel (true )).thenReturn (false );
220+ Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenAnswer (invocation -> {
221+ callbackCaptor .getValue ().completed (entry );
222+ throw new InterruptedException ();
223+ });
214224
215225 final LeaseRequest connRequest1 = mgr .lease ("some-id" , route , null );
216226 Assertions .assertThrows (InterruptedException .class , () ->
217227 connRequest1 .get (Timeout .ofSeconds (1 )));
218228
219229 Mockito .verify (future ).cancel (true );
220- Mockito .verify (future ).get (0L , TimeUnit .MILLISECONDS );
221230 Mockito .verify (pool ).release (entry , false );
231+ Mockito .verify (future , Mockito .never ()).get (0L , TimeUnit .MILLISECONDS );
222232 }
223233
224234 @ Test
@@ -231,10 +241,10 @@ void testReleaseReusable() throws Exception {
231241
232242 Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenReturn (entry );
233243 Mockito .when (pool .lease (
234- Mockito .eq (route ),
235- Mockito .eq (null ),
236- Mockito .any (),
237- Mockito .eq ( null )))
244+ Mockito .eq (route ),
245+ Mockito .eq (null ),
246+ Mockito .any (),
247+ Mockito .any ( FutureCallback . class )))
238248 .thenReturn (future );
239249 Mockito .when (conn .isOpen ()).thenReturn (true );
240250 Mockito .when (conn .isConsistent ()).thenReturn (true );
@@ -260,10 +270,10 @@ void testReleaseNonReusable() throws Exception {
260270
261271 Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenReturn (entry );
262272 Mockito .when (pool .lease (
263- Mockito .eq (route ),
264- Mockito .eq (null ),
265- Mockito .any (),
266- Mockito .eq ( null )))
273+ Mockito .eq (route ),
274+ Mockito .eq (null ),
275+ Mockito .any (),
276+ Mockito .any ( FutureCallback . class )))
267277 .thenReturn (future );
268278 Mockito .when (conn .isOpen ()).thenReturn (Boolean .FALSE );
269279
@@ -291,10 +301,10 @@ void testTargetConnect() throws Exception {
291301 Mockito .when (conn .isOpen ()).thenReturn (false );
292302 Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenReturn (entry );
293303 Mockito .when (pool .lease (
294- Mockito .eq (route ),
295- Mockito .eq (null ),
296- Mockito .any (),
297- Mockito .eq ( null )))
304+ Mockito .eq (route ),
305+ Mockito .eq (null ),
306+ Mockito .any (),
307+ Mockito .any ( FutureCallback . class )))
298308 .thenReturn (future );
299309
300310 final LeaseRequest connRequest1 = mgr .lease ("some-id" , route , null );
@@ -358,10 +368,10 @@ void testProxyConnectAndUpgrade() throws Exception {
358368 Mockito .when (conn .isOpen ()).thenReturn (false );
359369 Mockito .when (future .get (1 , TimeUnit .SECONDS )).thenReturn (entry );
360370 Mockito .when (pool .lease (
361- Mockito .eq (route ),
362- Mockito .eq (null ),
363- Mockito .any (),
364- Mockito .eq ( null )))
371+ Mockito .eq (route ),
372+ Mockito .eq (null ),
373+ Mockito .any (),
374+ Mockito .any ( FutureCallback . class )))
365375 .thenReturn (future );
366376
367377 final LeaseRequest connRequest1 = mgr .lease ("some-id" , route , null );
@@ -426,7 +436,6 @@ void testLeaseAfterShutdown() {
426436 }, "Attempting to lease a connection after shutdown should throw an exception." );
427437 }
428438
429-
430439 @ Test
431440 void testIsShutdown () {
432441 // Setup phase
@@ -442,7 +451,6 @@ void testIsShutdown() {
442451 Assertions .assertTrue (mgr .isClosed (), "Connection manager should be shutdown after close() is called." );
443452 }
444453
445-
446454 @ Test
447455 void testConcurrentShutdown () throws InterruptedException {
448456 final ExecutorService executor = Executors .newFixedThreadPool (2 );
@@ -455,5 +463,4 @@ void testConcurrentShutdown() throws InterruptedException {
455463 Assertions .assertTrue (mgr .isClosed (), "Connection manager should be shutdown after concurrent calls to shutdown." );
456464 }
457465
458-
459- }
466+ }
0 commit comments