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

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

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

Try.isFailure介绍

[英]Checks if this is a Failure.
[中]检查这是否为故障。

代码示例

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

  1. default void orElseRun(Consumer<? super Throwable> action) {
  2. Objects.requireNonNull(action, "action is null");
  3. if (isFailure()) {
  4. action.accept(getCause());
  5. }
  6. }

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

  1. default <X extends Throwable> T getOrElseThrow(Function<? super Throwable, X> exceptionProvider) throws X {
  2. Objects.requireNonNull(exceptionProvider, "exceptionProvider is null");
  3. if (isFailure()) {
  4. throw exceptionProvider.apply(getCause());
  5. } else {
  6. return get();
  7. }
  8. }

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

  1. default T getOrElseGet(Function<? super Throwable, ? extends T> other) {
  2. Objects.requireNonNull(other, "other is null");
  3. if (isFailure()) {
  4. return other.apply(getCause());
  5. } else {
  6. return get();
  7. }
  8. }

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

  1. /**
  2. * Folds either the {@code Failure} or the {@code Success} side of the Try value.
  3. *
  4. * @param ifFail maps the left value if this is a {@code Failure}
  5. * @param f maps the value if this is a {@code Success}
  6. * @param <X> type of the folded value
  7. * @return A value of type X
  8. */
  9. default <X> X fold(Function<? super Throwable, ? extends X> ifFail, Function<? super T, ? extends X> f) {
  10. if (isFailure()) {
  11. return ifFail.apply(getCause());
  12. } else {
  13. return f.apply(get());
  14. }
  15. }

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

  1. /**
  2. * Returns {@code Success(throwable)} if this is a {@code Failure(throwable)}, otherwise
  3. * a {@code Failure(new NoSuchElementException("Success.failed()"))} if this is a Success.
  4. *
  5. * @return a new Try
  6. */
  7. default Try<Throwable> failed() {
  8. if (isFailure()) {
  9. return new Success<>(getCause());
  10. } else {
  11. return new Failure<>(new NoSuchElementException("Success.failed()"));
  12. }
  13. }

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

  1. /**
  2. * Checks if this Future completed with a failure.
  3. *
  4. * @return true, if this Future completed and is a Failure, false otherwise.
  5. */
  6. default boolean isFailure() {
  7. return isCompleted() && getValue().get().isFailure();
  8. }

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

  1. /**
  2. * Converts this {@code Try} to an {@link Either}.
  3. *
  4. * @return A new {@code Either}
  5. */
  6. default Either<Throwable, T> toEither() {
  7. if (isFailure()) {
  8. return Either.left(getCause());
  9. } else {
  10. return Either.right(get());
  11. }
  12. }

代码示例来源: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 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 decorateConsumer() throws Exception {
  3. Consumer<Integer> consumer = mock(Consumer.class);
  4. Consumer<Integer> decorated = RateLimiter.decorateConsumer(limit, consumer);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try<Integer> decoratedConsumerResult = Try.success(1).andThen(decorated);
  8. then(decoratedConsumerResult.isFailure()).isTrue();
  9. then(decoratedConsumerResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(consumer, never()).accept(any());
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondConsumerResult = Try.success(1).andThen(decorated);
  14. then(secondConsumerResult.isSuccess()).isTrue();
  15. verify(consumer, times(1)).accept(1);
  16. }

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

  1. @Test
  2. public void decorateFunction() throws Exception {
  3. Function<Integer, String> function = mock(Function.class);
  4. Function<Integer, String> decorated = RateLimiter.decorateFunction(limit, function);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try<String> decoratedFunctionResult = Try.success(1).map(decorated);
  8. then(decoratedFunctionResult.isFailure()).isTrue();
  9. then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(function, never()).apply(any());
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondFunctionResult = Try.success(1).map(decorated);
  14. then(secondFunctionResult.isSuccess()).isTrue();
  15. verify(function, times(1)).apply(1);
  16. }

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

相关文章