View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.classic;
28  
29  
30  import org.apache.hc.client5.http.HttpRoute;
31  import org.apache.hc.core5.http.HttpHost;
32  import org.apache.hc.core5.util.TimeValue;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.junit.jupiter.api.Assertions.assertEquals;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  public class TestLinearBackoffManager {
40  
41      private LinearBackoffManager impl;
42      private MockConnPoolControl connPerRoute;
43      private HttpRoute route;
44      private static final long DEFAULT_COOL_DOWN_MS = 10;
45  
46      @BeforeEach
47      public void setUp() {
48          connPerRoute = new MockConnPoolControl();
49          route = new HttpRoute(new HttpHost("localhost", 80));
50          impl = new LinearBackoffManager(connPerRoute);
51          impl.setCoolDown(TimeValue.ofMilliseconds(DEFAULT_COOL_DOWN_MS));
52      }
53  
54      @Test
55      public void incrementsConnectionsOnBackoff() {
56          final LinearBackoffManager impl = new LinearBackoffManager(connPerRoute);
57          impl.setCoolDown(TimeValue.ofMilliseconds(DEFAULT_COOL_DOWN_MS)); // Set the cool-down period
58          connPerRoute.setMaxPerRoute(route, 4);
59          impl.backOff(route);
60          assertEquals(5, connPerRoute.getMaxPerRoute(route));
61      }
62  
63      @Test
64      public void decrementsConnectionsOnProbe() {
65          connPerRoute.setMaxPerRoute(route, 4);
66          impl.probe(route);
67          assertEquals(3, connPerRoute.getMaxPerRoute(route));
68      }
69  
70      @Test
71      public void backoffDoesNotAdjustDuringCoolDownPeriod() {
72          connPerRoute.setMaxPerRoute(route, 4);
73          impl.backOff(route);
74          final long max = connPerRoute.getMaxPerRoute(route);
75          // Replace Thread.sleep(1) with busy waiting
76          final long end = System.currentTimeMillis() + 1;
77          while (System.currentTimeMillis() < end) {
78              // Busy waiting
79          }
80  
81          impl.backOff(route);
82          assertEquals(max, connPerRoute.getMaxPerRoute(route));
83      }
84  
85      @Test
86      public void backoffStillAdjustsAfterCoolDownPeriod() throws InterruptedException {
87          final LinearBackoffManager impl = new LinearBackoffManager(connPerRoute);
88          impl.setCoolDown(TimeValue.ofMilliseconds(DEFAULT_COOL_DOWN_MS)); // Set the cool-down period
89          connPerRoute.setMaxPerRoute(route, 4);
90          impl.backOff(route);
91          final int max1 = connPerRoute.getMaxPerRoute(route);
92  
93          Thread.sleep(DEFAULT_COOL_DOWN_MS + 1); // Sleep for cooldown period + 1 ms
94  
95          impl.backOff(route);
96          final int max2 = connPerRoute.getMaxPerRoute(route);
97  
98          assertTrue(max2 > max1);
99      }
100 
101 
102     @Test
103     public void probeDoesNotAdjustDuringCooldownPeriod() {
104         connPerRoute.setMaxPerRoute(route, 4);
105         impl.probe(route);
106         final long max = connPerRoute.getMaxPerRoute(route);
107         // Replace Thread.sleep(1) with busy waiting
108         final long end = System.currentTimeMillis() + 1;
109         while (System.currentTimeMillis() < end) {
110             // Busy waiting
111         }
112 
113         impl.probe(route);
114         assertEquals(max, connPerRoute.getMaxPerRoute(route));
115     }
116 
117     @Test
118     public void probeStillAdjustsAfterCoolDownPeriod() throws InterruptedException {
119         connPerRoute.setMaxPerRoute(route, 4);
120         impl.probe(route);
121         Thread.sleep(DEFAULT_COOL_DOWN_MS + 1); // Sleep for cooldown period + 1 ms
122         impl.probe(route);
123         final long newMax = connPerRoute.getMaxPerRoute(route);
124         assertEquals(2, newMax); // The cap is set to 2 by default
125     }
126 
127     @Test
128     public void testSetPerHostConnectionCap() {
129         connPerRoute.setMaxPerRoute(route, 5);
130         impl.setPerHostConnectionCap(10); // Set the cap to a higher value
131         impl.backOff(route);
132         assertEquals(6, connPerRoute.getMaxPerRoute(route));
133     }
134 
135     @Test
136     public void probeUpdatesRemainingAttemptsIndirectly() throws InterruptedException {
137         // Set initial max per route
138         connPerRoute.setMaxPerRoute(route, 4);
139 
140         // Apply backOff twice
141         impl.backOff(route);
142         Thread.sleep(DEFAULT_COOL_DOWN_MS + 1);
143         impl.backOff(route);
144 
145         // Ensure that connection pool size has increased
146         assertEquals(6, connPerRoute.getMaxPerRoute(route));
147 
148         // Apply probe once
149         impl.probe(route);
150 
151         // Wait for a longer cool down period
152         Thread.sleep(DEFAULT_COOL_DOWN_MS * 2);
153 
154         // Apply probe once more
155         impl.probe(route);
156 
157         // Check that connection pool size has decreased once, indicating that the remaining attempts were updated
158         assertEquals(5, connPerRoute.getMaxPerRoute(route));
159     }
160 
161     @Test
162     public void linearIncrementTest() throws InterruptedException {
163         final int initialMax = 4;
164         connPerRoute.setMaxPerRoute(route, initialMax);
165         for (int i = 1; i <= 5; i++) {
166             impl.backOff(route);
167             assertEquals(initialMax + i, connPerRoute.getMaxPerRoute(route));
168             Thread.sleep(DEFAULT_COOL_DOWN_MS + 1);
169 
170         }
171     }
172 }