本文整理了Java中reactor.core.publisher.Mono.block()
方法的一些代码示例,展示了Mono.block()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mono.block()
方法的具体详情如下:
包路径:reactor.core.publisher.Mono
类名称:Mono
方法名:block
[英]Subscribe to this Mono and block indefinitely until a next signal is received. Returns that value, or null if the Mono completes empty. In case the Mono errors, the original exception is thrown (wrapped in a RuntimeException if it was a checked exception).
Note that each block() will trigger a new subscription: in other words, the result might miss signal from hot publishers.
[中]无限期订阅此单声道和块,直到收到下一个信号。返回该值,如果Mono为空,则返回null。在Mono错误的情况下,会抛出原始异常(如果它是选中的异常,则包装在RuntimeException中)。
请注意,每个block()都将触发一个新订阅:换句话说,结果可能会错过来自热门发布服务器的信号。
代码示例来源:origin: spring-projects/spring-framework
/**
* Return the raw request body content written through the request.
* <p><strong>Note:</strong> If the request content has not been consumed
* for any reason yet, use of this method will trigger consumption.
* @throws IllegalStateException if the request body is not been fully written.
*/
@Nullable
public byte[] getRequestBodyContent() {
return this.requestBody.block(this.timeout);
}
代码示例来源:origin: spring-projects/spring-framework
private void testCheckResource(Resource location, String requestPath) throws IOException {
List<Resource> locations = singletonList(location);
Resource actual = this.resolver.resolveResource(null, requestPath, locations, null).block(TIMEOUT);
if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) {
fail(requestPath + " doesn't actually exist as a relative path");
}
assertNull(actual);
}
代码示例来源:origin: spring-projects/spring-framework
@SuppressWarnings("unchecked")
private <T> T resolveValueWithEmptyBody(MethodParameter param) {
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/path"));
Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);
Object value = result.block(Duration.ofSeconds(5));
if (value != null) {
assertTrue("Unexpected parameter type: " + value,
param.getParameterType().isAssignableFrom(value.getClass()));
}
//no inspection unchecked
return (T) value;
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void resolveFromClasspath() throws IOException {
Resource location = new ClassPathResource("test/", PathResourceResolver.class);
String path = "bar.css";
List<Resource> locations = singletonList(location);
Resource actual = this.resolver.resolveResource(null, path, locations, null).block(TIMEOUT);
assertEquals(location.createRelative(path), actual);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void reactivexCompletableToPublisher() {
Object source = io.reactivex.Completable.complete();
Object target = getAdapter(io.reactivex.Completable.class).toPublisher(source);
assertTrue("Expected Mono Publisher: " + target.getClass().getName(), target instanceof Mono);
((Mono<Void>) target).block(Duration.ofMillis(1000));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void checkResourceWithAllowedLocations() {
this.resolver.setAllowedLocations(
new ClassPathResource("test/", PathResourceResolver.class),
new ClassPathResource("testalternatepath/", PathResourceResolver.class)
);
Resource location = getResource("main.css");
String actual = this.resolver.resolveUrlPath("../testalternatepath/bar.css",
singletonList(location), null).block(TIMEOUT);
assertEquals("../testalternatepath/bar.css", actual);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void rxSingleToPublisher() {
Object source = rx.Single.just(1);
Object target = getAdapter(rx.Single.class).toPublisher(source);
assertTrue("Expected Mono Publisher: " + target.getClass().getName(), target instanceof Mono);
assertEquals(Integer.valueOf(1), ((Mono<Integer>) target).block(Duration.ofMillis(1000)));
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-14952
public void writeAndFlushWithFluxOfDefaultDataBuffer() throws Exception {
TestServerHttpResponse response = new TestServerHttpResponse();
Flux<Flux<DefaultDataBuffer>> flux = Flux.just(Flux.just(wrap("foo")));
response.writeAndFlushWith(flux).block();
assertTrue(response.statusCodeWritten);
assertTrue(response.headersWritten);
assertTrue(response.cookiesWritten);
assertEquals(1, response.body.size());
assertEquals("foo", new String(response.body.get(0).asByteBuffer().array(), StandardCharsets.UTF_8));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void publisherToFlux() {
List<Integer> sequence = Arrays.asList(1, 2, 3);
Publisher<Integer> source = Flowable.fromIterable(sequence);
Object target = getAdapter(Flux.class).fromPublisher(source);
assertTrue(target instanceof Flux);
assertEquals(sequence, ((Flux<Integer>) target).collectList().block(Duration.ofMillis(1000)));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void filterWithParameterMethodNotAllowed() {
postForm("_method=TRACE").block(Duration.ZERO);
assertEquals(HttpMethod.POST, this.filterChain.getHttpMethod());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void handleNestedAnnotatedException() {
Throwable ex = new Exception(new CustomException());
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
assertEquals(HttpStatus.I_AM_A_TEAPOT, this.exchange.getResponse().getStatusCode());
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-15331
public void decodeFullContentAsString() {
String body = "data:foo\ndata:bar\n\ndata:baz\n\n";
MockServerHttpRequest request = MockServerHttpRequest.post("/")
.body(Mono.just(stringBuffer(body)));
String actual = messageReader
.readMono(ResolvableType.forClass(String.class), request, Collections.emptyMap())
.cast(String.class)
.block(Duration.ZERO);
assertEquals(body, actual);
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-13149
public void resolveWithNullRequest() {
String file = "js/foo.js";
Resource resolved = this.resolver.resolveResource(null, file, this.locations).block(TIMEOUT);
assertEquals(getResource(file).getDescription(), resolved.getDescription());
assertEquals(getResource(file).getFilename(), resolved.getFilename());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void resolveResourceMatchingEncoding() {
Resource resource = Mockito.mock(Resource.class);
Resource gzipped = Mockito.mock(Resource.class);
this.cache.put(resourceKey("bar.css"), resource);
this.cache.put(resourceKey("bar.css+encoding=gzip"), gzipped);
String file = "bar.css";
MockServerWebExchange exchange = MockServerWebExchange.from(get(file));
assertSame(resource, this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT));
exchange = MockServerWebExchange.from(get(file).header("Accept-Encoding", "gzip"));
assertSame(gzipped, this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void fromObject() {
String body = "foo";
EntityResponse<String> response = EntityResponse.fromObject(body).build().block();
assertSame(body, response.entity());
}
代码示例来源:origin: spring-projects/spring-framework
@SuppressWarnings("unchecked")
private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) {
assertEquals(StringDecoder.class, decoder.getClass());
assertTrue(decoder.canDecode(forClass(String.class), MimeTypeUtils.TEXT_PLAIN));
assertEquals(!textOnly, decoder.canDecode(forClass(String.class), MediaType.TEXT_EVENT_STREAM));
Flux<String> flux = (Flux<String>) decoder.decode(
Flux.just(new DefaultDataBufferFactory().wrap("line1\nline2".getBytes(StandardCharsets.UTF_8))),
ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap());
assertEquals(Arrays.asList("line1", "line2"), flux.collectList().block(Duration.ZERO));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void writeOneItem() throws Exception {
Mono<Void> completion = Flux.just("one").as(this::sendOperator);
Signal<Void> signal = completion.materialize().block();
assertNotNull(signal);
assertTrue("Unexpected signal: " + signal, signal.isOnComplete());
assertEquals(1, this.writer.items.size());
assertEquals("one", this.writer.items.get(0));
assertTrue(this.writer.completed);
}
代码示例来源:origin: spring-projects/spring-framework
private ServerHttpRequest generateMultipartRequest() {
MultipartBodyBuilder partsBuilder = new MultipartBodyBuilder();
partsBuilder.part("fooPart", new ClassPathResource("org/springframework/http/codec/multipart/foo.txt"));
partsBuilder.part("barPart", "bar");
MockClientHttpRequest outputMessage = new MockClientHttpRequest(HttpMethod.POST, "/");
new MultipartHttpMessageWriter()
.write(Mono.just(partsBuilder.build()), null, MediaType.MULTIPART_FORM_DATA, outputMessage, null)
.block(Duration.ofSeconds(5));
return MockServerHttpRequest.post("/")
.contentType(outputMessage.getHeaders().getContentType())
.body(outputMessage.getBody());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void resolveUrlPathWithRelativePathInParentDirectory() {
Resource resource = getResource("images/image.png");
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
String actual = this.transformer.resolveUrlPath("../bar.css", exchange, resource, this.chain).block(TIMEOUT);
assertEquals("../bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void resolveUrlPath() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/resources/main.css"));
String resourcePath = "/resources/bar.css";
Resource resource = getResource("main.css");
String actual = this.transformer.resolveUrlPath(resourcePath, exchange, resource, this.chain).block(TIMEOUT);
assertEquals("/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
assertEquals("/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}
内容来源于网络,如有侵权,请联系作者删除!