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();
}
use of io.vavr.control.Try in project resilience4j by resilience4j.
the class IntervalFunctionTest method shouldRejectOutOfBoundsMultiplier.
@Test
public void shouldRejectOutOfBoundsMultiplier() {
// Given
final Duration duration = Duration.ofMillis(100);
final float lessThenOneMultiplier = 0.9999f;
// When
final List<Try> tries = List.of(Try.of(() -> IntervalFunction.ofExponentialBackoff(duration, lessThenOneMultiplier)), Try.of(() -> IntervalFunction.ofExponentialRandomBackoff(duration, lessThenOneMultiplier)));
// Then
Assertions.assertThat(tries.forAll(Try::isFailure)).isTrue();
Assertions.assertThat(tries.map(Try::getCause).forAll(t -> t instanceof IllegalArgumentException)).isTrue();
}
use of io.vavr.control.Try in project resilience4j by resilience4j.
the class IntervalFunctionTest method shouldRejectOutOfBoundsRandomizationFactor.
@Test
public void shouldRejectOutOfBoundsRandomizationFactor() {
// Given
final Duration duration = Duration.ofMillis(100);
final float negativeFactor = -0.0001f;
final float greaterThanOneFactor = 1.0001f;
// When
final List<Try> tries = List.of(Try.of(() -> IntervalFunction.ofRandomized(duration, negativeFactor)), Try.of(() -> IntervalFunction.ofRandomized(duration, greaterThanOneFactor)));
// Then
Assertions.assertThat(tries.forAll(Try::isFailure)).isTrue();
Assertions.assertThat(tries.map(Try::getCause).forAll(t -> t instanceof IllegalArgumentException)).isTrue();
}
Aggregations