TestIdleConnectionEviction.java (httpcomponents-client-4.5.9-src) | : | TestIdleConnectionEviction.java (httpcomponents-client-4.5.10-src) | ||
---|---|---|---|---|
skipping to change at line 30 | skipping to change at line 30 | |||
* | * | |||
* This software consists of voluntary contributions made by many | * This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | * individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | * information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | * <http://www.apache.org/>. | |||
* | * | |||
*/ | */ | |||
package org.apache.http.impl.client.integration; | package org.apache.http.impl.client.integration; | |||
import java.net.URI; | ||||
import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | |||
import org.apache.http.HttpHost; | import org.apache.http.HttpHost; | |||
import org.apache.http.HttpResponse; | ||||
import org.apache.http.client.ClientProtocolException; | import org.apache.http.client.ClientProtocolException; | |||
import org.apache.http.client.HttpClient; | import org.apache.http.client.methods.CloseableHttpResponse; | |||
import org.apache.http.client.methods.HttpGet; | import org.apache.http.client.methods.HttpGet; | |||
import org.apache.http.client.methods.HttpUriRequest; | import org.apache.http.impl.client.CloseableHttpClient; | |||
import org.apache.http.impl.client.IdleConnectionEvictor; | import org.apache.http.impl.client.IdleConnectionEvictor; | |||
import org.apache.http.localserver.LocalServerTestBase; | import org.apache.http.localserver.LocalServerTestBase; | |||
import org.apache.http.util.EntityUtils; | import org.apache.http.util.EntityUtils; | |||
import org.junit.Test; | import org.junit.Test; | |||
public class TestIdleConnectionEviction extends LocalServerTestBase { | public class TestIdleConnectionEviction extends LocalServerTestBase { | |||
@Test | @Test | |||
public void testIdleConnectionEviction() throws Exception { | public void testIdleConnectionEviction() throws Exception { | |||
this.connManager.setDefaultMaxPerRoute(10); | this.connManager.setDefaultMaxPerRoute(10); | |||
this.connManager.setMaxTotal(50); | this.connManager.setMaxTotal(50); | |||
final HttpHost target = start(); | final HttpHost target = start(); | |||
final IdleConnectionEvictor idleConnectionMonitor = new IdleConnectionEv ictor( | final IdleConnectionEvictor idleConnectionMonitor = new IdleConnectionEv ictor( | |||
this.connManager, 50, TimeUnit.MILLISECONDS); | this.connManager, 50, TimeUnit.MILLISECONDS); | |||
idleConnectionMonitor.start(); | idleConnectionMonitor.start(); | |||
final HttpGet httpget = new HttpGet("/random/1024"); | final URI requestUri = new URI("/random/1024"); | |||
final WorkerThread[] workers = new WorkerThread[5]; | final WorkerThread[] workers = new WorkerThread[5]; | |||
for (int i = 0; i < workers.length; i++) { | for (int i = 0; i < workers.length; i++) { | |||
workers[i] = new WorkerThread(httpclient, target, httpget, 200); | workers[i] = new WorkerThread(httpclient, target, requestUri, 200); | |||
} | } | |||
for (final WorkerThread worker : workers) { | for (final WorkerThread worker : workers) { | |||
worker.start(); | worker.start(); | |||
} | } | |||
for (final WorkerThread worker : workers) { | for (final WorkerThread worker : workers) { | |||
worker.join(); | worker.join(); | |||
final Exception ex = worker.getException(); | final Exception ex = worker.getException(); | |||
if (ex != null) { | if (ex != null) { | |||
throw ex; | throw ex; | |||
} | } | |||
} | } | |||
idleConnectionMonitor.shutdown(); | idleConnectionMonitor.shutdown(); | |||
} | } | |||
static class WorkerThread extends Thread { | static class WorkerThread extends Thread { | |||
private final HttpClient httpclient; | private final CloseableHttpClient httpclient; | |||
private final HttpHost target; | private final HttpHost target; | |||
private final HttpUriRequest request; | private final URI requestUri; | |||
private final int count; | private final int count; | |||
private volatile Exception ex; | private volatile Exception ex; | |||
public WorkerThread( | public WorkerThread( | |||
final HttpClient httpclient, | final CloseableHttpClient httpclient, | |||
final HttpHost target, | final HttpHost target, | |||
final HttpUriRequest request, | final URI requestUri, | |||
final int count) { | final int count) { | |||
super(); | super(); | |||
this.httpclient = httpclient; | this.httpclient = httpclient; | |||
this.target = target; | this.target = target; | |||
this.request = request; | this.requestUri = requestUri; | |||
this.count = count; | this.count = count; | |||
} | } | |||
@Override | @Override | |||
public void run() { | public void run() { | |||
try { | try { | |||
for (int i = 0; i < this.count; i++) { | for (int i = 0; i < this.count; i++) { | |||
final HttpResponse response = this.httpclient.execute(this.t | final HttpGet httpget = new HttpGet(this.requestUri); | |||
arget, this.request); | final CloseableHttpResponse response = this.httpclient.execu | |||
final int status = response.getStatusLine().getStatusCode(); | te(this.target, httpget); | |||
if (status != 200) { | try { | |||
this.request.abort(); | final int status = response.getStatusLine().getStatusCod | |||
throw new ClientProtocolException("Unexpected status cod | e(); | |||
e: " + status); | if (status != 200) { | |||
throw new ClientProtocolException("Unexpected status | ||||
code: " + status); | ||||
} | ||||
EntityUtils.consume(response.getEntity()); | ||||
Thread.sleep(10); | ||||
} finally { | ||||
response.close(); | ||||
} | } | |||
EntityUtils.consume(response.getEntity()); | ||||
Thread.sleep(10); | ||||
} | } | |||
} catch (final Exception ex) { | } catch (final Exception ex) { | |||
this.ex = ex; | this.ex = ex; | |||
} | } | |||
} | } | |||
public Exception getException() { | public Exception getException() { | |||
return ex; | return ex; | |||
} | } | |||
End of changes. 13 change blocks. | ||||
19 lines changed or deleted | 24 lines changed or added |