Search in sources :

Example 1 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class RateLimiterTest method decorateConsumer.

@Test
public void decorateConsumer() throws Exception {
    Consumer<Integer> consumer = mock(Consumer.class);
    Consumer<Integer> decorated = RateLimiter.decorateConsumer(limit, consumer);
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
    Try<Integer> decoratedConsumerResult = Try.success(1).andThen(decorated);
    then(decoratedConsumerResult.isFailure()).isTrue();
    then(decoratedConsumerResult.getCause()).isInstanceOf(RequestNotPermitted.class);
    verify(consumer, never()).accept(any());
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
    Try secondConsumerResult = Try.success(1).andThen(decorated);
    then(secondConsumerResult.isSuccess()).isTrue();
    verify(consumer, times(1)).accept(1);
}
Also used : Try(io.vavr.control.Try) Test(org.junit.Test)

Example 2 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class RateLimiterTest method decorateCheckedRunnable.

@Test
public void decorateCheckedRunnable() throws Throwable {
    CheckedRunnable runnable = mock(CheckedRunnable.class);
    CheckedRunnable decorated = RateLimiter.decorateCheckedRunnable(limit, runnable);
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
    Try decoratedRunnableResult = Try.run(decorated);
    then(decoratedRunnableResult.isFailure()).isTrue();
    then(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
    verify(runnable, never()).run();
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
    Try secondRunnableResult = Try.run(decorated);
    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 3 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class RateLimiterTest method decorateCompletionStage.

@Test
public void decorateCompletionStage() throws Exception {
    Supplier supplier = mock(Supplier.class);
    BDDMockito.given(supplier.get()).willReturn("Resource");
    Supplier<CompletionStage<String>> completionStage = () -> supplyAsync(supplier);
    Supplier<CompletionStage<String>> decorated = RateLimiter.decorateCompletionStage(limit, completionStage);
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
    AtomicReference<Throwable> error = new AtomicReference<>(null);
    CompletableFuture<String> notPermittedFuture = decorated.get().whenComplete((v, e) -> error.set(e)).toCompletableFuture();
    Try<String> errorResult = Try.of(notPermittedFuture::get);
    assertTrue(errorResult.isFailure());
    then(errorResult.getCause()).isInstanceOf(ExecutionException.class);
    then(notPermittedFuture.isCompletedExceptionally()).isTrue();
    then(error.get()).isExactlyInstanceOf(RequestNotPermitted.class);
    verify(supplier, never()).get();
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
    AtomicReference<Throwable> shouldBeEmpty = new AtomicReference<>(null);
    CompletableFuture<String> success = decorated.get().whenComplete((v, e) -> error.set(e)).toCompletableFuture();
    Try<String> successResult = Try.of(success::get);
    then(successResult.isSuccess()).isTrue();
    then(success.isCompletedExceptionally()).isFalse();
    then(shouldBeEmpty.get()).isNull();
    verify(supplier).get();
}
Also used : Match(io.vavr.API.Match) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) CheckedFunction1(io.vavr.CheckedFunction1) CheckedFunction0(io.vavr.CheckedFunction0) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Predicates.instanceOf(io.vavr.Predicates.instanceOf) CheckedRunnable(io.vavr.CheckedRunnable) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) Duration(java.time.Duration) API.$(io.vavr.API.$) Before(org.junit.Before) Awaitility.await(com.jayway.awaitility.Awaitility.await) Assert.assertTrue(org.junit.Assert.assertTrue) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) Mockito.times(org.mockito.Mockito.times) Case(io.vavr.API.Case) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) LockSupport(java.util.concurrent.locks.LockSupport) Consumer(java.util.function.Consumer) BDDMockito(org.mockito.BDDMockito) Mockito.never(org.mockito.Mockito.never) Try(io.vavr.control.Try) CompletionStage(java.util.concurrent.CompletionStage) Mockito.mock(org.mockito.Mockito.mock) Supplier(java.util.function.Supplier) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompletionStage(java.util.concurrent.CompletionStage) Test(org.junit.Test)

Example 4 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class RateLimiterTest method decorateCheckedFunction.

@Test
public void decorateCheckedFunction() throws Throwable {
    CheckedFunction1<Integer, String> function = mock(CheckedFunction1.class);
    CheckedFunction1<Integer, String> decorated = RateLimiter.decorateCheckedFunction(limit, function);
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
    Try<String> decoratedFunctionResult = Try.success(1).mapTry(decorated);
    then(decoratedFunctionResult.isFailure()).isTrue();
    then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
    verify(function, never()).apply(any());
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
    Try secondFunctionResult = Try.success(1).mapTry(decorated);
    then(secondFunctionResult.isSuccess()).isTrue();
    verify(function, times(1)).apply(1);
}
Also used : Try(io.vavr.control.Try) Test(org.junit.Test)

Example 5 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class RateLimiterTest method decorateFunction.

@Test
public void decorateFunction() throws Exception {
    Function<Integer, String> function = mock(Function.class);
    Function<Integer, String> decorated = RateLimiter.decorateFunction(limit, function);
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
    Try<String> decoratedFunctionResult = Try.success(1).map(decorated);
    then(decoratedFunctionResult.isFailure()).isTrue();
    then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
    verify(function, never()).apply(any());
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
    Try secondFunctionResult = Try.success(1).map(decorated);
    then(secondFunctionResult.isSuccess()).isTrue();
    verify(function, times(1)).apply(1);
}
Also used : 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