1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import static org .awaitility .Awaitility .with ;
23
23
import static org .mockito .ArgumentMatchers .any ;
24
24
import static org .mockito .ArgumentMatchers .contains ;
25
+ import static org .mockito .BDDMockito .willReturn ;
25
26
import static org .mockito .Mockito .doAnswer ;
26
27
import static org .mockito .Mockito .doReturn ;
27
28
import static org .mockito .Mockito .mock ;
31
32
import java .io .ByteArrayOutputStream ;
32
33
import java .io .IOException ;
33
34
import java .io .InputStream ;
34
- import java .lang .reflect .Field ;
35
35
import java .lang .reflect .Method ;
36
36
import java .net .ConnectException ;
37
37
import java .net .ServerSocket ;
91
91
import org .springframework .messaging .MessagingException ;
92
92
import org .springframework .messaging .support .ErrorMessage ;
93
93
import org .springframework .scheduling .concurrent .ThreadPoolTaskExecutor ;
94
- import org .springframework .util .ReflectionUtils ;
95
94
import org .springframework .util .StopWatch ;
96
95
97
96
@@ -248,10 +247,9 @@ public void testMemoryLeak(TestInfo testInfo) throws Exception {
248
247
@ Test
249
248
@ DisabledIfEnvironmentVariable (named = "bamboo_buildKey" , matches = ".*?" ,
250
249
disabledReason = "Timing is too short for CI" )
251
- public void testCleanup () throws Exception {
250
+ public void testCleanup () {
252
251
TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory ("localhost" , 0 );
253
252
factory .setApplicationEventPublisher (nullPublisher );
254
- factory .setNioHarvestInterval (100 );
255
253
Map <SocketChannel , TcpNioConnection > connections = new HashMap <>();
256
254
SocketChannel chan1 = mock (SocketChannel .class );
257
255
SocketChannel chan2 = mock (SocketChannel .class );
@@ -262,49 +260,41 @@ public void testCleanup() throws Exception {
262
260
connections .put (chan1 , conn1 );
263
261
connections .put (chan2 , conn2 );
264
262
connections .put (chan3 , conn3 );
265
- boolean java8 = System .getProperty ("java.version" ).startsWith ("1.8" );
266
- final List <Field > fields = new ArrayList <>();
267
- if (java8 ) {
268
- ReflectionUtils .doWithFields (SocketChannel .class , field -> {
269
- field .setAccessible (true );
270
- fields .add (field );
271
- }, field -> field .getName ().equals ("open" ));
272
- }
273
- else {
274
- ReflectionUtils .doWithFields (SocketChannel .class , field -> {
275
- field .setAccessible (true );
276
- fields .add (field );
277
- }, field -> field .getName ().equals ("closed" ));
278
- }
279
- Field field = fields .get (0 );
280
- // Can't use Mockito because isOpen() is final
281
- ReflectionUtils .setField (field , chan1 , java8 );
282
- ReflectionUtils .setField (field , chan2 , java8 );
283
- ReflectionUtils .setField (field , chan3 , java8 );
263
+ willReturn (true ).given (chan1 ).isOpen ();
264
+ willReturn (true ).given (chan2 ).isOpen ();
265
+ willReturn (true ).given (chan3 ).isOpen ();
284
266
Selector selector = mock (Selector .class );
285
267
HashSet <SelectionKey > keys = new HashSet <>();
286
268
when (selector .selectedKeys ()).thenReturn (keys );
287
269
factory .processNioSelections (1 , selector , null , connections );
288
270
assertThat (connections .size ()).isEqualTo (3 ); // all open
289
271
290
- ReflectionUtils .setField (field , chan1 , !java8 );
272
+ DirectFieldAccessor factoryFieldAccessor = new DirectFieldAccessor (factory );
273
+
274
+ willReturn (false ).given (chan1 ).isOpen ();
291
275
factory .processNioSelections (1 , selector , null , connections );
292
276
assertThat (connections .size ()).isEqualTo (3 ); // interval didn't pass
293
- Thread .sleep (110 );
277
+
278
+ factoryFieldAccessor .setPropertyValue ("nextCheckForClosedNioConnections" , System .currentTimeMillis () - 10 );
279
+
294
280
factory .processNioSelections (1 , selector , null , connections );
295
281
assertThat (connections .size ()).isEqualTo (2 ); // first is closed
296
282
297
- ReflectionUtils . setField ( field , chan2 , ! java8 );
283
+ willReturn ( false ). given ( chan2 ). isOpen ( );
298
284
factory .processNioSelections (1 , selector , null , connections );
299
285
assertThat (connections .size ()).isEqualTo (2 ); // interval didn't pass
300
- Thread .sleep (110 );
286
+
287
+ factoryFieldAccessor .setPropertyValue ("nextCheckForClosedNioConnections" , System .currentTimeMillis () - 10 );
288
+
301
289
factory .processNioSelections (1 , selector , null , connections );
302
290
assertThat (connections .size ()).isEqualTo (1 ); // second is closed
303
291
304
- ReflectionUtils . setField ( field , chan3 , ! java8 );
292
+ willReturn ( false ). given ( chan3 ). isOpen ( );
305
293
factory .processNioSelections (1 , selector , null , connections );
306
294
assertThat (connections .size ()).isEqualTo (1 ); // interval didn't pass
307
- Thread .sleep (110 );
295
+
296
+ factoryFieldAccessor .setPropertyValue ("nextCheckForClosedNioConnections" , System .currentTimeMillis () - 10 );
297
+
308
298
factory .processNioSelections (1 , selector , null , connections );
309
299
assertThat (connections .size ()).isEqualTo (0 ); // third is closed
310
300
0 commit comments