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();
}
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[]
}
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);
}
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);
}
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);
}
Aggregations