Skip to content

Commit 6be7994

Browse files
committed
Refactor clock-injection pool tests to remove duplication by running a single parameterized test over PoolConcurrencyPolicy and centralizing pool construction in PoolTestSupport.
1 parent 97fc525 commit 6be7994

2 files changed

Lines changed: 57 additions & 146 deletions

File tree

httpcore5/src/test/java/org/apache/hc/core5/pool/PoolTestSupport.java

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package org.apache.hc.core5.pool;
2828

2929
import java.io.IOException;
30+
import java.time.Clock;
3031

3132
import org.apache.hc.core5.io.CloseMode;
3233
import org.apache.hc.core5.io.ModalCloseable;
@@ -59,54 +60,49 @@ boolean isClosed() {
5960

6061
static final DisposalCallback<DummyConn> DISPOSAL = DummyConn::close;
6162

62-
enum PoolType {
63-
STRICT,
64-
LAX,
65-
OFFLOCK;
66-
67-
ManagedConnPool<String, DummyConn> createPool(
68-
final int defaultMaxPerRoute,
69-
final int maxTotal) {
70-
71-
final TimeValue ttl = TimeValue.NEG_ONE_MILLISECOND;
72-
final PoolReusePolicy reusePolicy = PoolReusePolicy.LIFO;
73-
74-
switch (this) {
75-
case STRICT: {
76-
return new StrictConnPool<>(
77-
defaultMaxPerRoute,
78-
maxTotal,
79-
ttl,
80-
reusePolicy,
81-
DISPOSAL,
82-
null);
83-
}
84-
case LAX: {
85-
return new LaxConnPool<>(
86-
defaultMaxPerRoute,
87-
ttl,
88-
reusePolicy,
89-
DISPOSAL,
90-
null);
91-
}
92-
case OFFLOCK: {
93-
return new RouteSegmentedConnPool<>(
94-
defaultMaxPerRoute,
95-
maxTotal,
96-
ttl,
97-
reusePolicy,
98-
DISPOSAL);
99-
}
100-
default: {
101-
throw new IllegalStateException("Unexpected: " + this);
102-
}
63+
static ManagedConnPool<String, DummyConn> createPool(
64+
final PoolConcurrencyPolicy policy,
65+
final int defaultMaxPerRoute,
66+
final int maxTotal,
67+
final Clock clock) {
68+
69+
final TimeValue ttl = TimeValue.NEG_ONE_MILLISECOND;
70+
final PoolReusePolicy reusePolicy = PoolReusePolicy.LIFO;
71+
72+
switch (policy) {
73+
case STRICT: {
74+
return new StrictConnPool<>(
75+
defaultMaxPerRoute,
76+
maxTotal,
77+
ttl,
78+
reusePolicy,
79+
DISPOSAL,
80+
null,
81+
clock);
82+
}
83+
case LAX: {
84+
return new LaxConnPool<>(
85+
defaultMaxPerRoute,
86+
ttl,
87+
reusePolicy,
88+
DISPOSAL,
89+
null,
90+
clock);
91+
}
92+
case OFFLOCK: {
93+
return new RouteSegmentedConnPool<>(
94+
defaultMaxPerRoute,
95+
maxTotal,
96+
ttl,
97+
reusePolicy,
98+
DISPOSAL,
99+
null,
100+
clock);
101+
}
102+
default: {
103+
throw new IllegalStateException("Unexpected: " + policy);
103104
}
104105
}
105-
106-
boolean hasHardTotalLimit() {
107-
return this != LAX;
108-
}
109-
110106
}
111107

112108
}

httpcore5/src/test/java/org/apache/hc/core5/pool/TestConnPoolClockInjection.java

Lines changed: 15 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@
3535
import java.time.Instant;
3636
import java.time.ZoneId;
3737
import java.util.concurrent.Future;
38+
import java.util.concurrent.TimeUnit;
3839
import java.util.concurrent.atomic.AtomicLong;
3940

4041
import org.apache.hc.core5.io.CloseMode;
4142
import org.apache.hc.core5.util.TimeValue;
4243
import 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

4547
final 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

Comments
 (0)