|
| 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 static org.apache.hc.core5.util.ReflectionUtils.determineJRELevel; |
| 30 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 31 | + |
| 32 | +import java.util.concurrent.ExecutionException; |
| 33 | +import java.util.concurrent.Future; |
| 34 | + |
| 35 | +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; |
| 36 | +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; |
| 37 | +import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder; |
| 38 | +import org.apache.hc.client5.http.config.ConnectionConfig; |
| 39 | +import org.apache.hc.client5.http.config.RequestConfig; |
| 40 | +import org.apache.hc.client5.http.config.TlsConfig; |
| 41 | +import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager; |
| 42 | +import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel; |
| 43 | +import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel; |
| 44 | +import org.apache.hc.client5.testing.extension.async.TestAsyncClient; |
| 45 | +import org.apache.hc.core5.http.HttpHost; |
| 46 | +import org.apache.hc.core5.http.URIScheme; |
| 47 | +import org.apache.hc.core5.http2.H2StreamTimeoutException; |
| 48 | +import org.apache.hc.core5.http2.HttpVersionPolicy; |
| 49 | +import org.apache.hc.core5.pool.PoolStats; |
| 50 | +import org.apache.hc.core5.util.Timeout; |
| 51 | +import org.junit.jupiter.api.Assertions; |
| 52 | +import org.junit.jupiter.api.Disabled; |
| 53 | +import org.junit.jupiter.api.Nested; |
| 54 | +import org.junit.jupiter.api.Test; |
| 55 | + |
| 56 | +abstract class AbstractTestHttp2StreamResponseTimeout extends AbstractIntegrationTestBase { |
| 57 | + |
| 58 | + public AbstractTestHttp2StreamResponseTimeout(final URIScheme scheme) { |
| 59 | + this(scheme, false); |
| 60 | + } |
| 61 | + |
| 62 | + public AbstractTestHttp2StreamResponseTimeout(final URIScheme scheme, final boolean useUnixDomainSocket) { |
| 63 | + super(scheme, ClientProtocolLevel.STANDARD, ServerProtocolLevel.H2_ONLY, useUnixDomainSocket); |
| 64 | + } |
| 65 | + |
| 66 | + void checkAssumptions() { |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testResponseTimeout() throws Exception { |
| 71 | + checkAssumptions(); |
| 72 | + configureServer(bootstrap -> bootstrap.register("/random/*", AsyncRandomHandler::new)); |
| 73 | + final HttpHost target = startServer(); |
| 74 | + |
| 75 | + final TestAsyncClient client = startClient(); |
| 76 | + final PoolingAsyncClientConnectionManager connManager = client.getConnectionManager(); |
| 77 | + connManager.setDefaultConnectionConfig(ConnectionConfig.custom() |
| 78 | + .setSocketTimeout(Timeout.ofMinutes(1)) |
| 79 | + .build()); |
| 80 | + connManager.setDefaultTlsConfig(TlsConfig.custom() |
| 81 | + .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2) |
| 82 | + .build()); |
| 83 | + |
| 84 | + final SimpleHttpRequest request1 = SimpleRequestBuilder.get() |
| 85 | + .setHttpHost(target) |
| 86 | + .setPath("/random/1024") |
| 87 | + .setRequestConfig(RequestConfig.custom() |
| 88 | + .setUnixDomainSocket(getUnixDomainSocket()) |
| 89 | + .build()) |
| 90 | + .build(); |
| 91 | + final SimpleHttpRequest request2 = SimpleRequestBuilder.get() |
| 92 | + .setHttpHost(target) |
| 93 | + .setPath("/random/1024?delay=1000") |
| 94 | + .setRequestConfig(RequestConfig.custom() |
| 95 | + .setUnixDomainSocket(getUnixDomainSocket()) |
| 96 | + .setResponseTimeout(Timeout.ofMilliseconds(100)) |
| 97 | + .build()) |
| 98 | + .build(); |
| 99 | + |
| 100 | + final Future<SimpleHttpResponse> future1 = client.execute(request1, null, null); |
| 101 | + final Future<SimpleHttpResponse> future2 = client.execute(request2, null, null); |
| 102 | + final SimpleHttpResponse response1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); |
| 103 | + Assertions.assertNotNull(response1); |
| 104 | + final ExecutionException exception = Assertions.assertThrows(ExecutionException.class, () -> |
| 105 | + future2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit())); |
| 106 | + Assertions.assertInstanceOf(H2StreamTimeoutException.class, exception.getCause()); |
| 107 | + |
| 108 | + final PoolStats totalStats = connManager.getTotalStats(); |
| 109 | + Assertions.assertTrue(totalStats.getAvailable() > 0); |
| 110 | + } |
| 111 | + |
| 112 | +} |
| 113 | + |
| 114 | +@Disabled |
| 115 | +public class TestHttp2StreamResponseTimeout { |
| 116 | + |
| 117 | + @Nested |
| 118 | + class Http extends AbstractTestHttp2StreamResponseTimeout { |
| 119 | + public Http() { |
| 120 | + super(URIScheme.HTTP, false); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Nested |
| 125 | + class Https extends AbstractTestHttp2StreamResponseTimeout { |
| 126 | + public Https() { |
| 127 | + super(URIScheme.HTTPS, false); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Nested |
| 132 | + class Uds extends AbstractTestHttp2StreamResponseTimeout { |
| 133 | + public Uds() { |
| 134 | + super(URIScheme.HTTP, true); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + void checkAssumptions() { |
| 139 | + assumeTrue(determineJRELevel() >= 16, "Async UDS requires Java 16+"); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments