Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/com/izeye/throwaway/TestControllerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.izeye.throwaway; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
import org.springframework.http.client.reactive.ReactorClientHttpConnector; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import io.netty.channel.ChannelOption; | ||
import io.netty.handler.timeout.ReadTimeoutException; | ||
import io.netty.handler.timeout.ReadTimeoutHandler; | ||
import org.junit.jupiter.api.Test; | ||
import reactor.netty.http.client.HttpClient; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
/** | ||
* Tests for {@link TestController}. | ||
* | ||
* @author Johnny Lim | ||
*/ | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class TestControllerTests { | ||
|
||
private final WebClient webClient; | ||
private final int port; | ||
|
||
TestControllerTests(@Autowired WebClient.Builder webClientBuilder, @LocalServerPort int port) { | ||
HttpClient httpClient = HttpClient.create() | ||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 500) | ||
.doOnConnected((connection) -> connection.addHandlerLast( | ||
new ReadTimeoutHandler(500, TimeUnit.MILLISECONDS))); | ||
this.webClient = webClientBuilder.clientConnector(new ReactorClientHttpConnector(httpClient)).build(); | ||
this.port = port; | ||
} | ||
|
||
@Test | ||
void firstDelay() { | ||
String uri = "http://localhost:" + this.port + "/test/first_delay"; | ||
assertThatThrownBy(() -> this.webClient.get().uri(uri).retrieve().bodyToMono(String.class).block()) | ||
.isInstanceOf(ReadTimeoutException.class); | ||
} | ||
|
||
@Test | ||
void firstDelayWithRetry() { | ||
String uri = "http://localhost:" + this.port + "/test/first_delay"; | ||
String response = this.webClient.get().uri(uri).retrieve().bodyToMono(String.class).retry().block(); | ||
assertThat(response).isEqualTo("Completed."); | ||
} | ||
|
||
} |