-
Notifications
You must be signed in to change notification settings - Fork 63
/
CircuitBreakerRetryTest.java
581 lines (520 loc) · 27.7 KB
/
CircuitBreakerRetryTest.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
*******************************************************************************
* Copyright (c) 2016-2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package org.eclipse.microprofile.fault.tolerance.tck;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.lessThan;
import static org.testng.Assert.fail;
import java.sql.Connection;
import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.microprofile.fault.tolerance.tck.circuitbreaker.clientserver.CircuitBreakerClassLevelClientWithRetry;
import org.eclipse.microprofile.fault.tolerance.tck.circuitbreaker.clientserver.CircuitBreakerClientWithRetry;
import org.eclipse.microprofile.fault.tolerance.tck.circuitbreaker.clientserver.CircuitBreakerClientWithRetryAsync;
import org.eclipse.microprofile.fault.tolerance.tck.config.ConfigAnnotationAsset;
import org.eclipse.microprofile.fault.tolerance.tck.util.DurationMatcher;
import org.eclipse.microprofile.fault.tolerance.tck.util.Exceptions;
import org.eclipse.microprofile.fault.tolerance.tck.util.Packages;
import org.eclipse.microprofile.fault.tolerance.tck.util.TCKConfig;
import org.eclipse.microprofile.fault.tolerance.tck.util.TestException;
import org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException;
import org.hamcrest.MatcherAssert;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.Test;
import jakarta.inject.Inject;
/**
* Test CircuitBreaker Thresholds and delays with Retries.
*
* @author <a href="mailto:neil_young@uk.ibm.com">Neil Young</a>
* @author <a href="mailto:anrouse@uk.ibm.com">Andrew Rouse</a>
*/
public class CircuitBreakerRetryTest extends Arquillian {
private @Inject CircuitBreakerClientWithRetry clientForCBWithRetry;
private @Inject CircuitBreakerClassLevelClientWithRetry clientForClassLevelCBWithRetry;
private @Inject CircuitBreakerClientWithRetryAsync clientForCBWithRetryAsync;
@Deployment
public static WebArchive deploy() {
ConfigAnnotationAsset config = new ConfigAnnotationAsset()
.autoscaleMethod(CircuitBreakerClientWithRetry.class, "serviceC")
.autoscaleMethod(CircuitBreakerClientWithRetry.class, "serviceWithRetryOnCbOpen")
.autoscaleMethod(CircuitBreakerClientWithRetry.class, "serviceWithRetryOnTimeout")
.autoscaleMethod(CircuitBreakerClientWithRetry.class, "serviceWithRetryFailOnCbOpen")
.autoscaleMethod(CircuitBreakerClientWithRetryAsync.class, "serviceC")
.autoscaleMethod(CircuitBreakerClientWithRetryAsync.class, "serviceWithRetryOnCbOpen")
.autoscaleMethod(CircuitBreakerClientWithRetryAsync.class, "serviceWithRetryOnTimeout")
.autoscaleMethod(CircuitBreakerClientWithRetryAsync.class, "serviceWithRetryFailOnCbOpen");
JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "ftCircuitBreakerRetry.jar")
.addClasses(CircuitBreakerClientWithRetry.class,
CircuitBreakerClassLevelClientWithRetry.class,
CircuitBreakerClientWithRetryAsync.class,
TestException.class)
.addPackage(Packages.UTILS)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsManifestResource(config, "microprofile-config.properties")
.as(JavaArchive.class);
WebArchive war = ShrinkWrap.create(WebArchive.class, "ftCircuitBreakerRetry.war")
.addAsLibrary(testJar);
return war;
}
/**
* A test to exercise Circuit Breaker thresholds with sufficient retries to open the Circuit and result in a
* CircuitBreakerOpenException.
*/
@Test
public void testCircuitOpenWithMoreRetries() {
int invokeCounter = 0;
try {
clientForCBWithRetry.serviceA();
// serviceA should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
// should be thrown. Assert if this does not happen.
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceA();
Assert.fail("serviceA should retry in testCircuitOpenWithMoreRetries on iteration "
+ invokeCounter);
} catch (CircuitBreakerOpenException cboe) {
// Expected on iteration 4
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceA();
if (invokeCounter < 4) {
Assert.fail("serviceA should retry in testCircuitOpenWithMoreRetries on iteration "
+ invokeCounter);
}
} catch (Exception ex) {
// Not Expected
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceA();
Assert.fail(
"serviceA should retry or throw a CircuitBreakerOpenException in testCircuitOpenWithMoreRetries on iteration "
+ invokeCounter);
}
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceA();
Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
}
/**
* A test to exercise Circuit Breaker thresholds with insufficient retries to open the Circuit so that the Circuit
* remains closed and a TestException is caught.
*/
@Test
public void testCircuitOpenWithFewRetries() {
int invokeCounter = 0;
try {
clientForCBWithRetry.serviceB();
// serviceB should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
// should be thrown. Assert if this does not happen.
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
Assert.fail("serviceB should retry in testCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
} catch (CircuitBreakerOpenException cboe) {
// Not Expected
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
Assert.fail(
"serviceB should retry or throw a TestException (not a CBOE) in testCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
} catch (TestException ex) {
// Expected on iteration 3
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
if (invokeCounter < 3) {
Assert.fail("serviceB should retry in testCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
}
} catch (Exception ex) {
// Not Expected
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
Assert.fail(
"serviceB should retry or throw a TestException in testCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
}
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
Assert.assertEquals(invokeCounter, 3, "The number of executions should be 3");
}
/**
* Analogous to testCircuitOpenWithMoreRetries with Class level @CircuitBreaker and @Retry annotations that are
* inherited by serviceA
*/
@Test
public void testClassLevelCircuitOpenWithMoreRetries() {
int invokeCounter = 0;
try {
clientForClassLevelCBWithRetry.serviceA();
// serviceA should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
// should be thrown. Assert if this does not happen.
invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceA();
Assert.fail("serviceA should retry in testClassLevelCircuitOpenWithMoreRetries on iteration "
+ invokeCounter);
} catch (CircuitBreakerOpenException cboe) {
// Expected on iteration 4
invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceA();
if (invokeCounter < 4) {
Assert.fail("serviceA should retry in testClassLevelCircuitOpenWithMoreRetries on iteration "
+ invokeCounter);
}
} catch (Exception ex) {
// Not Expected
invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceA();
Assert.fail(
"serviceA should retry or throw a CircuitBreakerOpenException in testClassLevelCircuitOpenWithMoreRetries on iteration "
+ invokeCounter);
}
invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceA();
Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
}
/**
* Analogous to testCircuitOpenWithFewRetries with Class level @CircuitBreaker and @Retry annotations that are
* overridden by serviceB.
*/
@Test
public void testClassLevelCircuitOpenWithFewRetries() {
int invokeCounter = 0;
try {
clientForClassLevelCBWithRetry.serviceB();
// serviceB should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
// should be thrown. Assert if this does not happen.
invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
Assert.fail("serviceB should retry in testClassLevelCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
} catch (CircuitBreakerOpenException cboe) {
// Not Expected
invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
Assert.fail(
"serviceB should retry or throw a TestException (not a CBOE) in testClassLevelCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
} catch (TestException ex) {
// Expected on iteration 3
invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
if (invokeCounter < 3) {
Assert.fail("serviceB should retry in testClassLevelCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
}
} catch (Exception ex) {
// Not Expected
invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
Assert.fail(
"serviceB should retry or throw a TestException in testClassLevelCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
}
invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
Assert.assertEquals(invokeCounter, 3, "The number of executions should be 3");
}
/**
* Analogous to testCircuitOpenWithMoreRetries but execution failures are caused by timeouts.
*/
@Test
public void testCircuitOpenWithMultiTimeouts() {
int invokeCounter = 0;
try {
clientForCBWithRetry.serviceC();
// serviceC should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
// should be thrown. Assert if this does not happen.
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceC();
Assert.fail("serviceC should retry in testCircuitOpenWithMultiTimeouts on iteration "
+ invokeCounter);
} catch (CircuitBreakerOpenException cboe) {
// Expected on iteration 4
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceC();
if (invokeCounter < 4) {
Assert.fail("serviceC should retry in testCircuitOpenWithMultiTimeouts on iteration "
+ invokeCounter);
}
} catch (Exception ex) {
// Not Expected
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceC();
Assert.fail(
"serviceC should retry or throw a CircuitBreakerOpenException in testCircuitOpenWithMultiTimeouts on iteration "
+ invokeCounter + ", caught exception: " + ex);
}
invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceC();
Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
}
/**
* Test that we retry around an open circuit breaker
* <p>
* Test that when retries are configured with sufficient delay, a call to an open circuit can retry until the
* circuit half-closes, allowing the call to succeed.
*/
@Test
public void testRetriesSucceedWhenCircuitCloses() {
// Open the circuit by submitting four failures
for (int i = 0; i < 4; i++) {
try {
clientForCBWithRetry.serviceWithRetryOnCbOpen(true);
} catch (TestException e) {
// Expected
}
}
// Circuit is now open for 1 second
// Our next call should cause a CircuitBreakerOpenException which should then be retried
// The retry delay is 100ms, maxRetries is 20 so we expect a successful call after around 1 second when the
// circuit closes
long startTime = System.nanoTime();
clientForCBWithRetry.serviceWithRetryOnCbOpen(false);
long endTime = System.nanoTime();
// Check that the call took the expected time
// Allow a margin of 250ms since there's a retry delay of 100ms
MatcherAssert.assertThat("Call was successful but did not take the expected time",
Duration.ofNanos(endTime - startTime),
DurationMatcher.closeTo(TCKConfig.getConfig().getTimeoutInDuration(1000),
TCKConfig.getConfig().getTimeoutInDuration(250)));
}
/**
* Test that we don't retry around an open circuit breaker if CircuitBreakerOpenException is not included in the
* retryOn attribute of the Retry annotation
* <p>
* This test calls a method which only retries on TimeoutException
*/
public void testNoRetriesIfNotRetryOn() {
// Open the circuit by submitting four failures
for (int i = 0; i < 4; i++) {
try {
clientForCBWithRetry.serviceWithRetryOnTimeout(true);
} catch (TestException e) {
// Expected
}
}
// Circuit is now open for 1 second
// Our next call should cause a CircuitBreakerOpenException which should not be retried
// We expect a response almost immediately
long startTime = System.nanoTime();
Exceptions.expectCbOpen(() -> clientForCBWithRetry.serviceWithRetryOnTimeout(false));
long endTime = System.nanoTime();
// Check that the call took less than 200ms (i.e. we didn't delay and retry)
MatcherAssert.assertThat("Call was successful but did not take the expected time",
Duration.ofNanos(endTime - startTime),
lessThan(TCKConfig.getConfig().getTimeoutInDuration(200)));
}
/**
* Test that we don't retry around an open circuit breaker if CircuitBreakerOpenException is included in the abortOn
* attribute of the Retry annotation
*/
public void testNoRetriesIfAbortOn() {
// Open the circuit by submitting four failures
for (int i = 0; i < 4; i++) {
try {
clientForCBWithRetry.serviceWithRetryFailOnCbOpen(true);
} catch (TestException e) {
// Expected
}
}
// Circuit is now open for 1 second
// Our next call should cause a CircuitBreakerOpenException which should not be retried
// We expect a response almost immediately
long startTime = System.nanoTime();
Exceptions.expectCbOpen(() -> clientForCBWithRetry.serviceWithRetryFailOnCbOpen(false));
long endTime = System.nanoTime();
// Check that the call took less than 200ms (i.e. we didn't delay and retry)
MatcherAssert.assertThat("Call was successful but did not take the expected time",
Duration.ofNanos(endTime - startTime),
lessThan(TCKConfig.getConfig().getTimeoutInDuration(200)));
}
/**
* A test to exercise Circuit Breaker thresholds with sufficient retries to open the Circuit and result in a
* CircuitBreakerOpenException using an Asynchronous call.
*/
@Test
public void testCircuitOpenWithMoreRetriesAsync() {
int invokeCounter = 0;
Future<Connection> result = clientForCBWithRetryAsync.serviceA();
try {
result.get(TCKConfig.getConfig().getTimeoutInMillis(5000), TimeUnit.MILLISECONDS);
// serviceA should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
// should be thrown. Assert if this does not happen.
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceA();
Assert.fail("serviceA should retry in testCircuitOpenWithMoreRetries on iteration "
+ invokeCounter);
} catch (ExecutionException executionException) {
// Expected execution exception wrapping CircuitBreakerOpenException
MatcherAssert.assertThat("Thrown exception is the wrong type",
executionException.getCause(),
instanceOf(CircuitBreakerOpenException.class));
// Expected on iteration 4
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceA();
if (invokeCounter < 4) {
Assert.fail("serviceA should retry in testCircuitOpenWithMoreRetries on iteration "
+ invokeCounter);
}
} catch (Exception ex) {
// Not Expected
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceA();
Assert.fail(
"serviceA should retry or throw a CircuitBreakerOpenException in testCircuitOpenWithMoreRetries on iteration "
+ invokeCounter);
}
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceA();
Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
}
/**
* A test to exercise Circuit Breaker thresholds with insufficient retries to open the Circuit so that the Circuit
* remains closed and a TestException is caught when using an Asynchronous call.
*/
@Test
public void testCircuitOpenWithFewRetriesAsync() {
int invokeCounter = 0;
Future<Connection> result = clientForCBWithRetryAsync.serviceB();
try {
result.get(TCKConfig.getConfig().getTimeoutInMillis(5000), TimeUnit.MILLISECONDS);
// serviceB should retry until the Retry maxRetries is reached. At that point a TestException
// should be thrown. Assert if this does not happen.
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceB();
Assert.fail("serviceB should retry in testCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
} catch (ExecutionException ex) {
// Expected execution exception wrapping TestException
MatcherAssert.assertThat("Thrown exception is the wrong type",
ex.getCause(),
instanceOf(TestException.class));
// Expected on iteration 3
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceB();
if (invokeCounter < 3) {
Assert.fail("serviceB should retry in testCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
}
} catch (Exception ex) {
// Not Expected
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceB();
Assert.fail(
"serviceB should retry or throw a TestException in testCircuitOpenWithFewRetries on iteration "
+ invokeCounter);
}
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceB();
Assert.assertEquals(invokeCounter, 3, "The number of executions should be 3");
}
/**
* Analogous to testCircuitOpenWithMoreRetriesAsync but execution failures are caused by timeouts.
*/
@Test
public void testCircuitOpenWithMultiTimeoutsAsync() {
int invokeCounter = 0;
Future<Connection> result = clientForCBWithRetryAsync.serviceC();
try {
result.get(TCKConfig.getConfig().getTimeoutInMillis(10000), TimeUnit.MILLISECONDS); // Expected to finish
// after < 1 second
// serviceC should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
// should be thrown. Assert if this does not happen.
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceC();
Assert.fail("serviceC should retry in testCircuitOpenWithMultiTimeouts on iteration "
+ invokeCounter);
} catch (ExecutionException executionException) {
// Expected execution exception wrapping CircuitBreakerOpenException
MatcherAssert.assertThat("Thrown exception is the wrong type",
executionException.getCause(),
instanceOf(CircuitBreakerOpenException.class));
// Expected on iteration 4
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceC();
if (invokeCounter < 4) {
Assert.fail("serviceC should retry in testCircuitOpenWithMultiTimeouts on iteration "
+ invokeCounter);
}
} catch (Exception ex) {
// Not Expected
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceC();
Assert.fail(
"serviceC should retry or throw a CircuitBreakerOpenException in testCircuitOpenWithMultiTimeouts on iteration "
+ invokeCounter + ", caught exception: " + ex);
}
invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceC();
Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
}
/**
* Test that we retry around an open circuit breaker
* <p>
* Test that when retries are configured with sufficient delay, a call to an open circuit can retry until the
* circuit half-closes, allowing the call to succeed.
*/
@Test
public void testRetriesSucceedWhenCircuitClosesAsync() {
// Open the circuit by submitting four failures
for (int i = 0; i < 4; i++) {
Future<String> result = clientForCBWithRetryAsync.serviceWithRetryOnCbOpen(true);
Exceptions.expect(TestException.class, result);
}
// Circuit is now open for 1 second
// Our next call should cause a CircuitBreakerOpenException which should then be retried
// The retry delay is 100ms, maxRetries is 20 so we expect a successful call after around 1 second when the
// circuit closes
long startTime = System.nanoTime();
Future<String> result = clientForCBWithRetryAsync.serviceWithRetryOnCbOpen(false);
try {
result.get(TCKConfig.getConfig().getTimeoutInMillis(10000), TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
fail("Call to serviceWithRetryOnCbOpen did not succeed within 10 seconds");
} catch (ExecutionException e) {
fail("Call to serviceWithRetryOnCbOpen failed with exception", e);
} catch (InterruptedException e) {
fail("Call to serviceWithRetryOnCbOpen was interrupted", e);
}
long endTime = System.nanoTime();
// Check that the call took the expected time
// Allow a margin of 250ms since there's a retry delay of 100ms
MatcherAssert.assertThat("Call was successful but did not take the expected time",
Duration.ofNanos(endTime - startTime),
DurationMatcher.closeTo(TCKConfig.getConfig().getTimeoutInDuration(1000),
TCKConfig.getConfig().getTimeoutInDuration(250)));
}
/**
* Test that we don't retry around an open circuit breaker if CircuitBreakerOpenException is not included in the
* retryOn attribute of the Retry annotation
* <p>
* This test calls a method which only retries on TimeoutException
*/
@Test
public void testNoRetriesIfNotRetryOnAsync() {
// Open the circuit by submitting four failures
for (int i = 0; i < 4; i++) {
Future<String> result = clientForCBWithRetryAsync.serviceWithRetryOnTimeout(true);
Exceptions.expect(TestException.class, result);
}
// Circuit is now open for 1 second
// Our next call should cause a CircuitBreakerOpenException which should not be retried
long startTime = System.nanoTime();
Future<String> result = clientForCBWithRetryAsync.serviceWithRetryOnTimeout(false);
Exceptions.expect(CircuitBreakerOpenException.class, result);
long endTime = System.nanoTime();
// Check that the call took less than 200ms (i.e. we didn't delay and retry)
MatcherAssert.assertThat("Call was successful but did not take the expected time",
Duration.ofNanos(endTime - startTime),
lessThan(TCKConfig.getConfig().getTimeoutInDuration(200)));
}
/**
* Test that we don't retry around an open circuit breaker if CircuitBreakerOpenException is included in the abortOn
* attribute of the Retry annotation
*/
@Test
public void testNoRetriesIfAbortOnAsync() {
// Open the circuit by submitting four failures
for (int i = 0; i < 4; i++) {
Future<String> result = clientForCBWithRetryAsync.serviceWithRetryFailOnCbOpen(true);
Exceptions.expect(TestException.class, result);
}
// Circuit is now open for 1 second
// Our next call should cause a CircuitBreakerOpenException which should not be retried
long startTime = System.nanoTime();
Future<String> result = clientForCBWithRetryAsync.serviceWithRetryFailOnCbOpen(false);
Exceptions.expect(CircuitBreakerOpenException.class, result);
long endTime = System.nanoTime();
// Check that the call took less than 200ms (i.e. we didn't delay and retry)
MatcherAssert.assertThat("Call was successful but did not take the expected time",
Duration.ofNanos(endTime - startTime),
lessThan(TCKConfig.getConfig().getTimeoutInDuration(200)));
}
}