You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2019/08/22 14:44:29 UTC

[httpcomponents-client] 01/02: Fixed concurrent use of threading unsafe HttpUriRequest messages

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch development
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git

commit 779bb8c53b0ce03465071df206fd6d2dd47889f8
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Aug 22 16:08:12 2019 +0200

    Fixed concurrent use of threading unsafe HttpUriRequest messages
---
 .../integration/TestIdleConnectionEviction.java    | 38 ++++++++++++----------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestIdleConnectionEviction.java b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestIdleConnectionEviction.java
index eee9d36..ffab44b 100644
--- a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestIdleConnectionEviction.java
+++ b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestIdleConnectionEviction.java
@@ -27,14 +27,14 @@
 
 package org.apache.http.impl.client.integration;
 
+import java.net.URI;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
 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.HttpUriRequest;
+import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.IdleConnectionEvictor;
 import org.apache.http.localserver.LocalServerTestBase;
 import org.apache.http.util.EntityUtils;
@@ -53,10 +53,10 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
                 this.connManager, 50, TimeUnit.MILLISECONDS);
         idleConnectionMonitor.start();
 
-        final HttpGet httpget = new HttpGet("/random/1024");
+        final URI requestUri = new URI("/random/1024");
         final WorkerThread[] workers = new WorkerThread[5];
         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) {
             worker.start();
@@ -73,22 +73,22 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
 
     static class WorkerThread extends Thread {
 
-        private final HttpClient httpclient;
+        private final CloseableHttpClient httpclient;
         private final HttpHost target;
-        private final HttpUriRequest request;
+        private final URI requestUri;
         private final int count;
 
         private volatile Exception ex;
 
         public WorkerThread(
-                final HttpClient httpclient,
+                final CloseableHttpClient httpclient,
                 final HttpHost target,
-                final HttpUriRequest request,
+                final URI requestUri,
                 final int count) {
             super();
             this.httpclient = httpclient;
             this.target = target;
-            this.request = request;
+            this.requestUri = requestUri;
             this.count = count;
         }
 
@@ -96,14 +96,18 @@ public class TestIdleConnectionEviction extends LocalServerTestBase {
         public void run() {
             try {
                 for (int i = 0; i < this.count; i++) {
-                    final HttpResponse response = this.httpclient.execute(this.target, this.request);
-                    final int status = response.getStatusLine().getStatusCode();
-                    if (status != 200) {
-                        this.request.abort();
-                        throw new ClientProtocolException("Unexpected status code: " + status);
+                    final HttpGet httpget = new HttpGet(this.requestUri);
+                    final CloseableHttpResponse response = this.httpclient.execute(this.target, httpget);
+                    try {
+                        final int status = response.getStatusLine().getStatusCode();
+                        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) {
                 this.ex = ex;