Search in sources :

Example 16 with Try

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);
}
Also used : Try(io.vavr.control.Try) Test(org.junit.Test)

Example 17 with Try

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();
}
Also used : CheckedRunnable(io.vavr.CheckedRunnable) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 18 with Try

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();
}
Also used : CheckedFunction0(io.vavr.CheckedFunction0) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 19 with Try

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();
}
Also used : Duration(java.time.Duration) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 20 with Try

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();
}
Also used : Duration(java.time.Duration) Try(io.vavr.control.Try) Test(org.junit.Test)

Aggregations

Try (io.vavr.control.Try)20 Test (org.junit.Test)20 CheckedRunnable (io.vavr.CheckedRunnable)6 Duration (java.time.Duration)6 Supplier (java.util.function.Supplier)4 Before (org.junit.Before)4 BDDMockito (org.mockito.BDDMockito)4 HelloWorldService (io.github.resilience4j.test.HelloWorldService)3 API (io.vavr.API)3 API.$ (io.vavr.API.$)3 CheckedFunction0 (io.vavr.CheckedFunction0)3 Predicates (io.vavr.Predicates)3 WebServiceException (javax.xml.ws.WebServiceException)3 Mockito (org.mockito.Mockito)3 IntervalFunction (io.github.resilience4j.retry.IntervalFunction)2 Retry (io.github.resilience4j.retry.Retry)2 RetryConfig (io.github.resilience4j.retry.RetryConfig)2 Callable (java.util.concurrent.Callable)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2