@Override public boolean isDone() { return this.processor.isTerminated(); }
@Override public boolean isDone() { return this.processor.isTerminated(); }
/** * Indicates whether this {@code MonoProcessor} has been successfully completed a value. * * @return {@code true} if this {@code MonoProcessor} is successful, {@code false} otherwise. */ public final boolean isSuccess() { return isTerminated() && error == null; }
private void handleOnError(Throwable ex) { if (!this.content.isTerminated()) { this.content.onError(ex); } }
private <T> Consumer<T> updateConnectMono(MonoProcessor<Void> connectMono) { return o -> { if (!connectMono.isTerminated()) { if (o instanceof Throwable) { connectMono.onError((Throwable) o); } else { connectMono.onComplete(); } } }; }
/** * Indicates whether this {@code MonoProcessor} has been interrupted via cancellation. * * @return {@code true} if this {@code MonoProcessor} is cancelled, {@code false} * otherwise. */ public boolean isCancelled() { return subscription == Operators.cancelledSubscription() && !isTerminated(); }
/** * Return the produced {@link Throwable} error if any or null * * @return the produced {@link Throwable} error if any or null */ @Nullable public final Throwable getError() { return isTerminated() ? error : null; }
private void handleOnComplete() { if (!this.content.isTerminated()) { byte[] bytes = new byte[this.buffer.readableByteCount()]; this.buffer.read(bytes); this.content.onNext(bytes); } } }
public Mono<byte[]> getContent() { return Mono.defer(() -> { if (this.content.isTerminated()) { return this.content; } if (!this.hasContentConsumer) { // Couple of possible cases: // 1. Mock server never consumed request body (e.g. error before read) // 2. FluxExchangeResult: getResponseBodyContent called before getResponseBody //noinspection ConstantConditions (this.publisher != null ? this.publisher : this.publisherNested) .onErrorMap(ex -> new IllegalStateException( "Content has not been consumed, and " + "an error was raised while attempting to produce it.", ex)) .subscribe(); } return this.content; }); }
@Override @Nullable public Object scanUnsafe(Attr key) { //touch guard boolean t = isTerminated(); if (key == Attr.TERMINATED) return t; if (key == Attr.PARENT) return subscription; if (key == Attr.ERROR) return error; if (key == Attr.PREFETCH) return Integer.MAX_VALUE; if (key == Attr.CANCELLED) return isCancelled(); return null; }
/** * Invoke the method for the given exchange. * @param exchange the current exchange * @param bindingContext the binding context to use * @param providedArgs optional list of argument values to match by type * @return a Mono with a {@link HandlerResult}. * @throws ServerErrorException if method argument resolution or method invocation fails */ @Nullable public HandlerResult invokeForHandlerResult(ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) { MonoProcessor<HandlerResult> processor = MonoProcessor.create(); this.delegate.invoke(exchange, bindingContext, providedArgs).subscribeWith(processor); if (processor.isTerminated()) { Throwable ex = processor.getError(); if (ex != null) { throw (ex instanceof ServerErrorException ? (ServerErrorException) ex : new ServerErrorException("Failed to invoke: " + getShortLogMessage(), getMethod(), ex)); } return processor.peek(); } else { // Should never happen... throw new IllegalStateException( "SyncInvocableHandlerMethod should have completed synchronously."); } }
@Test public void whenMonoError() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.<Integer>error(new Exception("test1")), Mono.<Integer>error(new Exception("test2"))) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isTrue()) .then(() -> assertThat(mp.isSuccess()).isFalse()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .verifyErrorSatisfies(e -> assertThat(e).hasMessage("test1")); }
@Test public void whenDelayJustMono3() { MonoProcessor<Tuple3<Integer, Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zipDelayError(Mono.just(1), Mono.just(2), Mono.just(3)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2 && v.getT3() == 3).isTrue()) .verifyComplete(); }
@Test public void whenMonoJust() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void whenDelayJustMono() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zipDelayError(Mono.just(1), Mono.just(2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void firstMonoJust() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.first(Mono.just(1), Mono.just(2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .expectNext(1) .verifyComplete(); }
@Test public void filterMonoNot() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.just(1).filter(s -> s % 2 == 0).subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.peek()).isNull()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .verifyComplete(); } }
@Test public void otherwiseErrorFilter() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.<Integer>error(new TestException()) .onErrorResume(TestException.class, e -> Mono.just(1)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .expectNext(1) .verifyComplete(); }
@Test public void whenMonoCallable() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.fromCallable(() -> 1), Mono.fromCallable(() -> 2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void otherwiseReturnErrorUnfilter2() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.<Integer>error(new TestException()) .onErrorReturn(RuntimeException.class::isInstance, 1) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isTrue()) .then(() -> assertThat(mp.isSuccess()).isFalse()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .verifyError(TestException.class); } }