-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRetryingWebClientTest.java
More file actions
224 lines (196 loc) · 9.11 KB
/
RetryingWebClientTest.java
File metadata and controls
224 lines (196 loc) · 9.11 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.uid2.optout.web;
import io.netty.handler.codec.http.HttpMethod;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Random;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
@RunWith(VertxUnitRunner.class)
public class RetryingWebClientTest {
private Vertx vertx;
@Before
public void setup(TestContext ctx) {
vertx = Vertx.vertx();
Random rand = new Random();
vertx.createHttpServer()
.requestHandler(req -> {
try {
String subPath = req.path().substring(1);
if (subPath.startsWith("random")) {
// random/500_404_200
String[] statusCodes = subPath.split("/")[1].split("_");
// pick a random code and respond with it
int statusCode = Integer.valueOf(statusCodes[rand.nextInt(statusCodes.length)]);
req.response().setStatusCode(statusCode).end();
} else if (subPath.startsWith("delayed")) {
vertx.setTimer(1000, id -> {
try {
req.response().setStatusCode(200).end();
}
catch (Exception ex) {}
});
} else {
int statusCode = Integer.valueOf(subPath);
req.response().setStatusCode(statusCode).end();
}
} catch (Exception ex) {
req.response().setStatusCode(500).end();
}
})
.listen(18082, ctx.asyncAssertSuccess());
}
@After
public void tearDown(TestContext ctx) {
vertx.close(ctx.asyncAssertSuccess());
}
@Test
public void get_expectSuccess(TestContext ctx) {
expectSuccess(ctx, HttpMethod.POST);
}
@Test
public void post_expectSuccess(TestContext ctx) {
expectSuccess(ctx, HttpMethod.POST);
}
private void expectSuccess(TestContext ctx, HttpMethod testMethod) {
RetryingWebClient c = new RetryingWebClient(vertx, "http://localhost:18082/200", testMethod, 0, 0);
c.send((URI uri, HttpMethod method) -> {
return HttpRequest.newBuilder().uri(uri).method(method.toString(), HttpRequest.BodyPublishers.noBody()).build();
}, resp -> {
ctx.assertEquals(200, resp.statusCode());
return 200 == resp.statusCode();
}).onComplete(ctx.asyncAssertSuccess());
}
@Test
public void get_expectRetryFailure_zeroBackoff(TestContext ctx) {
expectRetryFailure_zeroBackoff(ctx, HttpMethod.GET);
}
@Test
public void post_expectRetryFailure_zeroBackoff(TestContext ctx) {
expectRetryFailure_zeroBackoff(ctx, HttpMethod.POST);
}
private void expectRetryFailure_zeroBackoff(TestContext ctx, HttpMethod testMethod) {
AtomicInteger totalAttempts = new AtomicInteger(0);
RetryingWebClient c = new RetryingWebClient(vertx, "http://localhost:18082/404", testMethod, 3, 0);
c.send((URI uri, HttpMethod method) -> {
return HttpRequest.newBuilder().uri(uri).method(method.toString(), HttpRequest.BodyPublishers.noBody()).build();
}, resp -> {
totalAttempts.incrementAndGet();
ctx.assertEquals(404, resp.statusCode());
// returning false for retry
return false;
}).onComplete(ctx.asyncAssertFailure(v -> ctx.assertTrue(v instanceof TooManyRetriesException)));
}
@Test
public void get_expectRetryFailure_withBackoff(TestContext ctx) {
expectRetryFailure_withBackoff(ctx, HttpMethod.GET);
}
@Test
public void post_expectRetryFailure_withBackoff(TestContext ctx) {
expectRetryFailure_withBackoff(ctx, HttpMethod.POST);
}
private void expectRetryFailure_withBackoff(TestContext ctx, HttpMethod testMethod) {
AtomicInteger totalAttempts = new AtomicInteger(0);
RetryingWebClient c = new RetryingWebClient(vertx, "http://localhost:18082/404", testMethod, 3, 1);
c.send((URI uri, HttpMethod method) -> {
return HttpRequest.newBuilder().uri(uri).method(method.toString(), HttpRequest.BodyPublishers.noBody()).build();
}, resp -> {
totalAttempts.incrementAndGet();
ctx.assertEquals(404, resp.statusCode());
// returning false for retry
return false;
}).onComplete(ctx.asyncAssertFailure(v -> {
ctx.assertEquals(4, (int) totalAttempts.get());
ctx.assertTrue(v instanceof TooManyRetriesException);
}));
}
@Test
public void get_expectSuccess_withRandomFailures(TestContext ctx) {
expectSuccess_withRandomFailures(ctx, HttpMethod.GET);
}
@Test
public void post_expectSuccess_withRandomFailures(TestContext ctx) {
expectSuccess_withRandomFailures(ctx, HttpMethod.POST);
}
private void expectSuccess_withRandomFailures(TestContext ctx, HttpMethod testMethod) {
for (int i = 0; i < 10; ++i) {
AtomicInteger totalAttempts = new AtomicInteger(0);
RetryingWebClient c = new RetryingWebClient(vertx, "http://localhost:18082/random/500_500_500_200",
testMethod, 100, 1);
c.send((URI uri, HttpMethod method) -> {
return HttpRequest.newBuilder().uri(uri).method(method.toString(), HttpRequest.BodyPublishers.noBody()).build();
}, resp -> {
totalAttempts.incrementAndGet();
return resp.statusCode() == 200;
}).onComplete(ctx.asyncAssertSuccess(v -> {
ctx.assertTrue(totalAttempts.get() >= 1);
ctx.assertTrue(totalAttempts.get() <= 101);
}));
}
}
@Test
public void get_expectImmediateFailure_withNonRetryErrors(TestContext ctx) {
expectImmediateFailure_withNonRetryErrors(ctx, HttpMethod.GET);
}
@Test
public void post_expectImmediateFailure_withNonRetryErrors(TestContext ctx) {
expectImmediateFailure_withNonRetryErrors(ctx, HttpMethod.POST);
}
private void expectImmediateFailure_withNonRetryErrors(TestContext ctx, HttpMethod testMethod) {
for (int i = 0; i < 10; ++i) {
AtomicInteger totalAttempts = new AtomicInteger(0);
RetryingWebClient c = new RetryingWebClient(vertx, "http://localhost:18082/404", testMethod, 100, 1);
c.send((URI uri, HttpMethod method) -> {
return HttpRequest.newBuilder().uri(uri).method(method.toString(), HttpRequest.BodyPublishers.noBody()).build();
}, resp -> {
totalAttempts.incrementAndGet();
if (resp.statusCode() == 200) return true;
else if (resp.statusCode() == 500) return false;
else throw new UnexpectedStatusCodeException(resp.statusCode());
}).onComplete(ctx.asyncAssertFailure(v -> {
// check that it only attempted once and failed
ctx.assertEquals(1, totalAttempts.get());
ctx.assertTrue(v instanceof UnexpectedStatusCodeException);
}));
}
}
public Function<HttpResponse, Boolean> assertStatusCodeFactory(TestContext ctx, int code) {
return result -> {
ctx.assertEquals(code, result.statusCode());
return code == result.statusCode();
};
}
public Handler<AsyncResult<Void>> ensureAsyncExceptionFactory(TestContext ctx, Class<? extends Exception> exceptionClass) {
return ctx.asyncAssertFailure(cause -> {
ctx.assertTrue(cause.getClass() == exceptionClass, "Expected a " + exceptionClass.toString() + " but got a " + cause);
});
}
@Test
public void longRequest_longerTimeout_expectSuccess(TestContext ctx) {
testDelayedResponse(ctx, assertStatusCodeFactory(ctx, 200), 1500)
.onComplete(ctx.asyncAssertSuccess());
}
@Test
public void longRequest_shorterTimeout_expectFailure(TestContext ctx) {
testDelayedResponse(ctx, req -> true, 500)
.onComplete(ensureAsyncExceptionFactory(ctx, TimeoutException.class));
}
private Future<Void> testDelayedResponse(TestContext ctx, Function<HttpResponse, Boolean> assertion, int resultTimeoutMs) {
Async async = ctx.async();
RetryingWebClient c = new RetryingWebClient(vertx, "http://localhost:18082/delayed", HttpMethod.GET, 0, 0, resultTimeoutMs);
return c.send((URI uri, HttpMethod method) -> HttpRequest.newBuilder().uri(uri).method(method.toString(), HttpRequest.BodyPublishers.noBody()).build(), assertion)
.andThen(r -> async.complete());
}
}