3232import java .util .concurrent .ExecutorService ;
3333import java .util .concurrent .Executors ;
3434import java .util .concurrent .Future ;
35- import java .util .concurrent .TimeUnit ;
3635import java .util .concurrent .TimeoutException ;
3736import java .util .concurrent .atomic .AtomicLong ;
37+ import java .util .concurrent .atomic .AtomicReference ;
3838import java .util .function .Supplier ;
3939import java .util .stream .Stream ;
4040
@@ -115,6 +115,7 @@ void testLeaseTimeoutDoesNotLeakLeasedEntries(final PoolCase poolCase) throws Ex
115115 final AtomicLong n = new AtomicLong (concurrentThreads * 100 );
116116
117117 final ExecutorService executorService = Executors .newFixedThreadPool (concurrentThreads );
118+ final AtomicReference <Exception > unexpectedException = new AtomicReference <>();
118119 try {
119120 for (int i = 0 ; i < concurrentThreads ; i ++) {
120121 executorService .execute (() -> {
@@ -127,9 +128,14 @@ void testLeaseTimeoutDoesNotLeakLeasedEntries(final PoolCase poolCase) throws Ex
127128 pool .release (entry , true );
128129 } catch (final InterruptedException ex ) {
129130 Thread .currentThread ().interrupt ();
130- Assertions . fail ( "Unexpected exception" , ex );
131- } catch (final TimeoutException | ExecutionException ignored ) {
131+ unexpectedException . compareAndSet ( null , ex );
132+ } catch (final TimeoutException ex ) {
132133 f .cancel (true );
134+ } catch (final ExecutionException ex ) {
135+ f .cancel (true );
136+ if (!(ex .getCause () instanceof TimeoutException )) {
137+ unexpectedException .compareAndSet (null , ex );
138+ }
133139 }
134140 }
135141 } finally {
@@ -140,13 +146,14 @@ void testLeaseTimeoutDoesNotLeakLeasedEntries(final PoolCase poolCase) throws Ex
140146
141147 Assertions .assertTrue (countDownLatch .await (TIMEOUT .getDuration (), TIMEOUT .getTimeUnit ()));
142148 Assertions .assertTrue (n .get () <= 0 );
149+ Assertions .assertNull (unexpectedException .get ());
143150
144151 final PoolStats stats = pool .getStats (route );
145152 Assertions .assertEquals (0 , stats .getLeased ());
146153
147154 } finally {
148155 executorService .shutdownNow ();
149- executorService .awaitTermination (5 , TimeUnit . SECONDS );
156+ executorService .awaitTermination (TIMEOUT . getDuration (), TIMEOUT . getTimeUnit () );
150157 pool .close (CloseMode .GRACEFUL );
151158 }
152159 }
0 commit comments