1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }