forked from googleapis/gax-java
/
ScheduledRetryingExecutorTest.java
337 lines (296 loc) · 13.7 KB
/
ScheduledRetryingExecutorTest.java
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright 2017 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.retrying;
import static com.google.api.gax.retrying.FailingCallable.FAST_RETRY_SETTINGS;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import com.google.api.core.ApiFuture;
import com.google.api.core.NanoClock;
import com.google.api.gax.retrying.FailingCallable.CustomException;
import com.google.api.gax.rpc.testing.FakeCallContext;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.threeten.bp.Duration;
// @RunWith(MockitoJUnitRunner.class)
public class ScheduledRetryingExecutorTest extends AbstractRetryingExecutorTest {
private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
// Number of test runs, essential for multithreaded tests.
private static final int EXECUTIONS_COUNT = 5;
@Override
protected RetryingExecutorWithContext<String> getExecutor(RetryAlgorithm<String> retryAlgorithm) {
return getRetryingExecutor(retryAlgorithm, scheduler);
}
@Override
protected RetryAlgorithm<String> getAlgorithm(
RetrySettings retrySettings, int apocalypseCountDown, RuntimeException apocalypseException) {
return new RetryAlgorithm<>(
new TestResultRetryAlgorithm<String>(apocalypseCountDown, apocalypseException),
new ExponentialRetryAlgorithm(retrySettings, NanoClock.getDefaultClock()));
}
private RetryingExecutorWithContext<String> getRetryingExecutor(
RetryAlgorithm<String> retryAlgorithm, ScheduledExecutorService scheduler) {
return new ScheduledRetryingExecutor<>(retryAlgorithm, scheduler);
}
@After
public void after() {
scheduler.shutdownNow();
}
@Test
public void testSuccessWithFailuresPeekAttempt() throws Exception {
for (int executionsCount = 0; executionsCount < EXECUTIONS_COUNT; executionsCount++) {
final int maxRetries = 100;
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
FailingCallable callable = new FailingCallable(15, "request", "SUCCESS", tracer);
RetrySettings retrySettings =
FAST_RETRY_SETTINGS
.toBuilder()
.setTotalTimeout(Duration.ofMillis(1000L))
.setMaxAttempts(maxRetries)
.build();
RetryingExecutorWithContext<String> executor =
getRetryingExecutor(getAlgorithm(retrySettings, 0, null), localExecutor);
RetryingFuture<String> future =
executor.createFuture(callable, FakeCallContext.createDefault().withTracer(tracer));
assertNull(future.peekAttemptResult());
assertSame(future.peekAttemptResult(), future.peekAttemptResult());
assertFalse(future.getAttemptResult().isDone());
assertFalse(future.getAttemptResult().isCancelled());
future.setAttemptFuture(executor.submit(future));
int failedAttempts = 0;
while (!future.isDone()) {
ApiFuture<String> attemptResult = future.peekAttemptResult();
if (attemptResult != null) {
assertTrue(attemptResult.isDone());
assertFalse(attemptResult.isCancelled());
try {
attemptResult.get();
} catch (ExecutionException e) {
if (e.getCause() instanceof CustomException) {
failedAttempts++;
}
}
}
Thread.sleep(0L, 100);
}
assertFutureSuccess(future);
assertEquals(15, future.getAttemptSettings().getAttemptCount());
assertTrue(failedAttempts > 0);
localExecutor.shutdownNow();
}
}
@Test
public void testSuccessWithFailuresGetAttempt() throws Exception {
for (int executionsCount = 0; executionsCount < EXECUTIONS_COUNT; executionsCount++) {
final int maxRetries = 100;
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
FailingCallable callable = new FailingCallable(15, "request", "SUCCESS", tracer);
RetrySettings retrySettings =
FAST_RETRY_SETTINGS
.toBuilder()
.setTotalTimeout(Duration.ofMillis(1000L))
.setMaxAttempts(maxRetries)
.build();
RetryingExecutorWithContext<String> executor =
getRetryingExecutor(getAlgorithm(retrySettings, 0, null), localExecutor);
RetryingFuture<String> future =
executor.createFuture(callable, FakeCallContext.createDefault().withTracer(tracer));
assertNull(future.peekAttemptResult());
assertSame(future.getAttemptResult(), future.getAttemptResult());
assertFalse(future.getAttemptResult().isDone());
assertFalse(future.getAttemptResult().isCancelled());
future.setAttemptFuture(executor.submit(future));
CustomException exception;
int checks = 0;
do {
exception = null;
checks++;
Future<String> attemptResult = future.getAttemptResult();
try {
// testing that the gotten attempt result is non-cancelable
assertFalse(attemptResult.cancel(false));
assertFalse(attemptResult.cancel(true));
attemptResult.get();
assertNotNull(future.peekAttemptResult());
} catch (ExecutionException e) {
exception = (CustomException) e.getCause();
}
assertTrue(attemptResult.isDone());
assertFalse(attemptResult.isCancelled());
} while (exception != null && checks < maxRetries + 1);
assertTrue(future.isDone());
assertFutureSuccess(future);
assertEquals(15, future.getAttemptSettings().getAttemptCount());
assertTrue("checks is equal to " + checks, checks > 1 && checks <= maxRetries);
localExecutor.shutdownNow();
}
}
@Test
public void testCancelGetAttempt() throws Exception {
for (int executionsCount = 0; executionsCount < EXECUTIONS_COUNT; executionsCount++) {
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
final int maxRetries = 100;
FailingCallable callable = new FailingCallable(maxRetries - 1, "request", "SUCCESS", tracer);
RetrySettings retrySettings =
FAST_RETRY_SETTINGS
.toBuilder()
.setTotalTimeout(Duration.ofMillis(1000L))
.setMaxAttempts(maxRetries)
.build();
RetryingExecutorWithContext<String> executor =
getRetryingExecutor(getAlgorithm(retrySettings, 0, null), localExecutor);
RetryingFuture<String> future = executor.createFuture(callable, retryingContext);
assertNull(future.peekAttemptResult());
assertSame(future.getAttemptResult(), future.getAttemptResult());
assertFalse(future.getAttemptResult().isDone());
assertFalse(future.getAttemptResult().isCancelled());
future.setAttemptFuture(executor.submit(future));
CustomException exception;
CancellationException cancellationException = null;
int checks = 0;
int failedCancelations = 0;
do {
exception = null;
checks++;
Future<String> attemptResult = future.getAttemptResult();
try {
attemptResult.get();
assertNotNull(future.peekAttemptResult());
} catch (CancellationException e) {
cancellationException = e;
} catch (ExecutionException e) {
exception = (CustomException) e.getCause();
}
assertTrue(attemptResult.isDone());
if (!future.cancel(true)) {
failedCancelations++;
}
} while (exception != null && checks < maxRetries);
assertTrue(future.isDone());
assertNotNull(cancellationException);
// future.cancel(true) may return false sometimes, which is ok. Also, the every cancellation
// of
// an already cancelled future should return false (this is what -1 means here)
assertEquals(2, checks - (failedCancelations - 1));
assertTrue(future.getAttemptSettings().getAttemptCount() > 0);
assertFutureCancel(future);
localExecutor.shutdownNow();
}
}
@Test
public void testCancelOuterFutureAfterStart() throws Exception {
for (int executionsCount = 0; executionsCount < EXECUTIONS_COUNT; executionsCount++) {
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
FailingCallable callable = new FailingCallable(4, "requset", "SUCCESS", tracer);
RetrySettings retrySettings =
FAST_RETRY_SETTINGS
.toBuilder()
.setInitialRetryDelay(Duration.ofMillis(1_000L))
.setMaxRetryDelay(Duration.ofMillis(1_000L))
.setTotalTimeout(Duration.ofMillis(10_0000L))
.build();
RetryingExecutorWithContext<String> executor =
getRetryingExecutor(getAlgorithm(retrySettings, 0, null), localExecutor);
RetryingFuture<String> future =
executor.createFuture(callable, FakeCallContext.createDefault().withTracer(tracer));
future.setAttemptFuture(executor.submit(future));
Thread.sleep(30L);
boolean res = future.cancel(false);
assertTrue(res);
assertFutureCancel(future);
assertTrue(future.getAttemptSettings().getAttemptCount() < 4);
localExecutor.shutdownNow();
}
}
@Test
public void testCancelIsTraced() throws Exception {
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
FailingCallable callable = new FailingCallable(4, "request", "SUCCESS", tracer);
RetrySettings retrySettings =
FAST_RETRY_SETTINGS
.toBuilder()
.setInitialRetryDelay(Duration.ofMillis(1_000L))
.setMaxRetryDelay(Duration.ofMillis(1_000L))
.setTotalTimeout(Duration.ofMillis(10_0000L))
.build();
RetryingExecutorWithContext<String> executor =
getRetryingExecutor(getAlgorithm(retrySettings, 0, null), localExecutor);
RetryingFuture<String> future =
executor.createFuture(callable, FakeCallContext.createDefault().withTracer(tracer));
future.setAttemptFuture(executor.submit(future));
Thread.sleep(30L);
boolean res = future.cancel(false);
assertTrue(res);
assertFutureCancel(future);
Mockito.verify(tracer).attemptCancelled();
localExecutor.shutdownNow();
}
@Test
public void testCancelProxiedFutureAfterStart() throws Exception {
// this is a heavy test, which takes a lot of time, so only few executions.
for (int executionsCount = 0; executionsCount < 2; executionsCount++) {
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
FailingCallable callable = new FailingCallable(5, "request", "SUCCESS", tracer);
RetrySettings retrySettings =
FAST_RETRY_SETTINGS
.toBuilder()
.setInitialRetryDelay(Duration.ofMillis(1_000L))
.setMaxRetryDelay(Duration.ofMillis(1_000L))
.setTotalTimeout(Duration.ofMillis(10_0000L))
.build();
RetryingExecutorWithContext<String> executor =
getRetryingExecutor(getAlgorithm(retrySettings, 0, null), localExecutor);
RetryingFuture<String> future =
executor.createFuture(callable, FakeCallContext.createDefault().withTracer(tracer));
future.setAttemptFuture(executor.submit(future));
Thread.sleep(50L);
// Note that shutdownNow() will not cancel internal FutureTasks automatically, which
// may potentially cause another thread handing on RetryingFuture#get() call forever.
// Canceling the tasks returned by shutdownNow() also does not help, because of missing
// feature
// in guava's ListenableScheduledFuture, which does not cancel itself, when its delegate is
// canceled.
// So only the graceful shutdown() is supported properly.
localExecutor.shutdown();
assertFutureFail(future, RejectedExecutionException.class);
localExecutor.shutdownNow();
}
}
}