|
| 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 | +package org.apache.hc.client5.testing.async; |
| 28 | + |
| 29 | +import java.net.InetSocketAddress; |
| 30 | +import java.util.concurrent.CancellationException; |
| 31 | +import java.util.concurrent.Future; |
| 32 | +import java.util.function.Consumer; |
| 33 | + |
| 34 | +import org.apache.hc.client5.http.config.RequestConfig; |
| 35 | +import org.apache.hc.client5.http.config.TlsConfig; |
| 36 | +import org.apache.hc.client5.http.impl.async.InternalTestHttpAsyncExecRuntime; |
| 37 | +import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager; |
| 38 | +import org.apache.hc.client5.http.protocol.HttpClientContext; |
| 39 | +import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel; |
| 40 | +import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel; |
| 41 | +import org.apache.hc.client5.testing.extension.async.TestAsyncClient; |
| 42 | +import org.apache.hc.client5.testing.extension.async.TestAsyncResources; |
| 43 | +import org.apache.hc.client5.testing.extension.async.TestAsyncServer; |
| 44 | +import org.apache.hc.client5.testing.extension.async.TestAsyncServerBootstrap; |
| 45 | +import org.apache.hc.core5.concurrent.BasicFuture; |
| 46 | +import org.apache.hc.core5.concurrent.Cancellable; |
| 47 | +import org.apache.hc.core5.concurrent.FutureContribution; |
| 48 | +import org.apache.hc.core5.http.HttpHeaders; |
| 49 | +import org.apache.hc.core5.http.HttpHost; |
| 50 | +import org.apache.hc.core5.http.HttpRequest; |
| 51 | +import org.apache.hc.core5.http.HttpResponse; |
| 52 | +import org.apache.hc.core5.http.Message; |
| 53 | +import org.apache.hc.core5.http.URIScheme; |
| 54 | +import org.apache.hc.core5.http.nio.support.AsyncClientPipeline; |
| 55 | +import org.apache.hc.core5.http.support.BasicRequestBuilder; |
| 56 | +import org.apache.hc.core5.http2.HttpVersionPolicy; |
| 57 | +import org.apache.hc.core5.pool.PoolStats; |
| 58 | +import org.apache.hc.core5.reactor.ConnectionInitiator; |
| 59 | +import org.apache.hc.core5.util.TimeValue; |
| 60 | +import org.apache.hc.core5.util.Timeout; |
| 61 | +import org.junit.jupiter.api.Assertions; |
| 62 | +import org.junit.jupiter.api.Test; |
| 63 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 64 | + |
| 65 | +public class TestInternalHttpAsyncExecRuntime { |
| 66 | + |
| 67 | + public static final Timeout TIMEOUT = Timeout.ofMinutes(1); |
| 68 | + |
| 69 | + @RegisterExtension |
| 70 | + private final TestAsyncResources testResources; |
| 71 | + |
| 72 | + public TestInternalHttpAsyncExecRuntime() { |
| 73 | + this.testResources = new TestAsyncResources(URIScheme.HTTP, ClientProtocolLevel.STANDARD, ServerProtocolLevel.STANDARD, TIMEOUT); |
| 74 | + } |
| 75 | + |
| 76 | + public void configureServer(final Consumer<TestAsyncServerBootstrap> serverCustomizer) { |
| 77 | + testResources.configureServer(serverCustomizer); |
| 78 | + } |
| 79 | + |
| 80 | + public HttpHost startServer() throws Exception { |
| 81 | + final TestAsyncServer server = testResources.server(); |
| 82 | + final InetSocketAddress inetSocketAddress = server.start(); |
| 83 | + return new HttpHost(testResources.scheme().id, "localhost", inetSocketAddress.getPort()); |
| 84 | + } |
| 85 | + |
| 86 | + public TestAsyncClient startClient() throws Exception { |
| 87 | + final TestAsyncClient client = testResources.client(); |
| 88 | + client.start(); |
| 89 | + return client; |
| 90 | + } |
| 91 | + |
| 92 | + static final int REQ_NUM = 5; |
| 93 | + |
| 94 | + HttpRequest createRequest(final HttpHost target) { |
| 95 | + return BasicRequestBuilder.get() |
| 96 | + .setHttpHost(target) |
| 97 | + .setPath("/random/20000") |
| 98 | + .addHeader(HttpHeaders.HOST, target.toHostString()) |
| 99 | + .build(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void testExecutionCancellation_http11HardCancellation_connectionMarkedNonReusable() throws Exception { |
| 104 | + configureServer(bootstrap -> bootstrap.register("/random/*", AsyncRandomHandler::new)); |
| 105 | + final HttpHost target = startServer(); |
| 106 | + |
| 107 | + final TestAsyncClient client = startClient(); |
| 108 | + final ConnectionInitiator connectionInitiator = client.getImplementation(); |
| 109 | + final PoolingAsyncClientConnectionManager connectionManager = client.getConnectionManager(); |
| 110 | + for (int i = 0; i < REQ_NUM; i++) { |
| 111 | + final HttpClientContext context = HttpClientContext.create(); |
| 112 | + |
| 113 | + final InternalTestHttpAsyncExecRuntime testRuntime = new InternalTestHttpAsyncExecRuntime( |
| 114 | + connectionManager, |
| 115 | + connectionInitiator, |
| 116 | + TlsConfig.custom() |
| 117 | + .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_1) |
| 118 | + .build()); |
| 119 | + final Future<Boolean> connectFuture = testRuntime.leaseAndConnect(target, context); |
| 120 | + Assertions.assertTrue(connectFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit())); |
| 121 | + |
| 122 | + final BasicFuture<Message<HttpResponse, byte[]>> resultFuture = new BasicFuture<>(null); |
| 123 | + final Cancellable cancellable = testRuntime.execute( |
| 124 | + "test-" + i, |
| 125 | + AsyncClientPipeline.assemble() |
| 126 | + .request(createRequest(target)).noContent() |
| 127 | + .response().asByteArray() |
| 128 | + .result(new FutureContribution<Message<HttpResponse, byte[]>>(resultFuture) { |
| 129 | + |
| 130 | + @Override |
| 131 | + public void completed(final Message<HttpResponse, byte[]> result) { |
| 132 | + resultFuture.completed(result); |
| 133 | + } |
| 134 | + |
| 135 | + }) |
| 136 | + .create(), |
| 137 | + context); |
| 138 | + // sleep a bit |
| 139 | + Thread.sleep(i % 10); |
| 140 | + cancellable.cancel(); |
| 141 | + |
| 142 | + // The message exchange is expected to get aborted |
| 143 | + try { |
| 144 | + resultFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); |
| 145 | + } catch (final CancellationException expected) { |
| 146 | + } |
| 147 | + Assertions.assertTrue(testRuntime.isAborted()); |
| 148 | + testRuntime.discardEndpoint(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void testExecutionCancellation_http11NoHardCancellation_connectionAlive() throws Exception { |
| 154 | + configureServer(bootstrap -> bootstrap.register("/random/*", AsyncRandomHandler::new)); |
| 155 | + final HttpHost target = startServer(); |
| 156 | + |
| 157 | + final TestAsyncClient client = startClient(); |
| 158 | + final ConnectionInitiator connectionInitiator = client.getImplementation(); |
| 159 | + final PoolingAsyncClientConnectionManager connectionManager = client.getConnectionManager(); |
| 160 | + for (int i = 0; i < REQ_NUM; i++) { |
| 161 | + final HttpClientContext context = HttpClientContext.create(); |
| 162 | + context.setRequestConfig(RequestConfig.custom() |
| 163 | + .setHardCancellationEnabled(false) |
| 164 | + .build()); |
| 165 | + |
| 166 | + final InternalTestHttpAsyncExecRuntime testRuntime = new InternalTestHttpAsyncExecRuntime( |
| 167 | + connectionManager, |
| 168 | + connectionInitiator, |
| 169 | + TlsConfig.custom() |
| 170 | + .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_1) |
| 171 | + .build()); |
| 172 | + final Future<Boolean> connectFuture = testRuntime.leaseAndConnect(target, context); |
| 173 | + Assertions.assertTrue(connectFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit())); |
| 174 | + |
| 175 | + final BasicFuture<Message<HttpResponse, byte[]>> resultFuture = new BasicFuture<>(null); |
| 176 | + final Cancellable cancellable = testRuntime.execute( |
| 177 | + "test-" + i, |
| 178 | + AsyncClientPipeline.assemble() |
| 179 | + .request(createRequest(target)).noContent() |
| 180 | + .response().asByteArray() |
| 181 | + .result(new FutureContribution<Message<HttpResponse, byte[]>>(resultFuture) { |
| 182 | + |
| 183 | + @Override |
| 184 | + public void completed(final Message<HttpResponse, byte[]> result) { |
| 185 | + resultFuture.completed(result); |
| 186 | + } |
| 187 | + |
| 188 | + }) |
| 189 | + .create(), |
| 190 | + context); |
| 191 | + // sleep a bit |
| 192 | + Thread.sleep(i % 10); |
| 193 | + cancellable.cancel(); |
| 194 | + |
| 195 | + // The message exchange should not get aborted and is expected to successfully complete |
| 196 | + final Message<HttpResponse, byte[]> message = resultFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); |
| 197 | + Assertions.assertNotNull(message); |
| 198 | + Assertions.assertFalse(testRuntime.isAborted()); |
| 199 | + // The underlying connection is expected to stay valid |
| 200 | + Assertions.assertTrue(testRuntime.isEndpointConnected()); |
| 201 | + testRuntime.markConnectionReusable(null, TimeValue.ofMinutes(1)); |
| 202 | + testRuntime.releaseEndpoint(); |
| 203 | + |
| 204 | + final PoolStats totalStats = connectionManager.getTotalStats(); |
| 205 | + Assertions.assertTrue(totalStats.getAvailable() > 0); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | +} |
0 commit comments