private StatusAssertions statusAssertions(HttpStatus status) { MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/")); MockClientHttpResponse response = new MockClientHttpResponse(status); MonoProcessor<byte[]> emptyContent = MonoProcessor.create(); emptyContent.onComplete(); ExchangeResult result = new ExchangeResult(request, response, emptyContent, emptyContent, Duration.ZERO, null); return new StatusAssertions(result, mock(WebTestClient.ResponseSpec.class)); }
@Test public void MonoProcessorThenFulfill() { MonoProcessor<Integer> mp = MonoProcessor.create(); mp.onNext(1); MonoProcessor<Integer> mp2 = mp.flatMap(s -> Mono.just(s * 2)) .toProcessor(); mp2.subscribe(); assertThat(mp2.isTerminated()).isTrue(); assertThat(mp2.isSuccess()).isTrue(); assertThat(mp2.peek()).isEqualTo(2); }
private <T> Consumer<T> updateConnectMono(MonoProcessor<Void> connectMono) { return o -> { if (!connectMono.isTerminated()) { if (o instanceof Throwable) { connectMono.onError((Throwable) o); } else { connectMono.onComplete(); } } }; }
private void handleOnError(Throwable ex) { if (!this.content.isTerminated()) { this.content.onError(ex); } }
@Override public boolean cancel(boolean mayInterruptIfRunning) { if (isCancelled()) { return false; } this.processor.cancel(); // isCancelled may still return false, if mono completed before the cancel return this.processor.isCancelled(); }
@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 resolveWithMono() { BindingResult bindingResult = createBindingResult(new Foo(), "foo"); MonoProcessor<BindingResult> monoProcessor = MonoProcessor.create(); monoProcessor.onNext(bindingResult); this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "foo", monoProcessor); MethodParameter parameter = this.testMethod.arg(Errors.class); Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange) .block(Duration.ofMillis(5000)); assertSame(bindingResult, actual); }
@Test public void promiseErrorCountCannotExceedOne() { MonoProcessor<Object> deferred = MonoProcessor.create(); Throwable error = new IOException("foo"); StepVerifier.create(deferred) .then(() -> { deferred.onError(error); deferred.onNext(error); }) .expectErrorMessage("foo") .verifyThenAssertThat() .hasDroppedExactly(error); Assertions.assertThat(deferred.getError()).isSameAs(error); }
@Test public void MonoProcessorMapFulfill() { MonoProcessor<Integer> mp = MonoProcessor.create(); mp.onNext(1); MonoProcessor<Integer> mp2 = mp.map(s -> s * 2) .toProcessor(); mp2.subscribe(); assertThat(mp2.isTerminated()).isTrue(); assertThat(mp2.isSuccess()).isTrue(); assertThat(mp2.peek()).isEqualTo(2); }
@Test public void MonoProcessorRejectedChainTogether() { MonoProcessor<String> mp = MonoProcessor.create(); MonoProcessor<String> mp2 = MonoProcessor.create(); mp.subscribe(mp2); mp.onError(new Exception("test")); assertThat(mp2.getError()).hasMessage("test"); assertThat(mp.isSuccess()).isFalse(); assertThat(mp.isError()).isTrue(); }
@Test public void filterMonoProcessorNot() { MonoProcessor<Integer> mp = MonoProcessor.create(); MonoProcessor<Integer> mp2 = MonoProcessor.create(); StepVerifier.create(mp.filter(s -> s % 2 == 0).subscribeWith(mp2)) .then(() -> mp.onNext(1)) .then(() -> assertThat(mp2.isError()).isFalse()) .then(() -> assertThat(mp2.isSuccess()).isTrue()) .then(() -> assertThat(mp2.peek()).isNull()) .then(() -> assertThat(mp2.isTerminated()).isTrue()) .verifyComplete(); }
@Test public void asyncFusion() { AssertSubscriber<Object> ts = AssertSubscriber.create(); MonoProcessor<Integer> up = MonoProcessor.create(); up.filter(v -> (v & 1) == 0) .subscribe(ts); up.onNext(2); up.onComplete(); ts.assertValues(2) .assertNoError() .assertComplete(); }
@Test public void MonoProcessorNullFulfill() { MonoProcessor<String> mp = MonoProcessor.create(); mp.onNext(null); assertThat(mp.isTerminated()).isTrue(); assertThat(mp.isSuccess()).isTrue(); assertThat(mp.peek()).isNull(); }
@Test public void timeoutRequested() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); MonoProcessor<Integer> source = MonoProcessor.create(); DirectProcessor<Integer> tp = DirectProcessor.create(); source.timeout(tp) .subscribe(ts); tp.onNext(1); source.onNext(2); source.onComplete(); ts.assertNoValues() .assertError(TimeoutException.class) .assertNotComplete(); }
@MessageMapping("mono") public Mono<String> handleMono() { this.mono = MonoProcessor.create(); return this.mono; }
@Test public void scanProcessor() { MonoProcessor<String> test = MonoProcessor.create(); Subscription subscription = Operators.emptySubscription(); test.onSubscribe(subscription); assertThat(test.scan(Scannable.Attr.PREFETCH)).isEqualTo(Integer.MAX_VALUE); assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse(); assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse(); test.onComplete(); assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue(); assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse(); }
/** * 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."); } }
@Override public void onComplete() { if (this.completionMono != null) { this.completionMono.onComplete(); } close(); }