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

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

本文整理了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

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

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

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

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

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

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

@Test
public void shouldDecorateFunctionAndReturnWithException() throws Throwable {
  // Given
  Bulkhead bulkhead = Bulkhead.of("test", config);
  BDDMockito.given(helloWorldService.returnHelloWorldWithName("Tom")).willThrow(new RuntimeException("BAM!"));
  // When
  Function<String, String> function = Bulkhead.decorateFunction(bulkhead, helloWorldService::returnHelloWorldWithName);
  Try<String> result = Try.of(() -> function.apply("Tom"));
  // Then
  assertThat(result.isFailure()).isTrue();
  assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}

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

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

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

@Test
public void shouldDecorateConsumerAndReturnWithException() throws Throwable {
  // Given
  CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
  CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  //When
  Consumer<String> consumer = CircuitBreaker.decorateConsumer(circuitBreaker, (value) -> {
    throw new RuntimeException("BAM!");
  });
  //Then
  Try<Void> result = Try.run(() -> consumer.accept("Tom"));
  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);
}

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

@Test
public void shouldDecorateRunnableAndReturnWithException() throws Throwable {
  // Given
  CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
  CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  //When
  Runnable runnable = CircuitBreaker.decorateRunnable(circuitBreaker, () -> {
    throw new RuntimeException("BAM!");
  });
  //Then
  Try<Void> result = Try.run(runnable::run);
  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);
}

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

@Test
public void shouldDecorateSupplierAndReturnWithException() throws Throwable {
  BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
  // And measure the call with a Timer
  Supplier<String> supplier = Timer.decorateSupplier(timer, helloWorldService::returnHelloWorld);
  Try<String> result = Try.of(supplier::get);
  assertThat(result.isFailure()).isTrue();
  assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  assertThat(timer.getMetrics().getNumberOfTotalCalls()).isEqualTo(1);
  assertThat(timer.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0);
  assertThat(timer.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
  // Then the helloWorldService should be invoked 1 time
  BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
}

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

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

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

@Test
public void shouldDecorateSupplierAndReturnWithException() {
  // Given
  CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  // Given the HelloWorldService throws an exception
  BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
  //When
  Supplier<String> supplier = CircuitBreaker.decorateSupplier(circuitBreaker, helloWorldService::returnHelloWorld);
  //Then
  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 the helloWorldService should be invoked 1 time
  BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld();
}

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

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

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

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

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

@Test
public void shouldDecorateCheckedSupplierAndReturnWithException() throws Throwable {
  // Given
  CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
  CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
  // Given the HelloWorldService throws an exception
  BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new RuntimeException("BAM!"));
  //When
  CheckedFunction0<String> checkedSupplier = CircuitBreaker.decorateCheckedSupplier(circuitBreaker, helloWorldService::returnHelloWorldWithException);
  //Then
  Try<String> result = Try.of(checkedSupplier);
  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 the helloWorldService should be invoked 1 time
  BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorldWithException();
}

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

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

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

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

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

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

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

@Test
public void shouldReturnAfterThreeAttempts() {
  // Given the HelloWorldService throws an exception
  BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
  // Create a Retry with default configuration
  Retry retry = Retry.ofDefaults("id");
  // Decorate the invocation of the HelloWorldService
  CheckedFunction0<String> retryableSupplier = Retry
      .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
  // When
  Try<String> result = Try.of(retryableSupplier);
  // Then the helloWorldService should be invoked 3 times
  BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
  // and the result should be a failure
  assertThat(result.isFailure()).isTrue();
  // and the returned exception should be of type RuntimeException
  assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
  assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}

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

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

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

@Test
public void shouldReturnFailureWithBulkheadFullException() {
  // tag::bulkheadFullException[]
  // Given
  BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(2).build();
  Bulkhead bulkhead = Bulkhead.of("test", config);
  bulkhead.isCallPermitted();
  bulkhead.isCallPermitted();
  // When
  CheckedRunnable checkedRunnable = Bulkhead.decorateCheckedRunnable(bulkhead, () -> {throw new RuntimeException("BAM!");});
  Try result = Try.run(checkedRunnable);
  //Then
  assertThat(result.isFailure()).isTrue();
  assertThat(result.failed().get()).isInstanceOf(BulkheadFullException.class);
  // end::bulkheadFullException[]
}

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

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

相关文章