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 2022/10/18 22:18:48 UTC

[httpcomponents-core] branch master updated (4864b1314 -> cd7df1aad)

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

olegk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


 discard 4864b1314 HTTPCORE-726: Improved capacity management in SharedInputBuffer
 discard 4c35c268e Fixed integration tests broken by JUnit 5 upgrade
     new 1f1c77fe4 Fixed integration tests broken by JUnit 5 upgrade
     new cd7df1aad HTTPCORE-726: Improved capacity management in SharedInputBuffer

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (4864b1314)
            \
             N -- N -- N   refs/heads/master (cd7df1aad)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/hc/core5/testing/SocksProxy.java    | 293 ---------------------
 .../testing/nio/H2SocksProxyIntegrationTest.java   |  72 -----
 .../nio/Http1SocksProxyIntegrationTest.java        |  76 ------
 3 files changed, 441 deletions(-)
 delete mode 100644 httpcore5-testing/src/main/java/org/apache/hc/core5/testing/SocksProxy.java
 delete mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2SocksProxyIntegrationTest.java
 delete mode 100644 httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1SocksProxyIntegrationTest.java


[httpcomponents-core] 01/02: Fixed integration tests broken by JUnit 5 upgrade

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 1f1c77fe46913ce34b0ee87a4cf0c0a55d491463
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Tue Oct 18 23:37:16 2022 +0200

    Fixed integration tests broken by JUnit 5 upgrade
---
 .../org/apache/hc/core5/testing/SocksProxy.java    | 293 ---------------------
 ...gResponseOutOfOrderStrategyIntegrationTest.java |   3 +-
 .../hc/core5/testing/nio/H2IntegrationTest.java    |  39 +--
 .../nio/H2ServerAndMultiplexingRequesterTest.java  |   2 +
 .../testing/nio/H2SocksProxyIntegrationTest.java   |  67 -----
 .../hc/core5/testing/nio/Http1IntegrationTest.java |  39 +--
 .../nio/Http1SocksProxyIntegrationTest.java        |  70 -----
 .../testing/nio/InternalH2ServerTestBase.java      |  19 +-
 pom.xml                                            |  11 +
 9 files changed, 68 insertions(+), 475 deletions(-)

diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/SocksProxy.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/SocksProxy.java
deleted file mode 100644
index 7d8234d8e..000000000
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/SocksProxy.java
+++ /dev/null
@@ -1,293 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-package org.apache.hc.core5.testing;
-
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.InetAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.SocketAddress;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.hc.core5.net.InetAddressUtils;
-import org.apache.hc.core5.util.TimeValue;
-
-/**
- * Cheap and nasty SOCKS protocol version 5 proxy, recommended for use in unit tests only so we can test our SOCKS client code.
- */
-public class SocksProxy {
-
-    private static class SocksProxyHandler {
-
-        public static final int VERSION_5 = 5;
-        public static final int COMMAND_CONNECT = 1;
-        public static final int ATYP_DOMAINNAME = 3;
-
-        private final SocksProxy parent;
-        private final Socket socket;
-        private volatile Socket remote;
-
-        public SocksProxyHandler(final SocksProxy parent, final Socket socket) {
-            this.parent = parent;
-            this.socket = socket;
-        }
-
-        public void start() {
-            new Thread(new Runnable() {
-                @Override
-                public void run() {
-                    try {
-                        final DataInputStream input = new DataInputStream(socket.getInputStream());
-                        final DataOutputStream output = new DataOutputStream(socket.getOutputStream());
-                        final Socket target = establishConnection(input, output);
-                        remote = target;
-
-                        final Thread t1 = pumpStream(input, target.getOutputStream());
-                        final Thread t2 = pumpStream(target.getInputStream(), output);
-                        try {
-                            t1.join();
-                        } catch (final InterruptedException e) {
-                        }
-                        try {
-                            t2.join();
-                        } catch (final InterruptedException e) {
-                        }
-                    } catch (final IOException e) {
-                    } finally {
-                        parent.cleanupSocksProxyHandler(SocksProxyHandler.this);
-                    }
-                }
-
-                private Socket establishConnection(final DataInputStream input, final DataOutputStream output) throws IOException {
-                    final int clientVersion = input.readUnsignedByte();
-                    if (clientVersion != VERSION_5) {
-                        throw new IOException("SOCKS implementation only supports version 5");
-                    }
-                    final int nMethods = input.readUnsignedByte();
-                    for (int i = 0; i < nMethods; i++) {
-                        input.readUnsignedByte(); // auth method
-                    }
-                    // response
-                    output.writeByte(VERSION_5);
-                    output.writeByte(0); // no auth method
-                    output.flush();
-
-                    input.readUnsignedByte(); // client version again
-                    final int command = input.readUnsignedByte();
-                    if (command != COMMAND_CONNECT) {
-                        throw new IOException("SOCKS implementation only supports CONNECT command");
-                    }
-                    input.readUnsignedByte(); // reserved
-
-                    final String targetHost;
-                    final byte[] targetAddress;
-                    final int addressType = input.readUnsignedByte();
-                    switch (addressType) {
-                        case InetAddressUtils.IPV4:
-                            targetHost = null;
-                            targetAddress = new byte[4];
-                            for (int i = 0; i < targetAddress.length; i++) {
-                                targetAddress[i] = input.readByte();
-                            }
-                            break;
-                        case InetAddressUtils.IPV6:
-                            targetHost = null;
-                            targetAddress = new byte[16];
-                            for (int i = 0; i < targetAddress.length; i++) {
-                                targetAddress[i] = input.readByte();
-                            }
-                            break;
-                        case ATYP_DOMAINNAME:
-                            final int length = input.readUnsignedByte();
-                            final StringBuilder domainname = new StringBuilder();
-                            for (int i = 0; i < length; i++) {
-                                domainname.append((char) input.readUnsignedByte());
-                            }
-                            targetHost = domainname.toString();
-                            targetAddress = null;
-                            break;
-                        default:
-                            throw new IOException("Unsupported address type: " + addressType);
-                    }
-
-                    final int targetPort = input.readUnsignedShort();
-                    final Socket target;
-                    if (targetHost != null) {
-                        target = new Socket(targetHost, targetPort);
-                    } else {
-                        target = new Socket(InetAddress.getByAddress(targetAddress), targetPort);
-                    }
-
-                    output.writeByte(VERSION_5);
-                    output.writeByte(0); /* success */
-                    output.writeByte(0); /* reserved */
-                    final byte[] localAddress = target.getLocalAddress().getAddress();
-                    if (localAddress.length == 4) {
-                        output.writeByte(InetAddressUtils.IPV4);
-                    } else if (localAddress.length == 16) {
-                        output.writeByte(InetAddressUtils.IPV6);
-                    } else {
-                        throw new IOException("Unsupported localAddress byte length: " + localAddress.length);
-                    }
-                    output.write(localAddress);
-                    output.writeShort(target.getLocalPort());
-                    output.flush();
-
-                    return target;
-                }
-
-                private Thread pumpStream(final InputStream input, final OutputStream output) {
-                    final Thread t = new Thread(() -> {
-                        final byte[] buffer = new byte[1024 * 8];
-                        try {
-                            while (true) {
-                                final int read = input.read(buffer);
-                                if (read < 0) {
-                                    break;
-                                }
-                                output.write(buffer, 0, read);
-                                output.flush();
-                            }
-                        } catch (final IOException e) {
-                        } finally {
-                            shutdown();
-                        }
-                    });
-                    t.start();
-                    return t;
-                }
-
-            }).start();
-        }
-
-        public void shutdown() {
-            try {
-                this.socket.close();
-            } catch (final IOException e) {
-            }
-            if (this.remote != null) {
-                try {
-                    this.remote.close();
-                } catch (final IOException e) {
-                }
-            }
-        }
-
-    }
-
-    private final int port;
-
-    private final List<SocksProxyHandler> handlers = new ArrayList<>();
-    private ServerSocket server;
-    private Thread serverThread;
-
-    public SocksProxy() {
-        this(0);
-    }
-
-    public SocksProxy(final int port) {
-        this.port = port;
-    }
-
-    public synchronized void start() throws IOException {
-        if (this.server == null) {
-            this.server = new ServerSocket(this.port);
-            this.serverThread = new Thread(() -> {
-                try {
-                    while (true) {
-                        final Socket socket = server.accept();
-                        startSocksProxyHandler(socket);
-                    }
-                } catch (final IOException e) {
-                } finally {
-                    if (server != null) {
-                        try {
-                            server.close();
-                        } catch (final IOException e) {
-                        }
-                        server = null;
-                    }
-                }
-            });
-            this.serverThread.start();
-        }
-    }
-
-    public void shutdown(final TimeValue timeout) throws InterruptedException {
-        final long waitUntil = System.currentTimeMillis() + timeout.toMilliseconds();
-        Thread t = null;
-        synchronized (this) {
-            if (this.server != null) {
-                try {
-                    this.server.close();
-                } catch (final IOException e) {
-                } finally {
-                    this.server = null;
-                }
-                t = this.serverThread;
-                this.serverThread = null;
-            }
-            for (final SocksProxyHandler handler : this.handlers) {
-                handler.shutdown();
-            }
-            while (!this.handlers.isEmpty()) {
-                final long waitTime = waitUntil - System.currentTimeMillis();
-                if (waitTime > 0) {
-                    wait(waitTime);
-                }
-            }
-        }
-        if (t != null) {
-            final long waitTime = waitUntil - System.currentTimeMillis();
-            if (waitTime > 0) {
-                t.join(waitTime);
-            }
-        }
-    }
-
-    protected void startSocksProxyHandler(final Socket socket) {
-        final SocksProxyHandler handler = new SocksProxyHandler(this, socket);
-        synchronized (this) {
-            this.handlers.add(handler);
-        }
-        handler.start();
-    }
-
-    protected synchronized void cleanupSocksProxyHandler(final SocksProxyHandler handler) {
-        this.handlers.remove(handler);
-    }
-
-    public SocketAddress getProxyAddress() {
-        return this.server.getLocalSocketAddress();
-    }
-
-}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/MonitoringResponseOutOfOrderStrategyIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/MonitoringResponseOutOfOrderStrategyIntegrationTest.java
index bf2f405bb..277af758b 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/MonitoringResponseOutOfOrderStrategyIntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/MonitoringResponseOutOfOrderStrategyIntegrationTest.java
@@ -57,7 +57,6 @@ import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.junit.jupiter.api.extension.Extensions;
 import org.junit.jupiter.migrationsupport.rules.ExternalResourceSupport;
-import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.rules.ExternalResource;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -70,7 +69,7 @@ public class MonitoringResponseOutOfOrderStrategyIntegrationTest {
     private static final int BUFFER_SIZE = 16 * 1024;
     private static final Timeout TIMEOUT = Timeout.ofSeconds(3);
 
-    @ParameterizedTest(name = "{0}")
+    @Parameterized.Parameters(name = "{0}")
     public static Collection<Object[]> protocols() {
         return Arrays.asList(new Object[][]{
                 { URIScheme.HTTP },
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java
index 173798c51..b29ecce85 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java
@@ -122,20 +122,18 @@ import org.apache.hc.core5.util.TextUtils;
 import org.apache.hc.core5.util.TimeValue;
 import org.apache.hc.core5.util.Timeout;
 import org.hamcrest.CoreMatchers;
+import org.junit.Rule;
 import org.junit.Test;
-import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.junit.jupiter.api.extension.Extensions;
 import org.junit.jupiter.migrationsupport.rules.ExternalResourceSupport;
+import org.junit.rules.ExternalResource;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-;
-
 @RunWith(Parameterized.class)
 @Extensions({@ExtendWith({ExternalResourceSupport.class})})
 public class H2IntegrationTest extends InternalH2ServerTestBase {
@@ -159,23 +157,28 @@ public class H2IntegrationTest extends InternalH2ServerTestBase {
 
     private H2TestClient client;
 
-    @BeforeEach
-    public void setup() throws Exception {
-        log.debug("Starting up test client");
-        client = new H2TestClient(buildReactorConfig(),
-                scheme == URIScheme.HTTPS ? SSLTestContexts.createClientSSLContext() : null, null, null);
-    }
+    @Rule
+    public ExternalResource clientResource = new ExternalResource() {
 
-    protected IOReactorConfig buildReactorConfig() {
-        return IOReactorConfig.DEFAULT;
-    }
+        @Override
+        protected void before() throws Throwable {
+            log.debug("Starting up test client");
+            client = new H2TestClient(buildReactorConfig(),
+                    scheme == URIScheme.HTTPS ? SSLTestContexts.createClientSSLContext() : null, null, null);
+        }
 
-    @AfterEach
-    public void cleanup() throws Exception {
-        log.debug("Shutting down test client");
-        if (client != null) {
-            client.shutdown(TimeValue.ofSeconds(5));
+        @Override
+        protected void after() {
+            log.debug("Shutting down test client");
+            if (client != null) {
+                client.shutdown(TimeValue.ofSeconds(5));
+            }
         }
+
+    };
+
+    protected IOReactorConfig buildReactorConfig() {
+        return IOReactorConfig.DEFAULT;
     }
 
     private URI createRequestURI(final InetSocketAddress serverEndpoint, final String path) {
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ServerAndMultiplexingRequesterTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ServerAndMultiplexingRequesterTest.java
index 0040076df..fc3935261 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ServerAndMultiplexingRequesterTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ServerAndMultiplexingRequesterTest.java
@@ -54,6 +54,7 @@ import org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler;
 import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
 import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
 import org.apache.hc.core5.http.protocol.HttpCoreContext;
+import org.apache.hc.core5.http2.HttpVersionPolicy;
 import org.apache.hc.core5.http2.impl.nio.bootstrap.H2MultiplexingRequester;
 import org.apache.hc.core5.http2.impl.nio.bootstrap.H2MultiplexingRequesterBootstrap;
 import org.apache.hc.core5.http2.impl.nio.bootstrap.H2ServerBootstrap;
@@ -111,6 +112,7 @@ public class H2ServerAndMultiplexingRequesterTest {
                             IOReactorConfig.custom()
                                     .setSoTimeout(TIMEOUT)
                                     .build())
+                    .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
                     .setTlsStrategy(scheme == URIScheme.HTTPS  ?
                             new H2ServerTlsStrategy(SSLTestContexts.createServerSSLContext()) : null)
                     .setIOSessionListener(LoggingIOSessionListener.INSTANCE)
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2SocksProxyIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2SocksProxyIntegrationTest.java
deleted file mode 100644
index 338c411f9..000000000
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2SocksProxyIntegrationTest.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-package org.apache.hc.core5.testing.nio;
-
-import org.apache.hc.core5.http.URIScheme;
-import org.apache.hc.core5.reactor.IOReactorConfig;
-import org.apache.hc.core5.testing.SocksProxy;
-import org.apache.hc.core5.util.TimeValue;
-import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.BeforeAll;
-
-public class H2SocksProxyIntegrationTest extends H2IntegrationTest {
-
-    protected static SocksProxy PROXY;
-
-    @BeforeAll
-    public static void before() throws Throwable {
-        PROXY = new SocksProxy();
-        PROXY.start();
-    }
-
-    @AfterAll
-    public static void after() {
-        if (PROXY != null) {
-            try {
-                PROXY.shutdown(TimeValue.ofSeconds(5));
-            } catch (final Exception ignore) {
-            }
-            PROXY = null;
-        }
-    }
-
-    public H2SocksProxyIntegrationTest(final URIScheme scheme) {
-        super(scheme);
-    }
-
-    @Override
-    protected IOReactorConfig buildReactorConfig() {
-        return IOReactorConfig.custom().setSocksProxyAddress(PROXY.getProxyAddress()).build();
-    }
-
-}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
index 664a05906..9185047db 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1IntegrationTest.java
@@ -133,13 +133,13 @@ import org.apache.hc.core5.util.TextUtils;
 import org.apache.hc.core5.util.TimeValue;
 import org.apache.hc.core5.util.Timeout;
 import org.hamcrest.CoreMatchers;
+import org.junit.Rule;
 import org.junit.Test;
-import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.junit.jupiter.api.extension.Extensions;
 import org.junit.jupiter.migrationsupport.rules.ExternalResourceSupport;
+import org.junit.rules.ExternalResource;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 import org.slf4j.Logger;
@@ -170,24 +170,29 @@ public class Http1IntegrationTest extends InternalHttp1ServerTestBase {
 
     private Http1TestClient client;
 
-    @BeforeEach
-    public void setup() throws Exception {
-        log.debug("Starting up test client");
-        client = new Http1TestClient(
-                buildReactorConfig(),
-                scheme == URIScheme.HTTPS ? SSLTestContexts.createClientSSLContext() : null, null, null);
-    }
+    @Rule
+    public ExternalResource clientResource = new ExternalResource() {
 
-    protected IOReactorConfig buildReactorConfig() {
-        return IOReactorConfig.DEFAULT;
-    }
+        @Override
+        protected void before() throws Throwable {
+            log.debug("Starting up test client");
+            client = new Http1TestClient(
+                    buildReactorConfig(),
+                    scheme == URIScheme.HTTPS ? SSLTestContexts.createClientSSLContext() : null, null, null);
+        }
 
-    @AfterEach
-    public void cleanup() throws Exception {
-        log.debug("Shutting down test client");
-        if (client != null) {
-            client.shutdown(TimeValue.ofSeconds(5));
+        @Override
+        protected void after() {
+            log.debug("Shutting down test client");
+            if (client != null) {
+                client.shutdown(TimeValue.ofSeconds(5));
+            }
         }
+
+    };
+
+    protected IOReactorConfig buildReactorConfig() {
+        return IOReactorConfig.DEFAULT;
     }
 
     private URI createRequestURI(final InetSocketAddress serverEndpoint, final String path) {
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1SocksProxyIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1SocksProxyIntegrationTest.java
deleted file mode 100644
index dc264acbc..000000000
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1SocksProxyIntegrationTest.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-package org.apache.hc.core5.testing.nio;
-
-import org.apache.hc.core5.http.URIScheme;
-import org.apache.hc.core5.reactor.IOReactorConfig;
-import org.apache.hc.core5.testing.SocksProxy;
-import org.apache.hc.core5.util.TimeValue;
-import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.jupiter.api.extension.Extensions;
-import org.junit.jupiter.migrationsupport.rules.ExternalResourceSupport;
-@Extensions({@ExtendWith({ExternalResourceSupport.class})})
-public class Http1SocksProxyIntegrationTest extends Http1IntegrationTest {
-
-    protected static SocksProxy PROXY;
-
-    @BeforeAll
-    public static void before() throws Throwable {
-        PROXY = new SocksProxy();
-        PROXY.start();
-    }
-
-    @AfterAll
-    public static void after() {
-        if (PROXY != null) {
-            try {
-                PROXY.shutdown(TimeValue.ofSeconds(5));
-            } catch (final Exception ignore) {
-            }
-            PROXY = null;
-        }
-    }
-
-    public Http1SocksProxyIntegrationTest(final URIScheme scheme) {
-        super(scheme);
-    }
-
-    @Override
-    protected IOReactorConfig buildReactorConfig() {
-        return IOReactorConfig.custom().setSocksProxyAddress(PROXY.getProxyAddress()).build();
-    }
-
-}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/InternalH2ServerTestBase.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/InternalH2ServerTestBase.java
index fae7f2405..af5f2933c 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/InternalH2ServerTestBase.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/InternalH2ServerTestBase.java
@@ -31,14 +31,15 @@ import org.apache.hc.core5.http.URIScheme;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.testing.SSLTestContexts;
 import org.apache.hc.core5.util.TimeValue;
-import org.junit.jupiter.api.extension.AfterEachCallback;
-import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.Rule;
 import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.Extensions;
+import org.junit.jupiter.migrationsupport.rules.ExternalResourceSupport;
+import org.junit.rules.ExternalResource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@ExtendWith({InternalH2ServerTestBase.serverResource.class})
+@Extensions({@ExtendWith({ExternalResourceSupport.class})})
 public abstract class InternalH2ServerTestBase {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
@@ -55,22 +56,24 @@ public abstract class InternalH2ServerTestBase {
 
     protected H2TestServer server;
 
-    class serverResource implements AfterEachCallback, BeforeEachCallback {
+    @Rule
+    public ExternalResource serverResource = new ExternalResource() {
 
         @Override
-        public void beforeEach(final ExtensionContext context) throws Exception {
+        protected void before() throws Throwable {
             log.debug("Starting up test server");
             server = new H2TestServer(IOReactorConfig.DEFAULT,
                     scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null, null, null);
         }
 
         @Override
-        public void afterEach(final ExtensionContext context) throws Exception {
+        protected void after() {
             log.debug("Shutting down test server");
             if (server != null) {
                 server.shutdown(TimeValue.ofSeconds(5));
             }
         }
-    }
+
+    };
 
 }
diff --git a/pom.xml b/pom.xml
index 0b66a4468..7246a8e87 100644
--- a/pom.xml
+++ b/pom.xml
@@ -244,6 +244,17 @@
           </execution>
         </executions>
       </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <dependencies>
+          <dependency>
+            <groupId>org.junit.vintage</groupId>
+            <artifactId>junit-vintage-engine</artifactId>
+            <version>${junit.version}</version>
+          </dependency>
+        </dependencies>
+      </plugin>
       <plugin>
         <groupId>org.apache.rat</groupId>
         <artifactId>apache-rat-plugin</artifactId>


[httpcomponents-core] 02/02: HTTPCORE-726: Improved capacity management in SharedInputBuffer

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit cd7df1aadfc9a8ff60e753a12065abecfd1d0bbb
Author: John Leacox <jo...@gmail.com>
AuthorDate: Mon Oct 17 17:29:06 2022 +0200

    HTTPCORE-726: Improved capacity management in SharedInputBuffer
---
 .../nio/support/classic/SharedInputBuffer.java     | 37 ++++++++++++++--------
 .../nio/support/classic/TestSharedInputBuffer.java |  2 +-
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java
index b19367540..45dea032c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java
@@ -29,6 +29,7 @@ package org.apache.hc.core5.http.nio.support.classic;
 import java.io.IOException;
 import java.io.InterruptedIOException;
 import java.nio.ByteBuffer;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.hc.core5.annotation.Contract;
@@ -41,14 +42,19 @@ import org.apache.hc.core5.http.nio.CapacityChannel;
 @Contract(threading = ThreadingBehavior.SAFE)
 public final class SharedInputBuffer extends AbstractSharedBuffer implements ContentInputBuffer {
 
+    private final int initialBufferSize;
+    private final AtomicInteger capacityIncrement;
+
     private volatile CapacityChannel capacityChannel;
 
     public SharedInputBuffer(final ReentrantLock lock, final int initialBufferSize) {
         super(lock, initialBufferSize);
+        this.initialBufferSize = initialBufferSize;
+        this.capacityIncrement = new AtomicInteger(0);
     }
 
     public SharedInputBuffer(final int bufferSize) {
-        super(new ReentrantLock(), bufferSize);
+        this(new ReentrantLock(), bufferSize);
     }
 
     public int fill(final ByteBuffer src) {
@@ -65,13 +71,22 @@ public final class SharedInputBuffer extends AbstractSharedBuffer implements Con
         }
     }
 
+    private void incrementCapacity() throws IOException {
+        if (capacityChannel != null) {
+            final int increment = capacityIncrement.getAndSet(0);
+            if (increment > 0) {
+                capacityChannel.update(increment);
+            }
+        }
+    }
+
     public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
         lock.lock();
         try {
             this.capacityChannel = capacityChannel;
             setInputMode();
-            if (buffer().hasRemaining()) {
-                capacityChannel.update(buffer().remaining());
+            if (buffer().position() == 0) {
+                capacityChannel.update(initialBufferSize);
             }
         } finally {
             lock.unlock();
@@ -106,11 +121,9 @@ public final class SharedInputBuffer extends AbstractSharedBuffer implements Con
                 return -1;
             }
             final int b = buffer().get() & 0xff;
-            if (!buffer().hasRemaining() && capacityChannel != null) {
-                setInputMode();
-                if (buffer().hasRemaining()) {
-                    capacityChannel.update(buffer().remaining());
-                }
+            capacityIncrement.incrementAndGet();
+            if (!buffer().hasRemaining()) {
+                incrementCapacity();
             }
             return b;
         } finally {
@@ -132,11 +145,9 @@ public final class SharedInputBuffer extends AbstractSharedBuffer implements Con
             }
             final int chunk = Math.min(buffer().remaining(), len);
             buffer().get(b, off, chunk);
-            if (!buffer().hasRemaining() && capacityChannel != null) {
-                setInputMode();
-                if (buffer().hasRemaining()) {
-                    capacityChannel.update(buffer().remaining());
-                }
+            capacityIncrement.addAndGet(chunk);
+            if (!buffer().hasRemaining()) {
+                incrementCapacity();
             }
             return chunk;
         } finally {
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedInputBuffer.java b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedInputBuffer.java
index ac1d101cb..2bb8f81a1 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedInputBuffer.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/support/classic/TestSharedInputBuffer.java
@@ -132,7 +132,7 @@ public class TestSharedInputBuffer {
 
         Assertions.assertEquals(Boolean.TRUE, task1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
         Assertions.assertEquals(Integer.valueOf('a'), task2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
-        Mockito.verify(capacityChannel).update(10);
+        Mockito.verify(capacityChannel).update(1);
     }
 
     @Test