Search in sources :

Example 26 with Try

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

Example 27 with Try

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

Example 28 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 29 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 30 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)

Aggregations

Try (io.vavr.control.Try)32 Test (org.junit.Test)31 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 CharSequence (java.lang.CharSequence)8 CheckedRunnable (io.vavr.CheckedRunnable)6 Duration (java.time.Duration)6 Supplier (java.util.function.Supplier)5 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 Function (java.util.function.Function)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