@@ -27,7 +27,6 @@ import okhttp3.FakeRoutePlanner
27
27
import okhttp3.OkHttpClient
28
28
import okhttp3.Request
29
29
import okhttp3.TestUtil.awaitGarbageCollection
30
- import okhttp3.TestValueFactory
31
30
import okhttp3.internal.concurrent.TaskRunner
32
31
import okhttp3.internal.connection.Locks.withLock
33
32
import okhttp3.internal.http2.Http2
@@ -39,7 +38,9 @@ import org.junit.jupiter.api.AfterEach
39
38
import org.junit.jupiter.api.Test
40
39
41
40
class ConnectionPoolTest {
42
- private val factory = TestValueFactory ()
41
+ private val routePlanner = FakeRoutePlanner ()
42
+ private val factory = routePlanner.factory
43
+ private val taskFaker = routePlanner.taskFaker
43
44
private val peer = MockHttp2Peer ()
44
45
45
46
/* * The fake task runner prevents the cleanup runnable from being started. */
@@ -50,8 +51,6 @@ class ConnectionPoolTest {
50
51
private val addressC = factory.newAddress(" c" )
51
52
private val routeC1 = factory.newRoute(addressC)
52
53
53
- private val routePlanner = FakeRoutePlanner (factory.taskFaker)
54
-
55
54
@AfterEach fun tearDown () {
56
55
factory.close()
57
56
peer.close()
@@ -205,7 +204,8 @@ class ConnectionPoolTest {
205
204
}
206
205
207
206
@Test fun connectionPreWarmingHttp1 () {
208
- val expireTime = factory.taskFaker.nanoTime + 1_000_000_000_000
207
+ taskFaker.advanceUntil(System .nanoTime())
208
+ val expireTime = taskFaker.nanoTime + 1_000_000_000_000
209
209
210
210
routePlanner.autoGeneratePlans = true
211
211
routePlanner.defaultConnectionIdleAtNanos = expireTime
@@ -219,19 +219,20 @@ class ConnectionPoolTest {
219
219
// Connections are replaced if they idle out or are evicted from the pool
220
220
evictAllConnections(pool)
221
221
assertThat(pool.connectionCount()).isEqualTo(2 )
222
- forceConnectionsToExpire(pool, routePlanner, expireTime)
222
+ forceConnectionsToExpire(pool, expireTime)
223
223
assertThat(pool.connectionCount()).isEqualTo(2 )
224
224
225
225
// Excess connections aren't removed until they idle out, even if no longer needed
226
226
setPolicy(pool, address, ConnectionPool .AddressPolicy (1 ))
227
227
assertThat(pool.connectionCount()).isEqualTo(2 )
228
- forceConnectionsToExpire(pool, routePlanner, expireTime)
228
+ forceConnectionsToExpire(pool, expireTime)
229
229
assertThat(pool.connectionCount()).isEqualTo(1 )
230
230
}
231
231
232
232
@Test fun connectionPreWarmingHttp2 () {
233
- val expireSooner = factory.taskFaker.nanoTime + 1_000_000_000_000
234
- val expireLater = factory.taskFaker.nanoTime + 2_000_000_000_000
233
+ taskFaker.advanceUntil(System .nanoTime())
234
+ val expireSooner = taskFaker.nanoTime + 1_000_000_000_000
235
+ val expireLater = taskFaker.nanoTime + 2_000_000_000_000
235
236
236
237
routePlanner.autoGeneratePlans = true
237
238
val address = routePlanner.address
@@ -258,7 +259,7 @@ class ConnectionPoolTest {
258
259
259
260
// Increase the connection's max so that the new connection is no longer needed
260
261
updateMaxConcurrentStreams(http2Connection, 5 )
261
- forceConnectionsToExpire(pool, routePlanner, expireSooner)
262
+ forceConnectionsToExpire(pool, expireSooner)
262
263
assertThat(pool.connectionCount()).isEqualTo(1 )
263
264
}
264
265
@@ -268,23 +269,22 @@ class ConnectionPoolTest {
268
269
policy : ConnectionPool .AddressPolicy ,
269
270
) {
270
271
pool.setPolicy(address, policy)
271
- routePlanner.factory. taskFaker.runTasks()
272
+ taskFaker.runTasks()
272
273
}
273
274
274
275
private fun evictAllConnections (pool : RealConnectionPool ) {
275
276
pool.evictAll()
276
277
assertThat(pool.connectionCount()).isEqualTo(0 )
277
- routePlanner.factory. taskFaker.runTasks()
278
+ taskFaker.runTasks()
278
279
}
279
280
280
281
private fun forceConnectionsToExpire (
281
282
pool : RealConnectionPool ,
282
- routePlanner : FakeRoutePlanner ,
283
283
expireTime : Long ,
284
284
) {
285
285
val idleTimeNanos = expireTime + pool.keepAliveDurationNs
286
286
repeat(pool.connectionCount()) { pool.closeConnections(idleTimeNanos) }
287
- routePlanner.factory. taskFaker.runTasks()
287
+ taskFaker.runTasks()
288
288
}
289
289
290
290
private fun connectHttp2 (
@@ -316,7 +316,7 @@ class ConnectionPoolTest {
316
316
assertThat(ackFrame.streamId).isEqualTo(0 )
317
317
assertThat(ackFrame.ack).isTrue()
318
318
319
- routePlanner.factory. taskFaker.runTasks()
319
+ taskFaker.runTasks()
320
320
321
321
return connection
322
322
}
@@ -329,7 +329,7 @@ class ConnectionPoolTest {
329
329
settings[Settings .MAX_CONCURRENT_STREAMS ] = amount
330
330
connection.readerRunnable.applyAndAckSettings(true , settings)
331
331
assertThat(connection.peerSettings[Settings .MAX_CONCURRENT_STREAMS ]).isEqualTo(amount)
332
- routePlanner.factory. taskFaker.runTasks()
332
+ taskFaker.runTasks()
333
333
}
334
334
335
335
/* * Use a helper method so there's no hidden reference remaining on the stack. */
0 commit comments