Search in sources :

Example 6 with CheckedRunnable

use of io.vavr.CheckedRunnable in project resilience4j by resilience4j.

the class TimerTest method shouldDecorateCheckedRunnableAndReturnWithSuccess.

@Test
public void shouldDecorateCheckedRunnableAndReturnWithSuccess() throws Throwable {
    // And measure the call with a Timer
    CheckedRunnable timedRunnable = Timer.decorateCheckedRunnable(timer, helloWorldService::sayHelloWorldWithException);
    timedRunnable.run();
    assertThat(timer.getMetrics().getNumberOfTotalCalls()).isEqualTo(1);
    assertThat(timer.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1);
    assertThat(timer.getMetrics().getNumberOfFailedCalls()).isEqualTo(0);
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorldWithException();
}
Also used : CheckedRunnable(io.vavr.CheckedRunnable) Test(org.junit.Test)

Example 7 with CheckedRunnable

use of io.vavr.CheckedRunnable in project resilience4j by resilience4j.

the class BulkheadTest method shouldReturnFailureWithBulkheadFullException.

@Test
public void shouldReturnFailureWithBulkheadFullException() {
    // tag::bulkheadFullException[]
    // Given
    BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(2).build();
    Bulkhead bulkhead = Bulkhead.of("test", config);
    bulkhead.isCallPermitted();
    bulkhead.isCallPermitted();
    // When
    CheckedRunnable checkedRunnable = Bulkhead.decorateCheckedRunnable(bulkhead, () -> {
        throw new RuntimeException("BAM!");
    });
    Try result = Try.run(checkedRunnable);
    // Then
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(BulkheadFullException.class);
// end::bulkheadFullException[]
}
Also used : CheckedRunnable(io.vavr.CheckedRunnable) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 8 with CheckedRunnable

use of io.vavr.CheckedRunnable in project resilience4j by resilience4j.

the class BulkheadTest method shouldDecorateCheckedRunnableAndReturnWithException.

@Test
public void shouldDecorateCheckedRunnableAndReturnWithException() throws Throwable {
    // Given
    Bulkhead bulkhead = Bulkhead.of("test", config);
    // When
    CheckedRunnable checkedRunnable = Bulkhead.decorateCheckedRunnable(bulkhead, () -> {
        throw new RuntimeException("BAM!");
    });
    Try<Void> result = Try.run(checkedRunnable);
    // Then
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}
Also used : CheckedRunnable(io.vavr.CheckedRunnable) Test(org.junit.Test)

Example 9 with CheckedRunnable

use of io.vavr.CheckedRunnable in project resilience4j by resilience4j.

the class BulkheadTest method shouldReturnFailureWithRuntimeException.

@Test
public void shouldReturnFailureWithRuntimeException() {
    // Given
    BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(2).build();
    Bulkhead bulkhead = Bulkhead.of("test", config);
    bulkhead.isCallPermitted();
    // v When
    CheckedRunnable checkedRunnable = Bulkhead.decorateCheckedRunnable(bulkhead, () -> {
        throw new RuntimeException("BAM!");
    });
    Try result = Try.run(checkedRunnable);
    // Then
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}
Also used : CheckedRunnable(io.vavr.CheckedRunnable) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 10 with CheckedRunnable

use of io.vavr.CheckedRunnable in project resilience4j by resilience4j.

the class EventPublisherTest method shouldReturnAfterThreeAttempts.

@Test
public void shouldReturnAfterThreeAttempts() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()).map(RetryEvent::getEventType).test();
    // Decorate the invocation of the HelloWorldService
    CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
    // When
    Try<Void> result = Try.run(retryableRunnable);
    // Then the helloWorldService should be invoked 3 times
    BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld();
    // and the result should be a failure
    Assertions.assertThat(result.isFailure()).isTrue();
    // and the returned exception should be of type RuntimeException
    Assertions.assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
    Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
    testSubscriber.assertValueCount(1).assertValues(RetryEvent.Type.ERROR);
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) CheckedRunnable(io.vavr.CheckedRunnable) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Aggregations

CheckedRunnable (io.vavr.CheckedRunnable)13 Test (org.junit.Test)13 Retry (io.github.resilience4j.retry.Retry)7 WebServiceException (javax.xml.ws.WebServiceException)7 RetryConfig (io.github.resilience4j.retry.RetryConfig)4 Try (io.vavr.control.Try)4 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)1 IntervalFunction (io.github.resilience4j.retry.IntervalFunction)1 HelloWorldService (io.github.resilience4j.test.HelloWorldService)1 API (io.vavr.API)1 Predicates (io.vavr.Predicates)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Assertions (org.assertj.core.api.Assertions)1 Before (org.junit.Before)1 BDDMockito (org.mockito.BDDMockito)1 Mockito (org.mockito.Mockito)1