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

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

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

Try.failed介绍

[英]Returns Success(throwable) if this is a Failure(throwable), otherwise a Failure(new NoSuchElementException("Success.failed()")) if this is a Success.
[中]如果这是一个失败(throwable),则返回Success(throwable);如果这是一个成功,则返回失败(new NoSuchElementException(“Success.failed()”)。

代码示例

代码示例来源: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 shouldDecorateFunctionAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithName("Tom")).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. Function<String, String> function = Bulkhead.decorateFunction(bulkhead, helloWorldService::returnHelloWorldWithName);
  8. Try<String> result = Try.of(() -> function.apply("Tom"));
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. }

代码示例来源: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 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 shouldDecorateSupplierAndReturnWithException() throws Throwable {
  3. BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
  4. // And measure the call with a Timer
  5. Supplier<String> supplier = Timer.decorateSupplier(timer, helloWorldService::returnHelloWorld);
  6. Try<String> result = Try.of(supplier::get);
  7. assertThat(result.isFailure()).isTrue();
  8. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  9. assertThat(timer.getMetrics().getNumberOfTotalCalls()).isEqualTo(1);
  10. assertThat(timer.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0);
  11. assertThat(timer.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
  12. // Then the helloWorldService should be invoked 1 time
  13. BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
  14. }

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

  1. @Test
  2. public void shouldDecorateCheckedFunctionAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithNameWithException("Tom")).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. CheckedFunction1<String, String> function = Bulkhead.decorateCheckedFunction(bulkhead, helloWorldService::returnHelloWorldWithNameWithException);
  8. Try<String> result = Try.of(() -> function.apply("Tom"));
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. }

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

  1. @Test
  2. public void shouldDecorateSupplierAndReturnWithException() {
  3. // Given
  4. CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
  5. CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  6. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  7. // Given the HelloWorldService throws an exception
  8. BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
  9. //When
  10. Supplier<String> supplier = CircuitBreaker.decorateSupplier(circuitBreaker, helloWorldService::returnHelloWorld);
  11. //Then
  12. Try<String> result = Try.ofSupplier(supplier);
  13. assertThat(result.isFailure()).isTrue();
  14. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  15. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  16. assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
  17. assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(0);
  18. // Then the helloWorldService should be invoked 1 time
  19. BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld();
  20. }

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

  1. @Test
  2. public void shouldDecorateCallableAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. Callable<String> callable = Bulkhead.decorateCallable(bulkhead, helloWorldService::returnHelloWorldWithException);
  8. Try<String> result = Try.of(callable::call);
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException();
  14. }

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

  1. @Test
  2. public void shouldDecorateSupplierAndReturnWithException() {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. Supplier<String> supplier = Bulkhead.decorateSupplier(bulkhead, helloWorldService::returnHelloWorld);
  8. Try<String> result = Try.of(supplier::get);
  9. //Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
  14. }

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

  1. @Test
  2. public void shouldDecorateCheckedSupplierAndReturnWithException() 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. // Given the HelloWorldService throws an exception
  9. BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new RuntimeException("BAM!"));
  10. //When
  11. CheckedFunction0<String> checkedSupplier = CircuitBreaker.decorateCheckedSupplier(circuitBreaker, helloWorldService::returnHelloWorldWithException);
  12. //Then
  13. Try<String> result = Try.of(checkedSupplier);
  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. // Then the helloWorldService should be invoked 1 time
  20. BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorldWithException();
  21. }

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

  1. @Test
  2. public void shouldDecorateCheckedSupplierAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. CheckedFunction0<String> checkedSupplier = Bulkhead.decorateCheckedSupplier(bulkhead, helloWorldService::returnHelloWorldWithException);
  8. Try<String> result = Try.of(checkedSupplier);
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException();
  14. }

代码示例来源: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 shouldReturnAfterThreeAttempts() {
  3. // Given the HelloWorldService throws an exception
  4. BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
  5. // Create a Retry with default configuration
  6. Retry retry = Retry.ofDefaults("id");
  7. // Decorate the invocation of the HelloWorldService
  8. CheckedFunction0<String> retryableSupplier = Retry
  9. .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
  10. // When
  11. Try<String> result = Try.of(retryableSupplier);
  12. // Then the helloWorldService should be invoked 3 times
  13. BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
  14. // and the result should be a failure
  15. assertThat(result.isFailure()).isTrue();
  16. // and the returned exception should be of type RuntimeException
  17. assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
  18. assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
  19. }

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

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

代码示例来源: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. }

相关文章