A read timeout bounds how long this client waits for a response after its connection has reached the loopback server.
Spring RestClient read timeout: reject a server that accepts but stalls
Hold the response on purpose
The local server accepts GET /risk/receipt-2048 and waits on a latch instead of sending headers. The client uses an 80ms read timeout. RestClientBoundaryTest expects ResourceAccessException and confirms that the server handler received the request. Only then does it release the handler. A second latch makes the test about response waiting rather than an accidental connection failure.
The selected SimpleClientHttpRequestFactory has separate connect and read timeout settings. The test checks the latter. It does not check DNS resolution, connection-pool acquisition or a response body that trickles bytes without completing.
The remote side may still work
When the client stops waiting, the server-side operation is not automatically cancelled. The handler may later write to a closed socket. For a GET that is usually a wasted computation; for a POST it can be a committed mutation with no received response. The write fixture makes that ambiguity visible.
A read timeout is not the same as the public endpoint deadline. Add connection wait, serialization, transaction time and any retry delay before choosing a number. The 80ms fixture exists to make the test quick and deterministic; it is not a production recommendation.
Checked source
assertThrows(ResourceAccessException.class, () -> client(Duration.ofMillis(80)).get()
.uri("/risk/receipt-2048").retrieve().body(String.class));
assertTrue(requestReceived.await(1, TimeUnit.SECONDS));Verification boundary
RestClientBoundaryTest.readTimeoutStopsWaitingForAStalledResponse.
Costs and limits
Only a local server that delays response headers is checked. Slow streaming, DNS, proxy and TLS behavior remain untested.
Common Mistakes
- Do not treat a read timeout as evidence that the remote operation stopped.
- Do not confuse a stalled response with a refused connection.
- Do not copy the short test timeout into a production service without measurement.
Read next
Spring RestClient transport: run the factory you configured, Spring HTTP timeout after POST: the write may already exist, Spring HTTP client timeouts: bound the call inside the request deadline, Spring RestClient 503 response: count attempts before adding a retry.
