Avoid blocking on a Mono indefinitely

Closes gh-15535
This commit is contained in:
Andy Wilkinson
2018-12-20 15:24:53 +00:00
parent 6403ec6e1b
commit 0741c90489
17 changed files with 139 additions and 91 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.web.embedded.undertow;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.Arrays;
import io.undertow.Undertow;
@@ -114,7 +115,7 @@ public class UndertowReactiveWebServerFactoryTests
Mono<String> result = client.post().uri("/test").contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromObject("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
assertThat(result.block()).isEqualTo("Hello World");
assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
File accessLog = new File(accessLogDirectory, expectedFile);
awaitFile(accessLog);
assertThat(accessLogDirectory.listFiles()).contains(accessLog);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.boot.web.reactive.result.view;
import java.time.Duration;
import org.junit.Before;
import org.junit.Test;
@@ -46,12 +48,15 @@ public class MustacheViewResolverTests {
@Test
public void resolveNonExistent() {
assertThat(this.resolver.resolveViewName("bar", null).block()).isNull();
assertThat(
this.resolver.resolveViewName("bar", null).block(Duration.ofSeconds(30)))
.isNull();
}
@Test
public void resolveExisting() {
assertThat(this.resolver.resolveViewName("template", null).block()).isNotNull();
assertThat(this.resolver.resolveViewName("template", null)
.block(Duration.ofSeconds(30))).isNotNull();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.boot.web.reactive.result.view;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Collections;
import com.samskivert.mustache.Mustache;
@@ -59,9 +60,9 @@ public class MustacheViewTests {
view.setCharset(StandardCharsets.UTF_8.displayName());
view.setApplicationContext(this.context);
view.render(Collections.singletonMap("World", "Spring"), MediaType.TEXT_HTML,
this.exchange).block();
assertThat(this.exchange.getResponse().getBodyAsString().block())
.isEqualTo("Hello Spring");
this.exchange).block(Duration.ofSeconds(30));
assertThat(this.exchange.getResponse().getBodyAsString()
.block(Duration.ofSeconds(30))).isEqualTo("Hello Spring");
}
}

View File

@@ -102,7 +102,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
.contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromObject("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
assertThat(result.block()).isEqualTo("Hello World");
assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
assertThat(this.webServer.getPort()).isEqualTo(specificPort);
}
@@ -131,7 +131,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
Mono<String> result = client.post().uri("/test").contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromObject("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
assertThat(result.block()).isEqualTo("Hello World");
assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
}
protected ReactorClientHttpConnector buildTrustAllSslConnector() {
@@ -187,7 +187,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
Mono<String> result = client.post().uri("/test").contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromObject("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
assertThat(result.block()).isEqualTo("Hello World");
assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
}
@Test
@@ -245,7 +245,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
public void compressionOfResponseToGetRequest() {
WebClient client = prepareCompressionTest();
ResponseEntity<Void> response = client.get().exchange()
.flatMap((res) -> res.toEntity(Void.class)).block();
.flatMap((res) -> res.toEntity(Void.class)).block(Duration.ofSeconds(30));
assertResponseIsCompressed(response);
}
@@ -253,7 +253,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
public void compressionOfResponseToPostRequest() {
WebClient client = prepareCompressionTest();
ResponseEntity<Void> response = client.post().exchange()
.flatMap((res) -> res.toEntity(Void.class)).block();
.flatMap((res) -> res.toEntity(Void.class)).block(Duration.ofSeconds(30));
assertResponseIsCompressed(response);
}
@@ -264,7 +264,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
compression.setMinResponseSize(3001);
WebClient client = prepareCompressionTest(compression);
ResponseEntity<Void> response = client.get().exchange()
.flatMap((res) -> res.toEntity(Void.class)).block();
.flatMap((res) -> res.toEntity(Void.class)).block(Duration.ofSeconds(30));
assertResponseIsNotCompressed(response);
}
@@ -274,7 +274,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
compression.setMimeTypes(new String[] { "application/json" });
WebClient client = prepareCompressionTest(compression);
ResponseEntity<Void> response = client.get().exchange()
.flatMap((res) -> res.toEntity(Void.class)).block();
.flatMap((res) -> res.toEntity(Void.class)).block(Duration.ofSeconds(30));
assertResponseIsNotCompressed(response);
}
@@ -285,7 +285,8 @@ public abstract class AbstractReactiveWebServerFactoryTests {
compression.setExcludedUserAgents(new String[] { "testUserAgent" });
WebClient client = prepareCompressionTest(compression);
ResponseEntity<Void> response = client.get().header("User-Agent", "testUserAgent")
.exchange().flatMap((res) -> res.toEntity(Void.class)).block();
.exchange().flatMap((res) -> res.toEntity(Void.class))
.block(Duration.ofSeconds(30));
assertResponseIsNotCompressed(response);
}