|
| 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 | + |
| 28 | +package org.apache.hc.client5.testing.async; |
| 29 | + |
| 30 | +import org.apache.hc.client5.http.ConnectTimeoutException; |
| 31 | +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; |
| 32 | +import org.apache.hc.client5.http.config.ConnectionConfig; |
| 33 | +import org.apache.hc.client5.http.config.RequestConfig; |
| 34 | +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; |
| 35 | +import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder; |
| 36 | +import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager; |
| 37 | +import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder; |
| 38 | +import org.apache.hc.core5.reactor.IOReactorConfig; |
| 39 | +import org.junit.jupiter.api.Timeout; |
| 40 | +import org.junit.jupiter.params.ParameterizedTest; |
| 41 | +import org.junit.jupiter.params.provider.ValueSource; |
| 42 | + |
| 43 | +import java.time.Duration; |
| 44 | +import java.time.temporal.ChronoUnit; |
| 45 | +import java.util.concurrent.ExecutionException; |
| 46 | + |
| 47 | +import static java.lang.String.format; |
| 48 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 49 | +import static org.apache.hc.core5.util.TimeValue.ofMilliseconds; |
| 50 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 51 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 53 | + |
| 54 | +@SuppressWarnings("deprecation") |
| 55 | +class TestAsyncConnectionTimeouts { |
| 56 | + private static final Duration EXPECTED_TIMEOUT = Duration.ofMillis(500); |
| 57 | + |
| 58 | + @Timeout(5) |
| 59 | + @ParameterizedTest |
| 60 | + @ValueSource(strings = { "http", "https" }) |
| 61 | + void testRequestConfig(final String scheme) throws Exception { |
| 62 | + try ( |
| 63 | + final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create() |
| 64 | + .setIOReactorConfig(IOReactorConfig.custom().setSelectInterval(ofMilliseconds(50)).build()) |
| 65 | + .setDefaultRequestConfig(RequestConfig.custom() |
| 66 | + .setConnectTimeout(EXPECTED_TIMEOUT.toMillis(), MILLISECONDS) |
| 67 | + .build()) |
| 68 | + .build() |
| 69 | + ) { |
| 70 | + warmUpClient(client); |
| 71 | + assertTimeout(getRequest(scheme), client); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Timeout(5) |
| 76 | + @ParameterizedTest |
| 77 | + @ValueSource(strings = { "http", "https" }) |
| 78 | + void testConnectionConfig(final String scheme) throws Exception { |
| 79 | + final PoolingAsyncClientConnectionManager connMgr = PoolingAsyncClientConnectionManagerBuilder.create() |
| 80 | + .setDefaultConnectionConfig( |
| 81 | + ConnectionConfig.custom() |
| 82 | + .setConnectTimeout(EXPECTED_TIMEOUT.toMillis(), MILLISECONDS) |
| 83 | + .build()) |
| 84 | + .build(); |
| 85 | + try ( |
| 86 | + final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create() |
| 87 | + .setIOReactorConfig(IOReactorConfig.custom().setSelectInterval(ofMilliseconds(50)).build()) |
| 88 | + .setConnectionManager(connMgr) |
| 89 | + .build() |
| 90 | + ) { |
| 91 | + warmUpClient(client); |
| 92 | + assertTimeout(getRequest(scheme), client); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private static void warmUpClient(final CloseableHttpAsyncClient client) { |
| 97 | + client.start(); |
| 98 | + } |
| 99 | + |
| 100 | + private static SimpleHttpRequest getRequest(final String scheme) { |
| 101 | + return SimpleHttpRequest.create("GET", scheme + "://198.51.100.1/ping"); |
| 102 | + } |
| 103 | + |
| 104 | + private static void assertTimeout(final SimpleHttpRequest request, final CloseableHttpAsyncClient client) { |
| 105 | + final long startTime = System.nanoTime(); |
| 106 | + final Throwable ex = assertThrows(ExecutionException.class, () -> client.execute(request, null).get()) |
| 107 | + .getCause(); |
| 108 | + final Duration actualTime = Duration.of(System.nanoTime() - startTime, ChronoUnit.NANOS); |
| 109 | + assertTrue(actualTime.toMillis() > EXPECTED_TIMEOUT.toMillis() / 2, |
| 110 | + format("Connection attempt timed out too soon (only %,d out of %,d ms)", |
| 111 | + actualTime.toMillis(), |
| 112 | + EXPECTED_TIMEOUT.toMillis())); |
| 113 | + assertTrue(actualTime.toMillis() < EXPECTED_TIMEOUT.toMillis() * 2, |
| 114 | + format("Connection attempt timed out too late (%,d out of %,d ms)", |
| 115 | + actualTime.toMillis(), |
| 116 | + EXPECTED_TIMEOUT.toMillis())); |
| 117 | + assertInstanceOf(ConnectTimeoutException.class, ex); |
| 118 | + assertTrue(ex.getMessage().contains(EXPECTED_TIMEOUT.toMillis() + " MILLISECONDS"), ex.getMessage()); |
| 119 | + } |
| 120 | +} |
0 commit comments