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  import java.util.HashSet;
30  import java.util.Map;
31  import java.util.Set;
32  import java.util.concurrent.ConcurrentHashMap;
33  
34  import org.apache.hc.client5.http.HttpRoute;
35  import org.apache.hc.core5.pool.ConnPoolControl;
36  import org.apache.hc.core5.pool.PoolStats;
37  import org.apache.hc.core5.util.TimeValue;
38  
39  public final class MockConnPoolControl implements ConnPoolControl<HttpRoute> {
40  
41      private final ConcurrentHashMap<HttpRoute, Integer> maxPerHostMap;
42  
43      private volatile int totalMax;
44      private volatile int defaultMax;
45  
46      public MockConnPoolControl() {
47          super();
48          this.maxPerHostMap = new ConcurrentHashMap<>();
49          this.totalMax = 20;
50          this.defaultMax = 2;
51      }
52  
53      @Override
54      public void setMaxTotal(final int max) {
55          this.totalMax = max;
56      }
57  
58      @Override
59      public int getMaxTotal() {
60          return this.totalMax;
61      }
62  
63      @Override
64      public PoolStats getTotalStats() {
65          return new PoolStats(-1, -1, -1, this.totalMax);
66      }
67  
68      @Override
69      public PoolStats getStats(final HttpRoute route) {
70          return new PoolStats(-1, -1, -1, getMaxPerRoute(route));
71      }
72  
73      @Override
74      public int getDefaultMaxPerRoute() {
75          return this.defaultMax;
76      }
77  
78      @Override
79      public void setDefaultMaxPerRoute(final int max) {
80          this.defaultMax = max;
81      }
82  
83      @Override
84      public void setMaxPerRoute(final HttpRoute route, final int max) {
85          this.maxPerHostMap.put(route, Integer.valueOf(max));
86      }
87  
88      @Override
89      public int getMaxPerRoute(final HttpRoute route) {
90          final Integer max = this.maxPerHostMap.get(route);
91          if (max != null) {
92              return max.intValue();
93          } else {
94              return this.defaultMax;
95          }
96      }
97  
98      @Override
99      public void closeIdle(final TimeValue idletime) {
100     }
101 
102     @Override
103     public void closeExpired() {
104     }
105 
106     public void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
107         if (map == null) {
108             return;
109         }
110         this.maxPerHostMap.clear();
111         this.maxPerHostMap.putAll(map);
112     }
113 
114     @Override
115     public Set<HttpRoute> getRoutes() {
116         return new HashSet<>(this.maxPerHostMap.keySet());
117     }
118 
119     @Override
120     public String toString() {
121         return this.maxPerHostMap.toString();
122     }
123 
124 }