Fixed concurrent use of threading unsafe HttpUriRequest messages
This commit is contained in:
parent
c1e2339f0f
commit
779bb8c53b
|
@ -27,14 +27,14 @@
|
||||||
|
|
||||||
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;
|
||||||
|
@ -53,10 +53,10 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
|
||||||
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();
|
||||||
|
@ -73,22 +73,22 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,14 +96,18 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
|
||||||
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.target, this.request);
|
final HttpGet httpget = new HttpGet(this.requestUri);
|
||||||
|
final CloseableHttpResponse response = this.httpclient.execute(this.target, httpget);
|
||||||
|
try {
|
||||||
final int status = response.getStatusLine().getStatusCode();
|
final int status = response.getStatusLine().getStatusCode();
|
||||||
if (status != 200) {
|
if (status != 200) {
|
||||||
this.request.abort();
|
|
||||||
throw new ClientProtocolException("Unexpected status code: " + status);
|
throw new ClientProtocolException("Unexpected status code: " + status);
|
||||||
}
|
}
|
||||||
EntityUtils.consume(response.getEntity());
|
EntityUtils.consume(response.getEntity());
|
||||||
Thread.sleep(10);
|
Thread.sleep(10);
|
||||||
|
} finally {
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (final Exception ex) {
|
} catch (final Exception ex) {
|
||||||
this.ex = ex;
|
this.ex = ex;
|
||||||
|
|
Loading…
Reference in New Issue