Mikhail
Mikhail

Reputation: 2935

Timeout on blocking read for 5000 MILLISECONDS in Spring Webflux

I wrote a test for Handler (Spring Webflux)

@Test
public void checkServicesHandlerTest() {
    Request request = new Request();
    request.setMsisdn("ffdfdfd");
    this.testClient.post().uri("/check")
                   .body(Mono.just(request), Request.class)
                   .exchange().expectStatus().isOk();
}

But in result I have an error.

Timeout on blocking read for 5000 MILLISECONDS

The handler is simple:

 public Mono<ServerResponse> check(ServerRequest request) {
       
     Request request = request.bodyToMono(Request.class).block();

Where is the problem? If i send a direct request to server all is ok.

Upvotes: 53

Views: 61537

Answers (7)

asgarov1
asgarov1

Reputation: 4171

I was getting the same error in my tests (Timeout on blocking read for 5000 MILLISECONDS in Spring Webflux) what helped me was adding @AutoConfigureWebTestClient annotation:

@AutoConfigureWebTestClient
@EnableAutoConfiguration
@SpringBootTest(
        webEnvironment = WebEnvironment.RANDOM_PORT,
        classes = { TestConfig.class }
)
@ActiveProfiles("unit-test")
class ApiGatewayApplicationTests {

    @Autowired
    private WebTestClient webClient;

    @Test
    void testWithoutToken() {
        webClient.get()
                .uri("/foo")
                .exchange()
                .expectStatus()
                .isUnauthorized();
    }
}

I am assuming it is because when I use AutoConfigureWebTestClient it correctly binds to application context, with manual config it is easy to miss sth

Upvotes: 0

Zahra Vahidi
Zahra Vahidi

Reputation: 231

You can also use this config in your application.yml file, if you have many test classes and need the timeout to be applied on all of them.

spring:
  test:
    webtestclient:
      timeout: 50000ms

Upvotes: 1

p_sha
p_sha

Reputation: 1

Check you pom file, if BlockHound dependency is present remove it.

<dependency>
  <groupId>io.projectreactor.tools</groupId>
  <artifactId>blockhound</artifactId>
  <version>$LATEST_RELEASE</version>
</dependency>

Upvotes: -1

Trippletech Magabe
Trippletech Magabe

Reputation: 21

With the WebTestClient you can use @AutoConfigureWebTestClient(timeout = "36000"), the timeout value being a value of your choice. Especially, consider the response time of your third-party service.

Upvotes: 1

ROCKY
ROCKY

Reputation: 1933

I was seeing similar issue and Exception when running Integration tests some of them aggregates responses from multiple other services which has database access and stuff. So we were seeing this issue intermittently when running Integration tests. We are using Spring Boot 2.0.0.RC1 and Junit 5 with Gradle. I did this to resolve the issue. The key is mutating the webclient with a response timeout of 30 seconds the worst case.

@Autowired
private WebTestClient webTestClient;

@BeforeEach
public void setUp() {
    webTestClient = webTestClient.mutate()
                                 .responseTimeout(Duration.ofMillis(30000))
                                 .build();
}

Upvotes: 76

Erohin Dmitry
Erohin Dmitry

Reputation: 21

In case of Kotlin test it could be following example:

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RouterTests {
    lateinit var client: WebTestClient

    private val dealService: DealService = QshDealService()
    private val dealStream: StreamHandler<MarketData> = DealStream(dealService)
    private lateinit var fileService: FileService

    @BeforeAll
    fun init() {
        val streamHandlers: Map<String, StreamHandler<MarketData>> = mapOf("dealStream" to dealStream)
        fileService = CommonFilesService(streamHandlers)
        val filesHandler = QshFilesHandler(fileService)
        this.client = WebTestClient
            .bindToRouterFunction(QshRouter(filesHandler).route())
            .configureClient()
            .responseTimeout(Duration.ofMillis(30000))
            .build()
    }

    @Test
    fun whenRequestToRoute_thenStatusShouldBeOk() {
        ...
    }

}

Upvotes: 1

Mustapha Charboub
Mustapha Charboub

Reputation: 849

You can override the timeout by using the annotation @AutoConfigureWebTestClient(timeout = "36000").

for example :

@AutoConfigureWebTestClient(timeout = "36000")
@SpringBootTest
class MyTestClass {
}

Upvotes: 75

Related Questions