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);
}
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();
}
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();
}
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);
}
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);
}
Aggregations