io.vavr.control.Try.run()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(15.6k)|赞(0)|评价(0)|浏览(446)

本文整理了Java中io.vavr.control.Try.run()方法的一些代码示例,展示了Try.run()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.run()方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:run

Try.run介绍

[英]Creates a Try of a CheckedRunnable.
[中]创建CheckedRunnable的尝试。

代码示例

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Creates a Try of a Runnable.
  3. *
  4. * @param runnable A runnable
  5. * @return {@code Success(null)} if no exception occurs, otherwise {@code Failure(throwable)} if an exception occurs
  6. * calling {@code runnable.run()}.
  7. */
  8. static Try<Void> runRunnable(Runnable runnable) {
  9. Objects.requireNonNull(runnable, "runnable is null");
  10. return run(runnable::run);
  11. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Transforms the value of this {@code Future}, whether it is a success or a failure.
  3. *
  4. * @param f A transformation
  5. * @param <U> Generic type of transformation {@code Try} result
  6. * @return A {@code Future} of type {@code U}
  7. * @throws NullPointerException if {@code f} is null
  8. */
  9. default <U> Future<U> transformValue(Function<? super Try<T>, ? extends Try<? extends U>> f) {
  10. Objects.requireNonNull(f, "f is null");
  11. return run(executor(), complete ->
  12. onComplete(t -> Try.run(() -> complete.with(f.apply(t)))
  13. .onFailure(x -> complete.with(Try.failure(x)))
  14. )
  15. );
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldDecorateConsumerAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. // When
  6. Consumer<String> consumer = Bulkhead.decorateConsumer(bulkhead, (value) -> {throw new RuntimeException("BAM!");});
  7. Try<Void> result = Try.run(() -> consumer.accept("Tom"));
  8. // Then
  9. assertThat(result.isFailure()).isTrue();
  10. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  11. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  12. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldDecorateCheckedRunnableAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. // When
  6. CheckedRunnable checkedRunnable = Bulkhead.decorateCheckedRunnable(bulkhead, () -> {throw new RuntimeException("BAM!");});
  7. Try<Void> result = Try.run(checkedRunnable);
  8. // Then
  9. assertThat(result.isFailure()).isTrue();
  10. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  11. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  12. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldDecorateRunnableAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. // When
  6. Runnable runnable = Bulkhead.decorateRunnable(bulkhead, () -> {throw new RuntimeException("BAM!");});
  7. Try<Void> result = Try.run(runnable::run);
  8. //Then
  9. assertThat(result.isFailure()).isTrue();
  10. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  11. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  12. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateCheckedRunnable() throws Throwable {
  3. CheckedRunnable runnable = mock(CheckedRunnable.class);
  4. CheckedRunnable decorated = RateLimiter.decorateCheckedRunnable(limit, runnable);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try decoratedRunnableResult = Try.run(decorated);
  8. then(decoratedRunnableResult.isFailure()).isTrue();
  9. then(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(runnable, never()).run();
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondRunnableResult = Try.run(decorated);
  14. then(secondRunnableResult.isSuccess()).isTrue();
  15. verify(runnable, times(1)).run();
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldDecorateCheckedConsumerAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. // When
  6. CheckedConsumer<String> checkedConsumer = Bulkhead.decorateCheckedConsumer(bulkhead, (value) -> {
  7. throw new RuntimeException("BAM!");
  8. });
  9. Try<Void> result = Try.run(() -> checkedConsumer.accept("Tom"));
  10. // Then
  11. assertThat(result.isFailure()).isTrue();
  12. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  13. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  14. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldDecorateConsumerAndReturnWithException() throws Throwable {
  3. // Given
  4. CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
  5. CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
  6. CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  7. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  8. //When
  9. Consumer<String> consumer = CircuitBreaker.decorateConsumer(circuitBreaker, (value) -> {
  10. throw new RuntimeException("BAM!");
  11. });
  12. //Then
  13. Try<Void> result = Try.run(() -> consumer.accept("Tom"));
  14. assertThat(result.isFailure()).isTrue();
  15. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  16. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  17. assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
  18. assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(0);
  19. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldDecorateCheckedRunnableAndReturnWithException() throws Throwable {
  3. // Given
  4. CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
  5. CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
  6. CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  7. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  8. //When
  9. CheckedRunnable checkedRunnable = CircuitBreaker.decorateCheckedRunnable(circuitBreaker, () -> {
  10. throw new RuntimeException("BAM!");
  11. });
  12. //Then
  13. Try<Void> result = Try.run(checkedRunnable);
  14. assertThat(result.isFailure()).isTrue();
  15. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  16. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  17. assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
  18. assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(0);
  19. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldDecorateRunnableAndReturnWithException() throws Throwable {
  3. // Given
  4. CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
  5. CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
  6. CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  7. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  8. //When
  9. Runnable runnable = CircuitBreaker.decorateRunnable(circuitBreaker, () -> {
  10. throw new RuntimeException("BAM!");
  11. });
  12. //Then
  13. Try<Void> result = Try.run(runnable::run);
  14. assertThat(result.isFailure()).isTrue();
  15. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  16. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  17. assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
  18. assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(0);
  19. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldReturnFailureWithRuntimeException() {
  3. // Given
  4. CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
  5. assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
  6. CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  7. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  8. assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0);
  9. //When
  10. CheckedRunnable checkedRunnable = CircuitBreaker.decorateCheckedRunnable(circuitBreaker, () -> {
  11. throw new RuntimeException("BAM!");
  12. });
  13. Try result = Try.run(checkedRunnable);
  14. //Then
  15. assertThat(result.isFailure()).isTrue();
  16. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  17. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  18. assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
  19. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldDecorateCheckedConsumerAndReturnWithException() throws Throwable {
  3. // Given
  4. CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
  5. CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
  6. CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  7. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  8. //When
  9. CheckedConsumer<String> checkedConsumer = CircuitBreaker.decorateCheckedConsumer(circuitBreaker, (value) -> {
  10. throw new RuntimeException("BAM!");
  11. });
  12. //Then
  13. Try<Void> result = Try.run(() -> checkedConsumer.accept("Tom"));
  14. assertThat(result.isFailure()).isTrue();
  15. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  16. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  17. assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
  18. assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(0);
  19. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldReturnFailureWithRuntimeException() {
  3. // Given
  4. BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(2).build();
  5. Bulkhead bulkhead = Bulkhead.of("test", config);
  6. bulkhead.isCallPermitted();
  7. //v When
  8. CheckedRunnable checkedRunnable = Bulkhead.decorateCheckedRunnable(bulkhead, () -> {throw new RuntimeException("BAM!");});
  9. Try result = Try.run(checkedRunnable);
  10. //Then
  11. assertThat(result.isFailure()).isTrue();
  12. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  13. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  14. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldReturnAfterThreeAttempts() {
  3. // Given the HelloWorldService throws an exception
  4. BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
  5. // Create a Retry with default configuration
  6. Retry retry = Retry.ofDefaults("id");
  7. // Decorate the invocation of the HelloWorldService
  8. CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
  9. // When
  10. Try<Void> result = Try.run(retryableRunnable);
  11. // Then the helloWorldService should be invoked 3 times
  12. BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld();
  13. // and the result should be a failure
  14. Assertions.assertThat(result.isFailure()).isTrue();
  15. // and the returned exception should be of type RuntimeException
  16. Assertions.assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
  17. Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION*2);
  18. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void testDecorateRunnable() {
  3. // Given the HelloWorldService throws an exception
  4. BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
  5. // Create a Retry with default configuration
  6. Retry retry = Retry.ofDefaults("id");
  7. // Decorate the invocation of the HelloWorldService
  8. Runnable runnable = Retry.decorateRunnable(retry, helloWorldService::sayHelloWorld);
  9. // When
  10. Try<Void> result = Try.run(runnable::run);
  11. // Then the helloWorldService should be invoked 3 times
  12. BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld();
  13. // and the result should be a failure
  14. Assertions.assertThat(result.isFailure()).isTrue();
  15. // and the returned exception should be of type RuntimeException
  16. Assertions.assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
  17. Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION*2);
  18. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldReturnFailureWithBulkheadFullException() {
  3. // tag::bulkheadFullException[]
  4. // Given
  5. BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(2).build();
  6. Bulkhead bulkhead = Bulkhead.of("test", config);
  7. bulkhead.isCallPermitted();
  8. bulkhead.isCallPermitted();
  9. // When
  10. CheckedRunnable checkedRunnable = Bulkhead.decorateCheckedRunnable(bulkhead, () -> {throw new RuntimeException("BAM!");});
  11. Try result = Try.run(checkedRunnable);
  12. //Then
  13. assertThat(result.isFailure()).isTrue();
  14. assertThat(result.failed().get()).isInstanceOf(BulkheadFullException.class);
  15. // end::bulkheadFullException[]
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldReturnAfterOneAttempt() {
  3. // Given the HelloWorldService throws an exception
  4. BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
  5. // Create a Retry with default configuration
  6. RetryConfig config = RetryConfig.custom().maxAttempts(1).build();
  7. Retry retry = Retry.of("id", config);
  8. // Decorate the invocation of the HelloWorldService
  9. CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
  10. // When
  11. Try<Void> result = Try.run(retryableRunnable);
  12. // Then the helloWorldService should be invoked 1 time
  13. BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorld();
  14. // and the result should be a failure
  15. Assertions.assertThat(result.isFailure()).isTrue();
  16. // and the returned exception should be of type RuntimeException
  17. Assertions.assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
  18. Assertions.assertThat(sleptTime).isEqualTo(0);
  19. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void testDecorateCheckedRunnable() throws IOException {
  3. CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
  4. CheckedRunnable decoratedRunnable = Decorators.ofCheckedRunnable(() -> helloWorldService.sayHelloWorldWithException())
  5. .withCircuitBreaker(circuitBreaker)
  6. .withRetry(Retry.ofDefaults("id"))
  7. .withRateLimiter(RateLimiter.ofDefaults("testName"))
  8. .withBulkhead(Bulkhead.ofDefaults("testName"))
  9. .decorate();
  10. Try.run(decoratedRunnable);
  11. CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  12. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  13. assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
  14. // Then the helloWorldService should be invoked 1 time
  15. BDDMockito.then(helloWorldService).should(times(1)).sayHelloWorldWithException();
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldTakeIntoAccountBackoffFunction() {
  3. // Given the HelloWorldService throws an exception
  4. BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
  5. // Create a Retry with a backoff function squaring the interval
  6. RetryConfig config = RetryConfig
  7. .custom()
  8. .intervalFunction(IntervalFunction.of(Duration.ofMillis(500), x -> x * x))
  9. .build();
  10. Retry retry = Retry.of("id", config);
  11. // Decorate the invocation of the HelloWorldService
  12. CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
  13. // When
  14. Try<Void> result = Try.run(retryableRunnable);
  15. // Then the slept time should be according to the backoff function
  16. BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld();
  17. Assertions.assertThat(sleptTime).isEqualTo(
  18. RetryConfig.DEFAULT_WAIT_DURATION +
  19. RetryConfig.DEFAULT_WAIT_DURATION*RetryConfig.DEFAULT_WAIT_DURATION);
  20. }
  21. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldReturnAfterTwoAttempts() {
  3. // Given the HelloWorldService throws an exception
  4. BDDMockito.willThrow(new WebServiceException("BAM!")).willDoNothing().given(helloWorldService).sayHelloWorld();
  5. // Create a Retry with default configuration
  6. Retry retry = Retry.ofDefaults("id");
  7. TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher())
  8. .map(RetryEvent::getEventType)
  9. .test();
  10. // Decorate the invocation of the HelloWorldService
  11. CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
  12. // When
  13. Try<Void> result = Try.run(retryableRunnable);
  14. // Then the helloWorldService should be invoked 2 times
  15. BDDMockito.then(helloWorldService).should(Mockito.times(2)).sayHelloWorld();
  16. // and the result should be a sucess
  17. Assertions.assertThat(result.isSuccess()).isTrue();
  18. Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
  19. testSubscriber.assertValueCount(2).assertValues(RetryEvent.Type.RETRY, RetryEvent.Type.SUCCESS);
  20. }

相关文章