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  
28  package org.apache.http.impl.client.integration;
29  
30  import java.util.concurrent.TimeUnit;
31  
32  import org.apache.http.HttpHost;
33  import org.apache.http.HttpResponse;
34  import org.apache.http.client.ClientProtocolException;
35  import org.apache.http.client.HttpClient;
36  import org.apache.http.client.methods.HttpGet;
37  import org.apache.http.client.methods.HttpUriRequest;
38  import org.apache.http.impl.client.IdleConnectionEvictor;
39  import org.apache.http.localserver.LocalServerTestBase;
40  import org.apache.http.util.EntityUtils;
41  import org.junit.Test;
42  
43  public class TestIdleConnectionEviction extends LocalServerTestBase {
44  
45      @Test
46      public void testIdleConnectionEviction() throws Exception {
47          this.connManager.setDefaultMaxPerRoute(10);
48          this.connManager.setMaxTotal(50);
49  
50          final HttpHost target = start();
51  
52          final IdleConnectionEvictor idleConnectionMonitor = new IdleConnectionEvictor(
53                  this.connManager, 50, TimeUnit.MILLISECONDS);
54          idleConnectionMonitor.start();
55  
56          final HttpGet httpget = new HttpGet("/random/1024");
57          final WorkerThread[] workers = new WorkerThread[5];
58          for (int i = 0; i < workers.length; i++) {
59              workers[i] = new WorkerThread(httpclient, target, httpget, 200);
60          }
61          for (final WorkerThread worker : workers) {
62              worker.start();
63          }
64          for (final WorkerThread worker : workers) {
65              worker.join();
66              final Exception ex = worker.getException();
67              if (ex != null) {
68                  throw ex;
69              }
70          }
71          idleConnectionMonitor.shutdown();
72      }
73  
74      static class WorkerThread extends Thread {
75  
76          private final HttpClient httpclient;
77          private final HttpHost target;
78          private final HttpUriRequest request;
79          private final int count;
80  
81          private volatile Exception ex;
82  
83          public WorkerThread(
84                  final HttpClient httpclient,
85                  final HttpHost target,
86                  final HttpUriRequest request,
87                  final int count) {
88              super();
89              this.httpclient = httpclient;
90              this.target = target;
91              this.request = request;
92              this.count = count;
93          }
94  
95          @Override
96          public void run() {
97              try {
98                  for (int i = 0; i < this.count; i++) {
99                      final HttpResponse response = this.httpclient.execute(this.target, this.request);
100                     final int status = response.getStatusLine().getStatusCode();
101                     if (status != 200) {
102                         this.request.abort();
103                         throw new ClientProtocolException("Unexpected status code: " + status);
104                     }
105                     EntityUtils.consume(response.getEntity());
106                     Thread.sleep(10);
107                 }
108             } catch (final Exception ex) {
109                 this.ex = ex;
110             }
111         }
112 
113         public Exception getException() {
114             return ex;
115         }
116 
117     }
118 
119 }