use of io.vavr.control.Try 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.control.Try 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.control.Try in project resilience4j by resilience4j.
the class CircuitBreakerTest method shouldReturnFailureWithCircuitBreakerOpenException.
@Test
public void shouldReturnFailureWithCircuitBreakerOpenException() {
// Given
// Create a custom configuration for a CircuitBreaker
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().ringBufferSizeInClosedState(2).ringBufferSizeInHalfOpenState(2).failureRateThreshold(50).waitDurationInOpenState(Duration.ofMillis(1000)).build();
// Create a CircuitBreakerRegistry with a custom global configuration
CircuitBreaker circuitBreaker = CircuitBreaker.of("testName", circuitBreakerConfig);
circuitBreaker.onError(0, new RuntimeException());
circuitBreaker.onError(0, new RuntimeException());
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.OPEN);
CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(2);
assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(2);
// When
CheckedRunnable checkedRunnable = CircuitBreaker.decorateCheckedRunnable(circuitBreaker, () -> {
throw new RuntimeException("BAM!");
});
Try result = Try.run(checkedRunnable);
// Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(CircuitBreakerOpenException.class);
}
use of io.vavr.control.Try in project resilience4j by resilience4j.
the class RateLimiterTest method decorateRunnable.
@Test
public void decorateRunnable() throws Exception {
Runnable runnable = mock(Runnable.class);
Runnable decorated = RateLimiter.decorateRunnable(limit, runnable);
when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
Try decoratedRunnableResult = Try.success(decorated).andThen(Runnable::run);
then(decoratedRunnableResult.isFailure()).isTrue();
then(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(runnable, never()).run();
when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
Try secondRunnableResult = Try.success(decorated).andThen(Runnable::run);
then(secondRunnableResult.isSuccess()).isTrue();
verify(runnable, times(1)).run();
}
use of io.vavr.control.Try in project resilience4j by resilience4j.
the class RateLimiterTest method decorateCheckedSupplier.
@Test
public void decorateCheckedSupplier() throws Throwable {
CheckedFunction0 supplier = mock(CheckedFunction0.class);
CheckedFunction0 decorated = RateLimiter.decorateCheckedSupplier(limit, supplier);
when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
Try decoratedSupplierResult = Try.of(decorated);
then(decoratedSupplierResult.isFailure()).isTrue();
then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(supplier, never()).apply();
when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
Try secondSupplierResult = Try.of(decorated);
then(secondSupplierResult.isSuccess()).isTrue();
verify(supplier, times(1)).apply();
}
Aggregations