Java Code Examples for io.github.resilience4j.retry.Retry#Metrics

The following examples show how to use io.github.resilience4j.retry.Retry#Metrics . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingSingle() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());
    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();

    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 2
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingFlowable() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    Flowable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 3
Source File: RetryOperatorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void returnOnErrorUsingMono() {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryOperator<String> retryOperator = RetryOperator.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    StepVerifier.create(Mono.fromCallable(helloWorldService::returnHelloWorld)
        .transformDeferred(retryOperator))
        .expectSubscription()
        .expectError(HelloWorldException.class)
        .verify(Duration.ofSeconds(1));

    StepVerifier.create(Mono.fromCallable(helloWorldService::returnHelloWorld)
        .transformDeferred(retryOperator))
        .expectSubscription()
        .expectError(HelloWorldException.class)
        .verify(Duration.ofSeconds(1));

    then(helloWorldService).should(times(6)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 4
Source File: RetryOperatorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void retryOnResultFailAfterMaxAttemptsUsingFlux() {
    RetryConfig config = RetryConfig.<String>custom()
        .retryOnResult("retry"::equals)
        .waitDuration(Duration.ofMillis(10))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);

    StepVerifier.create(Flux.just("retry")
        .transformDeferred(RetryOperator.of(retry)))
        .expectSubscription()
        .expectNextCount(1)
        .expectComplete()
        .verify(Duration.ofSeconds(1));

    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
}
 
Example 5
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingObservable() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    Observable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 6
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void returnOnErrorUsingObservable() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    Observable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete();
    Observable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete();

    then(helloWorldService).should(times(6)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 7
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotRetryWhenItThrowErrorSingle() {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new Error("BAM!"));

    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(Error.class)
        .assertNotComplete()
        .assertSubscribed();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 8
Source File: RetryOperatorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void retryOnResultUsingFlux() {
    RetryConfig config = RetryConfig.<String>custom()
        .retryOnResult("retry"::equals)
        .waitDuration(Duration.ofMillis(10))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);

    StepVerifier.create(Flux.just("retry", "success")
        .transformDeferred(RetryOperator.of(retry)))
        .expectSubscription()
        .expectNext("retry")
        .expectNext("success")
        .expectComplete()
        .verify(Duration.ofMillis(100));

    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
}
 
Example 9
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void retryOnResultUsingMaybe() throws InterruptedException {
    RetryConfig config = RetryConfig.<String>custom()
        .retryOnResult("retry"::equals)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willReturn("retry")
        .willReturn("success");
    Maybe.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertValueCount(1)
        .assertValue("success")
        .assertComplete();

    then(helloWorldService).should(times(2)).returnHelloWorld();

    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
}
 
Example 10
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void retryOnResultUsingSingle() throws InterruptedException {
    RetryConfig config = RetryConfig.<String>custom()
        .retryOnResult("retry"::equals)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willReturn("retry")
        .willReturn("success");

    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertValueCount(1)
        .assertValue("success")
        .assertComplete()
        .assertSubscribed();

    then(helloWorldService).should(times(2)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
}
 
Example 11
Source File: RetryOperatorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotRetryWhenItThrowErrorMono() {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryOperator<String> retryOperator = RetryOperator.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new Error("BAM!"));

    StepVerifier.create(Mono.fromCallable(helloWorldService::returnHelloWorld)
        .transformDeferred(retryOperator))
        .expectSubscription()
        .expectError(Error.class)
        .verify(Duration.ofSeconds(1));

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 12
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void retryOnResultUsingObservable() throws InterruptedException {
    RetryConfig config = RetryConfig.<String>custom()
        .retryOnResult("retry"::equals)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willReturn("retry")
        .willReturn("success");

    Observable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertValueCount(1)
        .assertValue("success")
        .assertComplete()
        .assertSubscribed();

    then(helloWorldService).should(times(2)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
}
 
Example 13
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void retryOnResultUsingFlowable() throws InterruptedException {
    RetryConfig config = RetryConfig.<String>custom()
        .retryOnResult("retry"::equals)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willReturn("retry")
        .willReturn("success");

    Flowable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertValueCount(1)
        .assertValue("success")
        .assertComplete()
        .assertSubscribed();

    then(helloWorldService).should(times(2)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
}
 
Example 14
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotRetryWhenItThrowErrorSingle() {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new Error("BAM!"));

    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(Error.class)
        .assertNotComplete();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 15
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test(expected = StackOverflowError.class)
public void shouldNotRetryUsingSingleStackOverFlow() {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new StackOverflowError("BAM!"));

    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 16
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnCompleteUsingMaybe() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Maybe.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertValueCount(1)
        .assertValues("Hello world")
        .assertComplete();
    Maybe.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertValueCount(1)
        .assertValues("Hello world")
        .assertComplete();

    then(helloWorldService).should(times(4)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();

    assertThat(metrics.getNumberOfSuccessfulCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 17
Source File: RetryOperatorTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnCompleteUsingMono() {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryOperator<String> retryOperator = RetryOperator.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");

    StepVerifier.create(Mono.fromCallable(helloWorldService::returnHelloWorld)
        .transformDeferred(retryOperator))
        .expectNext("Hello world")
        .expectComplete()
        .verify(Duration.ofSeconds(1));
    StepVerifier.create(Mono.fromCallable(helloWorldService::returnHelloWorld)
        .transformDeferred(retryOperator))
        .expectNext("Hello world")
        .expectComplete()
        .verify(Duration.ofSeconds(1));

    then(helloWorldService).should(times(4)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfSuccessfulCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 18
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnErrorUsingObservable() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    Observable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();
    Observable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();

    then(helloWorldService).should(times(6)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 19
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnCompleteUsingMaybe() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Maybe.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertValueCount(1)
        .assertValues("Hello world")
        .assertComplete();
    Maybe.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertValueCount(1)
        .assertValues("Hello world")
        .assertComplete();

    then(helloWorldService).should(times(4)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();

    assertThat(metrics.getNumberOfSuccessfulCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 20
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnErrorUsingCompletable() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    doThrow(new HelloWorldException()).when(helloWorldService).sayHelloWorld();

    Completable.fromRunnable(helloWorldService::sayHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();
    Completable.fromRunnable(helloWorldService::sayHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();

    then(helloWorldService).should(times(6)).sayHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}