Java Code Examples for io.vavr.control.Try#ofSupplier()

The following examples show how to use io.vavr.control.Try#ofSupplier() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RetryEventPublisherTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldConsumeIgnoredErrorEvent() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());
    RetryConfig retryConfig = RetryConfig.custom()
        .retryOnException(throwable -> Match(throwable).of(
            Case($(instanceOf(HelloWorldException.class)), false),
            Case($(), true)))
        .build();
    retry = Retry.of("testName", retryConfig);
    retry.getEventPublisher().onIgnoredError(
        event -> logger.info(event.getEventType().toString()));

    Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld));

    then(logger).should(times(1)).info("IGNORED_ERROR");
    then(helloWorldService).should(times(1)).returnHelloWorld();
}
 
Example 2
Source File: RetryMetricsCollectorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
    registry = new CollectorRegistry();
    retryRegistry = RetryRegistry.ofDefaults();
    retry = retryRegistry.retry("backendA");

    RetryMetricsCollector.ofRetryRegistry(retryRegistry).register(registry);

    retry.executeSupplier(() -> "return");

    Supplier<String> supplier = Retry.decorateSupplier(retry, () -> {
        throw new RuntimeException();
    });

    Try.ofSupplier(supplier);
}
 
Example 3
Source File: CircuitBreakerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecorateSupplierAndReturnWithException() {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
    given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
    Supplier<String> supplier = circuitBreaker
        .decorateSupplier(helloWorldService::returnHelloWorld);

    Try<String> result = Try.ofSupplier(supplier);

    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(0);
    then(helloWorldService).should().returnHelloWorld();
}
 
Example 4
Source File: BulkheadEventPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConsumeOnCallRejectedEvent() {
    Bulkhead bulkhead = Bulkhead.of("test", config);
    bulkhead.getEventPublisher().onCallRejected(
        event -> logger.info(event.getEventType().toString()));
    bulkhead.tryAcquirePermission();
    Supplier<String> supplier = Bulkhead
        .decorateSupplier(bulkhead, helloWorldService::returnHelloWorld);

    Try.ofSupplier(supplier);

    then(logger).should(times(1)).info("CALL_REJECTED");
}
 
Example 5
Source File: BulkheadEventPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConsumeOnCallFinishedEventWhenExecutionIsFinished() throws Exception {
    Bulkhead bulkhead = Bulkhead.of("test", config);
    bulkhead.getEventPublisher().onCallFinished(
        event -> logger.info(event.getEventType().toString()));
    Supplier<String> supplier = Bulkhead
        .decorateSupplier(bulkhead, helloWorldService::returnHelloWorld);

    Try.ofSupplier(supplier);

    then(logger).should(times(1)).info("CALL_FINISHED");
}
 
Example 6
Source File: RetryEventPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConsumeOnRetryEvent() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());
    retry.getEventPublisher().onRetry(
        event -> logger.info(event.getEventType().toString()));

    Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld));

    then(helloWorldService).should(times(3)).returnHelloWorld();
    then(logger).should(times(2)).info("RETRY");
}
 
Example 7
Source File: RetryEventPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConsumeOnErrorEvent() {
    given(helloWorldService.returnHelloWorld()).willThrow(new HelloWorldException());
    retry.getEventPublisher().onError(
        event -> logger.info(event.getEventType().toString()));

    Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld));

    then(logger).should(times(1)).info("ERROR");
    then(helloWorldService).should(times(3)).returnHelloWorld();
}
 
Example 8
Source File: RateLimiterEventPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConsumeOnFailureEvent() throws Throwable {
    rateLimiter.getEventPublisher().onFailure(
        event -> logger.info(event.getEventType().toString()));
    rateLimiter.executeSupplier(() -> "Hello world");

    Try.ofSupplier(RateLimiter.decorateSupplier(rateLimiter, () -> "Hello world"));

    then(logger).should(times(1)).info("FAILED_ACQUIRE");
}
 
Example 9
Source File: AbstractRetryMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRegisterMetricsWithRetry() throws Throwable {
    Retry retry = givenMetricRegistry(metricRegistry);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException())
        .willThrow(new HelloWorldException())
        .willThrow(new HelloWorldException());
    String value1 = retry.executeSupplier(helloWorldService::returnHelloWorld);

    Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld));

    assertThat(value1).isEqualTo("Hello world");
    then(helloWorldService).should(times(5)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(4);
    assertThat(metricRegistry.getGauges()
        .get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue())
        .isEqualTo(1L);
    assertThat(metricRegistry.getGauges()
        .get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue())
        .isEqualTo(0L);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY)
            .getValue()).isEqualTo(1L);
    assertThat(metricRegistry.getGauges()
        .get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue())
        .isEqualTo(0L);
}