View Javadoc
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.hamcrest.MatcherAssert.assertThat;
30  
31  import java.util.concurrent.Future;
32  import java.util.concurrent.atomic.AtomicInteger;
33  
34  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
35  import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
36  import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
37  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
38  import org.apache.hc.core5.function.Resolver;
39  import org.apache.hc.core5.http.HttpHost;
40  import org.apache.hc.core5.http.HttpRequest;
41  import org.apache.hc.core5.http.HttpStatus;
42  import org.apache.hc.core5.http.URIScheme;
43  import org.apache.hc.core5.http.config.Http1Config;
44  import org.apache.hc.core5.testing.nio.H2TestServer;
45  import org.apache.hc.core5.util.TimeValue;
46  import org.hamcrest.CoreMatchers;
47  import org.junit.jupiter.api.Test;
48  
49  public abstract class TestHttp1RequestReExecution extends AbstractIntegrationTestBase {
50  
51      public TestHttp1RequestReExecution(final URIScheme scheme) {
52          super(scheme);
53      }
54  
55      protected H2TestServer startServer() throws Exception {
56          final Resolver<HttpRequest, TimeValue> serviceAvailabilityResolver = new Resolver<HttpRequest, TimeValue>() {
57  
58              private final AtomicInteger count = new AtomicInteger(0);
59  
60              @Override
61              public TimeValue resolve(final HttpRequest request) {
62                  final int n = count.incrementAndGet();
63                  return n <= 3 ? TimeValue.ofSeconds(1) : null;
64              }
65  
66          };
67  
68          return startServer(Http1Config.DEFAULT, null, handler ->
69                  new ServiceUnavailableAsyncDecorator(handler, serviceAvailabilityResolver));
70      }
71  
72      @Test
73      public void testGiveUpAfterOneRetry() throws Exception {
74          final H2TestServer server = startServer();
75          server.register("/random/*", AsyncRandomHandler::new);
76          final HttpHost target = targetHost();
77  
78          final CloseableHttpAsyncClient client = startClient(builder -> builder
79                  .setRetryStrategy(new DefaultHttpRequestRetryStrategy(1, TimeValue.ofSeconds(1))));
80  
81          final Future<SimpleHttpResponse> future = client.execute(
82                  SimpleRequestBuilder.get()
83                          .setHttpHost(target)
84                          .setPath("/random/2048")
85                          .build(), null);
86          final SimpleHttpResponse response = future.get();
87          assertThat(response, CoreMatchers.notNullValue());
88          assertThat(response.getCode(), CoreMatchers.equalTo(HttpStatus.SC_SERVICE_UNAVAILABLE));
89      }
90  
91      @Test
92      public void testDoNotGiveUpEasily() throws Exception {
93          final H2TestServer server = startServer();
94          server.register("/random/*", AsyncRandomHandler::new);
95          final HttpHost target = targetHost();
96  
97          final CloseableHttpAsyncClient client = startClient(builder -> builder
98                  .setRetryStrategy(new DefaultHttpRequestRetryStrategy(5, TimeValue.ofSeconds(1))));
99  
100         final Future<SimpleHttpResponse> future = client.execute(
101                 SimpleRequestBuilder.get()
102                         .setHttpHost(target)
103                         .setPath("/random/2048")
104                         .build(), null);
105         final SimpleHttpResponse response = future.get();
106         assertThat(response, CoreMatchers.notNullValue());
107         assertThat(response.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
108     }
109 
110 }