2626 */
2727package org .apache .hc .core5 .pool ;
2828
29+ import java .time .Clock ;
30+ import java .time .Instant ;
31+ import java .time .ZoneId ;
2932import java .util .concurrent .TimeUnit ;
3033import java .util .concurrent .atomic .AtomicLong ;
3134
32- import org .apache .hc .core5 .function .Supplier ;
3335import org .apache .hc .core5 .http .HttpConnection ;
3436import org .apache .hc .core5 .io .CloseMode ;
3537import org .apache .hc .core5 .util .Deadline ;
4244class TestPoolEntry {
4345
4446 private AtomicLong count ;
45- private Supplier < Long > currentTimeSupplier ;
47+ private Clock clock ;
4648
4749 @ BeforeEach
4850 void setup () {
4951 count = new AtomicLong (1 );
50- currentTimeSupplier = () -> count .addAndGet (1 );
52+ clock = new Clock () {
53+
54+ private final ZoneId zoneId = ZoneId .systemDefault ();
55+
56+ @ Override
57+ public ZoneId getZone () {
58+ return zoneId ;
59+ }
60+
61+ @ Override
62+ public Clock withZone (final ZoneId zone ) {
63+ return this ;
64+ }
65+
66+ @ Override
67+ public long millis () {
68+ return count .addAndGet (1 );
69+ }
70+
71+ @ Override
72+ public Instant instant () {
73+ return Instant .ofEpochMilli (millis ());
74+ }
75+
76+ };
5177 }
5278
5379 @ Test
5480 void testBasics () {
5581 final PoolEntry <String , HttpConnection > entry1 = new PoolEntry <>(
56- "route1" , TimeValue .of (10L , TimeUnit .MILLISECONDS ), currentTimeSupplier );
82+ "route1" , TimeValue .of (10L , TimeUnit .MILLISECONDS ), clock );
5783
5884 Assertions .assertEquals ("route1" , entry1 .getRoute ());
5985 Assertions .assertEquals (0 , entry1 .getUpdated ());
@@ -80,7 +106,7 @@ void testNullConstructor() {
80106 @ Test
81107 void testValidInfinitely () {
82108 final PoolEntry <String , HttpConnection > entry1 = new PoolEntry <>(
83- "route1" , TimeValue .ZERO_MILLISECONDS , currentTimeSupplier );
109+ "route1" , TimeValue .ZERO_MILLISECONDS , clock );
84110 entry1 .assignConnection (Mockito .mock (HttpConnection .class ));
85111 Assertions .assertEquals (Deadline .MAX_VALUE , entry1 .getValidityDeadline ());
86112 Assertions .assertEquals (entry1 .getValidityDeadline (), entry1 .getExpiryDeadline ());
@@ -89,7 +115,7 @@ void testValidInfinitely() {
89115 @ Test
90116 void testExpiry () {
91117 final PoolEntry <String , HttpConnection > entry1 = new PoolEntry <>(
92- "route1" , TimeValue .ZERO_MILLISECONDS , currentTimeSupplier );
118+ "route1" , TimeValue .ZERO_MILLISECONDS , clock );
93119 entry1 .assignConnection (Mockito .mock (HttpConnection .class ));
94120 Assertions .assertEquals (Deadline .MAX_VALUE , entry1 .getExpiryDeadline ());
95121 entry1 .updateExpiry (TimeValue .of (50L , TimeUnit .MILLISECONDS ));
@@ -98,7 +124,7 @@ void testExpiry() {
98124 Assertions .assertEquals (Deadline .MAX_VALUE , entry1 .getExpiryDeadline ());
99125
100126 final PoolEntry <String , HttpConnection > entry2 = new PoolEntry <>(
101- "route1" , TimeValue .of (100L , TimeUnit .MILLISECONDS ), currentTimeSupplier );
127+ "route1" , TimeValue .of (100L , TimeUnit .MILLISECONDS ), clock );
102128 entry2 .assignConnection (Mockito .mock (HttpConnection .class ));
103129 final Deadline validityDeadline = entry2 .getValidityDeadline ();
104130 Assertions .assertEquals (entry2 .getUpdated () + 100L , entry2 .getExpiryDeadline ().getValue ());
@@ -111,17 +137,17 @@ void testExpiry() {
111137 @ Test
112138 void testInvalidExpiry () {
113139 final PoolEntry <String , HttpConnection > entry = new PoolEntry <>(
114- "route1" , TimeValue .of (0L , TimeUnit .MILLISECONDS ), currentTimeSupplier );
140+ "route1" , TimeValue .of (0L , TimeUnit .MILLISECONDS ), clock );
115141 Assertions .assertThrows (NullPointerException .class , () ->
116142 entry .updateExpiry (null ));
117143 }
118144
119145 @ Test
120146 void testExpiryDoesNotOverflow () {
121147 final PoolEntry <String , HttpConnection > entry = new PoolEntry <>(
122- "route1" , TimeValue .of (Long .MAX_VALUE , TimeUnit .MILLISECONDS ), currentTimeSupplier );
148+ "route1" , TimeValue .of (Long .MAX_VALUE , TimeUnit .MILLISECONDS ), clock );
123149 entry .assignConnection (Mockito .mock (HttpConnection .class ));
124150 Assertions .assertEquals (Deadline .MAX_VALUE , entry .getValidityDeadline ());
125151 }
126152
127- }
153+ }
0 commit comments