3535import java .time .Instant ;
3636import java .time .ZoneId ;
3737import java .util .concurrent .Future ;
38+ import java .util .concurrent .TimeUnit ;
3839import java .util .concurrent .atomic .AtomicLong ;
3940
4041import org .apache .hc .core5 .io .CloseMode ;
4142import org .apache .hc .core5 .util .TimeValue ;
4243import org .apache .hc .core5 .util .Timeout ;
43- import org .junit .jupiter .api .Test ;
44+ import org .junit .jupiter .params .ParameterizedTest ;
45+ import org .junit .jupiter .params .provider .EnumSource ;
4446
4547final class TestConnPoolClockInjection {
4648
@@ -77,27 +79,20 @@ public long millis() {
7779 public Instant instant () {
7880 return Instant .ofEpochMilli (millis ());
7981 }
82+
8083 }
8184
82- @ Test
83- void strictPoolCloseIdleUsesInjectedClock () throws Exception {
85+ @ ParameterizedTest
86+ @ EnumSource (PoolConcurrencyPolicy .class )
87+ void closeIdleUsesInjectedClock (final PoolConcurrencyPolicy policy ) throws Exception {
8488 final TestClock clock = new TestClock (0L );
85-
86- final StrictConnPool <String , PoolTestSupport .DummyConn > pool = new StrictConnPool <>(
87- 1 ,
88- 1 ,
89- TimeValue .NEG_ONE_MILLISECOND ,
90- PoolReusePolicy .LIFO ,
91- PoolTestSupport .DISPOSAL ,
92- null ,
93- clock );
89+ final ManagedConnPool <String , PoolTestSupport .DummyConn > pool =
90+ PoolTestSupport .createPool (policy , 1 , 1 , clock );
9491
9592 try {
9693 final Future <PoolEntry <String , PoolTestSupport .DummyConn >> f1 =
97- pool .lease ("r1" , null , Timeout .DISABLED , null );
98- assertTrue (f1 .isDone ());
99-
100- final PoolEntry <String , PoolTestSupport .DummyConn > e1 = f1 .get ();
94+ pool .lease ("r1" , null , Timeout .ofSeconds (1 ), null );
95+ final PoolEntry <String , PoolTestSupport .DummyConn > e1 = f1 .get (1 , TimeUnit .SECONDS );
10196 assertNotNull (e1 );
10297
10398 final PoolTestSupport .DummyConn conn = new PoolTestSupport .DummyConn ();
@@ -106,100 +101,20 @@ void strictPoolCloseIdleUsesInjectedClock() throws Exception {
106101
107102 pool .closeIdle (TimeValue .ofMilliseconds (1 ));
108103 assertFalse (conn .isClosed ());
109-
110- clock .advanceMillis (10_000L );
111-
112- pool .closeIdle (TimeValue .ofMilliseconds (1 ));
113- assertTrue (conn .isClosed ());
114-
115- assertEquals (0 , pool .getTotalStats ().getAvailable ());
104+ assertEquals (1 , pool .getTotalStats ().getAvailable ());
116105 assertEquals (0 , pool .getTotalStats ().getLeased ());
117106 assertEquals (0 , pool .getTotalStats ().getPending ());
118- } finally {
119- pool .close (CloseMode .IMMEDIATE );
120- }
121- }
122-
123- @ Test
124- void laxPoolCloseIdleUsesInjectedClock () throws Exception {
125- final TestClock clock = new TestClock (0L );
126-
127- final LaxConnPool <String , PoolTestSupport .DummyConn > pool = new LaxConnPool <>(
128- 1 ,
129- TimeValue .NEG_ONE_MILLISECOND ,
130- PoolReusePolicy .LIFO ,
131- PoolTestSupport .DISPOSAL ,
132- null ,
133- clock );
134-
135- try {
136- final Future <PoolEntry <String , PoolTestSupport .DummyConn >> f1 =
137- pool .lease ("r1" , null , Timeout .DISABLED , null );
138- assertTrue (f1 .isDone ());
139-
140- final PoolEntry <String , PoolTestSupport .DummyConn > e1 = f1 .get ();
141- assertNotNull (e1 );
142-
143- final PoolTestSupport .DummyConn conn = new PoolTestSupport .DummyConn ();
144- e1 .assignConnection (conn );
145- pool .release (e1 , true );
146-
147- pool .closeIdle (TimeValue .ofMilliseconds (1 ));
148- assertFalse (conn .isClosed ());
149107
150108 clock .advanceMillis (10_000L );
151109
152110 pool .closeIdle (TimeValue .ofMilliseconds (1 ));
153- assertTrue (conn .isClosed ());
154-
155111 assertEquals (0 , pool .getTotalStats ().getAvailable ());
156112 assertEquals (0 , pool .getTotalStats ().getLeased ());
157113 assertEquals (0 , pool .getTotalStats ().getPending ());
158- } finally {
159- pool .close (CloseMode .IMMEDIATE );
160- }
161- }
162-
163- @ Test
164- void offlockPoolCloseIdleUsesInjectedClock () throws Exception {
165- final TestClock clock = new TestClock (0L );
166-
167- final RouteSegmentedConnPool <String , PoolTestSupport .DummyConn > pool = new RouteSegmentedConnPool <>(
168- 1 ,
169- 1 ,
170- TimeValue .NEG_ONE_MILLISECOND ,
171- PoolReusePolicy .LIFO ,
172- PoolTestSupport .DISPOSAL ,
173- null ,
174- clock );
175-
176- try {
177- final Future <PoolEntry <String , PoolTestSupport .DummyConn >> f1 =
178- pool .lease ("r1" , null , Timeout .DISABLED , null );
179- assertTrue (f1 .isDone ());
180-
181- final PoolEntry <String , PoolTestSupport .DummyConn > e1 = f1 .get ();
182- assertNotNull (e1 );
183-
184- final PoolTestSupport .DummyConn conn = new PoolTestSupport .DummyConn ();
185- e1 .assignConnection (conn );
186- pool .release (e1 , true );
187-
188- // Not idle yet at t=0
189- pool .closeIdle (TimeValue .ofMilliseconds (1 ));
190- assertFalse (conn .isClosed ());
191- assertEquals (1 , pool .getStats ("r1" ).getAvailable ());
192-
193- // Advance injected clock: now the entry is definitely idle
194- clock .advanceMillis (10_000L );
195-
196- pool .closeIdle (TimeValue .ofMilliseconds (1 ));
197- assertEquals (0 , pool .getStats ("r1" ).getAvailable ());
198114
199- final PoolStats totals = pool .getTotalStats ();
200- assertEquals (0 , totals .getAvailable ());
201- assertEquals (0 , totals .getLeased ());
202- assertEquals (0 , totals .getPending ());
115+ if (policy != PoolConcurrencyPolicy .OFFLOCK ) {
116+ assertTrue (conn .isClosed ());
117+ }
203118 } finally {
204119 pool .close (CloseMode .IMMEDIATE );
205120 }
0 commit comments