Java Code Examples for io.github.resilience4j.retry.Retry#getMetrics()

The following examples show how to use io.github.resilience4j.retry.Retry#getMetrics() . 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: RetryOperatorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingMono() {
    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());

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

    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 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 3
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 4
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 5
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 6
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingCompletable() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    doThrow(new HelloWorldException()).when(helloWorldService).sayHelloWorld();

    Completable.fromRunnable(helloWorldService::sayHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete();

    then(helloWorldService).should().sayHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 7
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 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();
    Completable.fromRunnable(helloWorldService::sayHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete();

    then(helloWorldService).should(times(6)).sayHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 8
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()
        .assertSubscribed();

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

    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
}
 
Example 9
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingMaybe() {
    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());

    Maybe.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 10
Source File: RetryOperatorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFailWithExceptionFlux() {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryOperator<Object> retryOperator = RetryOperator.of(retry);

    StepVerifier.create(Flux.error(new HelloWorldException())
        .transformDeferred(retryOperator))
        .expectSubscription()
        .expectError(HelloWorldException.class)
        .verify(Duration.ofSeconds(1));

    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfSuccessfulCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 11
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 12
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();

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

    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 13
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 14
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingCompletable() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    doThrow(new HelloWorldException()).when(helloWorldService).sayHelloWorld();

    Completable.fromRunnable(helloWorldService::sayHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();

    then(helloWorldService).should().sayHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 15
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnCompleteUsingSingle() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");

    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertValueCount(1)
        .assertValues("Hello world")
        .assertComplete();
    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .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 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: RetryTransformerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnCompleteUsingCompletable() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    doNothing()
        .doThrow(new HelloWorldException())
        .doThrow(new HelloWorldException())
        .doNothing()
        .when(helloWorldService).sayHelloWorld();
    Completable.fromRunnable(helloWorldService::sayHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertNoValues()
        .assertComplete();
    Completable.fromRunnable(helloWorldService::sayHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertNoValues()
        .assertComplete();

    then(helloWorldService).should(times(4)).sayHelloWorld();

    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 returnOnCompleteUsingFlowable() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

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

    Flowable.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 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);
}