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