question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestClient.request does not honor timeout

See original GitHub issue

Checklist

  • The bug is reproducible against the latest release and/or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

timeout parameter exposed in testclient.request has no effect. Looking through the code, it seems as though we use the same interface as requests package but only a subset of those features are supported by testclient

To reproduce

import time

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.testclient import TestClient

mock_service = Starlette()


@mock_service.route("/slow_response")
def slow_response(request):
    time.sleep(0.01)
    return JSONResponse({"mock": "slow example"})


def test_timeout():
    """ no timeout exception raised -- this test passes when it should fail """
    client = TestClient(mock_service, raise_server_exceptions=True)
    response = client.get("/slow_response", timeout=0.001)
    assert response.json() == {"mock": "slow example"}

Expected behavior

A TimeoutException should be raised if the timeout specified in request is shorter than the time the request takes.

Actual behavior

No timeout exceptions are raised when setting timeout.

Additional context

In CI/CD context, testing our own endpoints which, during integration testing, may timeout, is very helpful. If this is unsupported, it might be nice to have a list of differences between the interface of requests/testclient (this might include #1102).

I did look a bit how the requests package implemented their timeout exceptions, and to be completely honest I got a bit lost going down the rabbit hole of requests -> urllib3 -> http.client. Since we’re using a different adapter for the testclient, it might be some work to reimplement all the way down the timeout exceptions in the same way.

I did notice that the async_asgi_testclient works with a timeout. Of course, it requires an async endpoint. But their implementation might still be useful?

Found that httpx had a similar issue with timeout having no effect when mounting an app with its client.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kludexcommented, Sep 6, 2022

I have a case where a “timeout” would be useful in testing how the server handles disconnecting clients. So a “timeout” where the client times out immediately after sending the request would be useful and should not be flaky since no actual timing is involved.

This may help: https://github.com/encode/starlette/commit/25a52fe52a1fa4f55da4096fb0c7e6f6de0ae980

1reaction
tomchristiecommented, Feb 8, 2022

Okay - that comment is a bit brief. But see #1109. Frameworks such as Flask and Django have always had this constraint. It’s something that we can live with as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

27112 (django.test.client.Client doesn't support timeouts)
I'm trying to test my middleware for replay processing (same request was issued twice, because first attempt failed due to client timeout).
Read more >
Unit testing HttpClient with timeout - Stack Overflow
In my app I am using a HttpClient, and I have to set Timeout, as the app has to upload images. But how...
Read more >
Changes — Werkzeug Documentation (2.2.x)
The test client returns TestResponse instances and can no longer be treated as a tuple. All data is available as properties on the...
Read more >
Testing - Spring
The WebTestClient builds on the mock request and response to provide support for testing WebFlux applications without an HTTP server. The client ...
Read more >
Changelog — aiohttp 3.8.1 documentation
You are not reading the most recent version of this documentation. v3.8.3 is the ... Fixed HTTP client requests to honor no_proxy environment...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found