Skip to content

Commit 9ab6874

Browse files
author
Robert Winkler
committed
Fixed Issue ReactiveX#2
1 parent df2941e commit 9ab6874

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

src/main/java/io/github/robwin/retry/Retry.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,20 @@ public interface Retry {
3737
*/
3838
void handleRuntimeException(RuntimeException runtimeException);
3939

40+
/**
41+
* Creates a RetryContext.Builder to configure a custom Retry.
42+
*
43+
* @return a RetryContext.Builder
44+
*/
4045
static RetryContext.Builder custom(){
4146
return new RetryContext.Builder();
4247
}
4348

49+
/**
50+
* Creates a Retry with default configuration.
51+
*
52+
* @return a Retry with default configuration
53+
*/
4454
static Retry ofDefaults(){
4555
return Retry.custom().build();
4656
}
@@ -57,10 +67,11 @@ static <T> Try.CheckedSupplier<T> retryableCheckedSupplier(Try.CheckedSupplier<T
5767
};
5868
}
5969

60-
static <T> Try.CheckedRunnable retryableCheckedRunnable(Try.CheckedRunnable runnable, Retry retryContext){
70+
static Try.CheckedRunnable retryableCheckedRunnable(Try.CheckedRunnable runnable, Retry retryContext){
6171
return () -> {
6272
do try {
6373
runnable.run();
74+
break;
6475
} catch (Exception exception) {
6576
retryContext.handleException(exception);
6677
} while (retryContext.isRetryAllowedAfterException());
@@ -91,10 +102,11 @@ static <T> Supplier<T> retryableSupplier(Supplier<T> supplier, Retry retryContex
91102
};
92103
}
93104

94-
static <T> Runnable retryableRunnable(Runnable runnable, Retry retryContext){
105+
static Runnable retryableRunnable(Runnable runnable, Retry retryContext){
95106
return () -> {
96107
do try {
97108
runnable.run();
109+
break;
98110
} catch (RuntimeException runtimeException) {
99111
retryContext.handleRuntimeException(runtimeException);
100112
} while (retryContext.isRetryAllowedAfterRuntimeException());

src/test/java/io/github/robwin/circuitbreaker/CircuitBreakerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public void shouldChainDecoratedFunctions() throws ExecutionException, Interrupt
221221
// You can chain other functions with map and flatMap. The Try Monad returns a Success<String>, if the all
222222
// functions run successfully.
223223
Try<String> result = Try.of(decoratedSupplier)
224-
.map(decoratedFunction);
224+
.mapTry(decoratedFunction::apply);
225225

226226
// Then
227227
assertThat(result.isSuccess()).isTrue();

src/test/java/io/github/robwin/retry/RunnableRetryTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ public void setUp(){
3939
helloWorldService = mock(HelloWorldService.class);
4040
}
4141

42+
43+
@Test
44+
public void shouldNotRetry() {
45+
// Create a Retry with default configuration
46+
Retry retryContext = Retry.ofDefaults();
47+
// Decorate the invocation of the HelloWorldService
48+
Try.CheckedRunnable retryableRunnable = Retry.retryableCheckedRunnable(helloWorldService::sayHelloWorld, retryContext);
49+
50+
// When
51+
Try<Void> result = Try.run(retryableRunnable);
52+
// Then the helloWorldService should be invoked 1 time
53+
BDDMockito.then(helloWorldService).should(times(1)).sayHelloWorld();
54+
// and the result should be a success
55+
assertThat(result.isSuccess()).isTrue();
56+
}
57+
4258
@Test
4359
public void shouldReturnAfterThreeAttempts() {
4460
// Given the HelloWorldService throws an exception

src/test/java/io/github/robwin/retry/SupplierRetryTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ public void setUp(){
3939
helloWorldService = mock(HelloWorldService.class);
4040
}
4141

42+
@Test
43+
public void shouldNotRetry() {
44+
// Given the HelloWorldService returns Hello world
45+
given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
46+
// Create a Retry with default configuration
47+
Retry retryContext = Retry.ofDefaults();
48+
// Decorate the invocation of the HelloWorldService
49+
Try.CheckedSupplier<String> retryableSupplier = Retry.retryableCheckedSupplier(helloWorldService::returnHelloWorld, retryContext);
50+
51+
// When
52+
Try<String> result = Try.of(retryableSupplier);
53+
// Then the helloWorldService should be invoked 1 time
54+
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
55+
// and the result should be a success
56+
assertThat(result.isSuccess()).isTrue();
57+
assertThat(result.get()).isEqualTo("Hello world");
58+
}
59+
4260
@Test
4361
public void shouldReturnSuccessfullyAfterSecondAttempt() {
4462
// Given the HelloWorldService throws an exception

0 commit comments

Comments
 (0)