forked from spring-projects/spring-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnableRetryTests.java
919 lines (729 loc) · 21.4 KB
/
EnableRetryTests.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/*
* Copyright 2014-2022 the original author or authors.
*
* 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
*
* https://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.springframework.retry.annotation;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Properties;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.Ordered;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryListener;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.backoff.Sleeper;
import org.springframework.retry.interceptor.RetryInterceptorBuilder;
import org.springframework.retry.listener.RetryListenerSupport;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
* @author Aldo Sinanaj
* @since 1.1
*/
public class EnableRetryTests {
@Test
public void vanilla() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
Service service = context.getBean(Service.class);
Foo foo = context.getBean(Foo.class);
assertFalse(AopUtils.isAopProxy(foo));
assertTrue(AopUtils.isAopProxy(service));
service.service();
assertEquals(3, service.getCount());
TestConfiguration config = context.getBean(TestConfiguration.class);
assertTrue(config.listener1);
assertTrue(config.listener2);
assertTrue(config.twoFirst);
context.close();
}
@Test
public void multipleMethods() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
MultiService service = context.getBean(MultiService.class);
service.service();
assertEquals(3, service.getCount());
service.other();
assertEquals(4, service.getCount());
context.close();
}
@Test
public void proxyTargetClass() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
TestProxyConfiguration.class);
Service service = context.getBean(Service.class);
assertTrue(AopUtils.isCglibProxy(service));
RecoverableService recoverable = context.getBean(RecoverableService.class);
recoverable.service();
assertTrue(recoverable.isOtherAdviceCalled());
context.close();
}
@Test
public void marker() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
Service service = context.getBean(Service.class);
assertTrue(AopUtils.isCglibProxy(service));
assertTrue(service instanceof org.springframework.retry.interceptor.Retryable);
context.close();
}
@Test
public void recovery() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
RecoverableService service = context.getBean(RecoverableService.class);
service.service();
assertEquals(3, service.getCount());
assertNotNull(service.getCause());
context.close();
}
@Test
public void type() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
RetryableService service = context.getBean(RetryableService.class);
service.service();
assertEquals(3, service.getCount());
context.close();
}
@Test
public void excludes() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
ExcludesService service = context.getBean(ExcludesService.class);
try {
service.service();
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
}
assertEquals(1, service.getCount());
context.close();
}
@Test
public void excludesOnly() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
ExcludesOnlyService service = context.getBean(ExcludesOnlyService.class);
service.setExceptionToThrow(new IllegalStateException());
try {
service.service();
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
}
assertEquals(1, service.getCount());
service.setExceptionToThrow(new IllegalArgumentException());
service.service();
assertEquals(3, service.getCount());
context.close();
}
@Test
public void stateful() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
StatefulService service = context.getBean(StatefulService.class);
for (int i = 0; i < 3; i++) {
try {
service.service(1);
}
catch (Exception e) {
assertEquals("Planned", e.getMessage());
}
}
assertEquals(3, service.getCount());
context.close();
}
@Test
public void testExternalInterceptor() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
InterceptableService service = context.getBean(InterceptableService.class);
service.service();
assertEquals(5, service.getCount());
context.close();
}
@Test
public void testInterface() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
TheInterface service = context.getBean(TheInterface.class);
service.service1();
service.service2();
assertEquals(4, service.getCount());
service.service3();
assertTrue(service.isRecovered());
context.close();
}
@Test
public void testInterfaceWithNoRecover() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
NoRecoverInterface service = context.getBean(NoRecoverInterface.class);
service.service();
assertTrue(service.isRecovered());
}
@Test
public void testImplementation() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
NotAnnotatedInterface service = context.getBean(NotAnnotatedInterface.class);
service.service1();
service.service2();
assertEquals(5, service.getCount());
context.close();
}
@Test
public void testExpression() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
ExpressionService service = context.getBean(ExpressionService.class);
service.service1();
assertEquals(3, service.getCount());
try {
service.service2();
fail("expected exception");
}
catch (RuntimeException e) {
assertEquals("this cannot be retried", e.getMessage());
}
assertEquals(4, service.getCount());
service.service3();
assertEquals(9, service.getCount());
RetryConfiguration config = context.getBean(RetryConfiguration.class);
AnnotationAwareRetryOperationsInterceptor advice = (AnnotationAwareRetryOperationsInterceptor) new DirectFieldAccessor(
config).getPropertyValue("advice");
@SuppressWarnings("unchecked")
Map<Object, Map<Method, MethodInterceptor>> delegates = (Map<Object, Map<Method, MethodInterceptor>>) new DirectFieldAccessor(
advice).getPropertyValue("delegates");
MethodInterceptor interceptor = delegates.get(target(service))
.get(ExpressionService.class.getDeclaredMethod("service3"));
RetryTemplate template = (RetryTemplate) new DirectFieldAccessor(interceptor)
.getPropertyValue("retryOperations");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
ExponentialBackOffPolicy backOff = (ExponentialBackOffPolicy) templateAccessor
.getPropertyValue("backOffPolicy");
assertEquals(1, backOff.getInitialInterval());
assertEquals(5, backOff.getMaxInterval());
assertEquals(1.1, backOff.getMultiplier(), 0.1);
SimpleRetryPolicy retryPolicy = (SimpleRetryPolicy) templateAccessor.getPropertyValue("retryPolicy");
assertEquals(5, retryPolicy.getMaxAttempts());
service.service4();
assertEquals(11, service.getCount());
service.service5();
assertEquals(12, service.getCount());
context.close();
}
@Test
public void rethrow() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
RethrowService service = context.getBean(RethrowService.class);
for (int i = 0; i < 3; i++) {
try {
service.service();
}
catch (RuntimeException e) {
assertEquals("Planned", e.getMessage());
}
}
assertEquals(3, service.getCount());
context.close();
}
@Test
public void rethrowExceptions() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
RethrowExceptionsService service = context.getBean(RethrowExceptionsService.class);
for (int i = 0; i < 3; i++) {
try {
service.service();
}
catch (RuntimeException e) {
assertEquals("Planned", e.getMessage());
}
}
assertEquals(3, service.getCount());
context.close();
}
private Object target(Object target) {
if (!AopUtils.isAopProxy(target)) {
return target;
}
try {
return target(((Advised) target).getTargetSource().getTarget());
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
@Configuration
@EnableRetry(proxyTargetClass = true)
protected static class TestProxyConfiguration {
@Bean
public Service service() {
return new Service();
}
@Bean
public RecoverableService recoverable() {
return new RecoverableService();
}
@Bean
public static AdviceBPP bpp() {
return new AdviceBPP();
}
static class AdviceBPP implements BeanPostProcessor, Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
if (bean instanceof RecoverableService) {
Advised advised = (Advised) bean;
advised.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if (invocation.getMethod().getName().equals("recover")) {
((RecoverableService) bean).setOtherAdviceCalled();
}
return invocation.proceed();
}
});
return bean;
}
return bean;
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
}
}
@Configuration
@EnableRetry
protected static class TestConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer pspc() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
Properties properties = new Properties();
properties.setProperty("one", "1");
properties.setProperty("five", "5");
properties.setProperty("onePointOne", "1.1");
properties.setProperty("retryMethod", "shouldRetry");
pspc.setProperties(properties);
return pspc;
}
boolean listener1;
boolean listener2;
protected boolean twoFirst;
@SuppressWarnings("serial")
@Bean
public Sleeper sleeper() {
return new Sleeper() {
@Override
public void sleep(long period) throws InterruptedException {
}
};
}
@Bean
public Service service() {
return new Service();
}
@Bean
public RetryListener listener1() {
return new OrderedListener() {
@Override
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
TestConfiguration.this.listener1 = true;
TestConfiguration.this.twoFirst = true;
return super.open(context, callback);
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
};
}
@Bean
public RetryListener listener2() {
return new OrderedListener() {
private boolean listener1;
@Override
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
TestConfiguration.this.listener2 = true;
TestConfiguration.this.twoFirst = false;
return super.open(context, callback);
}
@Override
public int getOrder() {
return Integer.MIN_VALUE;
}
};
}
@Bean
public MultiService multiService() {
return new MultiService();
}
@Bean
public RecoverableService recoverable() {
return new RecoverableService();
}
@Bean
public RetryableService retryable() {
return new RetryableService();
}
@Bean
public StatefulService stateful() {
return new StatefulService();
}
@Bean
public ExcludesService excludes() {
return new ExcludesService();
}
@Bean
public ExcludesOnlyService excludesOnly() {
return new ExcludesOnlyService();
}
@Bean
public RethrowService rethrowService() {
return new RethrowService();
}
@Bean
public RethrowExceptionsService rethrowExceptionsService() {
return new RethrowExceptionsService();
}
@Bean
public MethodInterceptor retryInterceptor() {
return RetryInterceptorBuilder.stateless().maxAttempts(5).build();
}
@Bean
public InterceptableService serviceWithExternalInterceptor() {
return new InterceptableService();
}
@Bean
public ExpressionService expressionService() {
return new ExpressionService();
}
@Bean
public ExceptionChecker exceptionChecker() {
return new ExceptionChecker();
}
@Bean
public Integer integerFiveBean() {
return Integer.valueOf(5);
}
@Bean
public Foo foo() {
return new Foo();
}
@Bean
public TheInterface anInterface() {
return new TheClass();
}
@Bean
public NoRecoverInterface anInterfaceWithNoRecover() {
return new NoRecoverClass();
}
@Bean
public NotAnnotatedInterface notAnnotatedInterface() {
return new RetryableImplementation();
}
}
protected static class Service {
private int count = 0;
@Retryable(RuntimeException.class)
public void service() {
if (this.count++ < 2) {
throw new RuntimeException("Planned");
}
}
public int getCount() {
return this.count;
}
}
protected static class MultiService {
private int count = 0;
@Retryable(RuntimeException.class)
public void service() {
if (this.count++ < 2) {
throw new RuntimeException("Planned");
}
}
@Retryable(RuntimeException.class)
public void other() {
if (this.count++ < 3) {
throw new RuntimeException("Other");
}
}
public int getCount() {
return this.count;
}
}
protected static class RecoverableService {
private int count = 0;
private Throwable cause;
boolean otherAdviceCalled;
@Retryable(RuntimeException.class)
public void service() {
this.count++;
throw new RuntimeException("Planned");
}
@Recover
public void recover(Throwable cause) {
this.cause = cause;
}
public Throwable getCause() {
return this.cause;
}
public int getCount() {
return this.count;
}
public void setOtherAdviceCalled() {
this.otherAdviceCalled = true;
}
public boolean isOtherAdviceCalled() {
return this.otherAdviceCalled;
}
}
@Retryable(RuntimeException.class)
protected static class RetryableService {
private int count = 0;
public void service() {
if (this.count++ < 2) {
throw new RuntimeException("Planned");
}
}
public int getCount() {
return this.count;
}
}
protected static class ExcludesService {
private int count = 0;
@Retryable(include = RuntimeException.class, exclude = IllegalStateException.class)
public void service() {
if (this.count++ < 2) {
throw new IllegalStateException("Planned");
}
}
public int getCount() {
return this.count;
}
}
protected static class ExcludesOnlyService {
private int count = 0;
private RuntimeException exceptionToThrow;
@Retryable(exclude = IllegalStateException.class)
public void service() {
if (this.count++ < 2) {
throw this.exceptionToThrow;
}
}
public int getCount() {
return this.count;
}
public void setExceptionToThrow(RuntimeException exceptionToThrow) {
this.exceptionToThrow = exceptionToThrow;
}
}
protected static class StatefulService {
private int count = 0;
@Retryable(stateful = true)
public void service(int value) {
if (this.count++ < 2) {
throw new RuntimeException("Planned");
}
}
public int getCount() {
return this.count;
}
}
private static class InterceptableService {
private int count = 0;
@Retryable(interceptor = "retryInterceptor")
public void service() {
if (this.count++ < 4) {
throw new RuntimeException("Planned");
}
}
public int getCount() {
return this.count;
}
}
private static class ExpressionService {
private int count = 0;
@Retryable(exceptionExpression = "message.contains('this can be retried')")
public void service1() {
if (this.count++ < 2) {
throw new RuntimeException("this can be retried");
}
}
@Retryable(exceptionExpression = "message.contains('this can be retried')")
public void service2() {
this.count++;
throw new RuntimeException("this cannot be retried");
}
@Retryable(exceptionExpression = "@exceptionChecker.${retryMethod}(#root)",
maxAttemptsExpression = "@integerFiveBean", backoff = @Backoff(delayExpression = "${one}",
maxDelayExpression = "@integerFiveBean", multiplierExpression = "${onePointOne}"))
public void service3() {
if (this.count++ < 8) {
throw new RuntimeException();
}
}
@Retryable(exceptionExpression = "message.contains('this can be retried')")
public void service4() {
if (this.count++ < 10) {
throw new RuntimeException("this can be retried");
}
}
@Retryable(exceptionExpression = "message.contains('this can be retried')", include = RuntimeException.class)
public void service5() {
if (this.count++ < 11) {
throw new RuntimeException("this can be retried");
}
}
public int getCount() {
return this.count;
}
}
private static class RethrowService {
private int count = 0;
@Retryable(include = IllegalArgumentException.class, rethrow = true)
public void service() {
if (this.count++ < 2) {
throw new RuntimeException("Planned");
}
}
public int getCount() {
return this.count;
}
}
private static class RethrowExceptionsService {
private int count = 0;
@Retryable(include = IllegalArgumentException.class, rethrowExceptions = RuntimeException.class)
public void service() {
if (this.count++ < 2) {
throw new RuntimeException("Planned");
}
}
public int getCount() {
return this.count;
}
}
public static class ExceptionChecker {
public boolean shouldRetry(Throwable t) {
return true;
}
}
private static class Foo {
}
public static interface TheInterface {
void service1();
@Retryable
void service2();
int getCount();
void service3();
void recover(Exception e);
boolean isRecovered();
}
public static class TheClass implements TheInterface {
private int count = 0;
private boolean recovered;
@Override
@Retryable
public void service1() {
if (this.count++ < 1) {
throw new RuntimeException("Planned");
}
}
@Override
public void service2() {
if (this.count++ < 3) {
throw new RuntimeException("Planned");
}
}
@Override
public int getCount() {
return this.count;
}
@Override
@Retryable
public void service3() {
throw new RuntimeException("planned");
}
@Override
@Recover
public void recover(Exception e) {
this.recovered = true;
}
@Override
public boolean isRecovered() {
return this.recovered;
}
}
public static interface NoRecoverInterface {
void service();
boolean isRecovered();
}
public static class NoRecoverClass implements NoRecoverInterface {
private boolean recovered;
@Override
@Retryable
public void service() {
throw new RuntimeException("Planned");
}
@Recover
public void recover(Exception e) {
this.recovered = true;
}
@Override
public boolean isRecovered() {
return this.recovered;
}
}
public static interface NotAnnotatedInterface {
void service1();
void service2();
int getCount();
}
@Retryable
public static class RetryableImplementation implements NotAnnotatedInterface {
private int count = 0;
@Override
public void service1() {
if (this.count++ < 2) {
throw new RuntimeException("Planned");
}
}
@Override
public void service2() {
if (this.count++ < 4) {
throw new RuntimeException("Planned");
}
}
@Override
public int getCount() {
return this.count;
}
}
public abstract static class OrderedListener extends RetryListenerSupport implements Ordered {
}
}