Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27112 closed New feature (wontfix)

django.test.client.Client doesn't support timeouts

Reported by: roboslone Owned by: nobody
Component: Testing framework Version: 1.10
Severity: Normal Keywords: testing, timeout
Cc: me@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by roboslone)

I'm trying to test my middleware for replay processing (same request was issued twice, because first attempt failed due to client timeout). I need some control over timeouts in django.test.client.Client. Would it be possible to implement requests-like (http://docs.python-requests.org/en/master/user/advanced/#timeouts) kwarg for timeout?

Change History (6)

comment:1 by roboslone, 8 years ago

Description: modified (diff)

comment:2 by Tim Graham, 8 years ago

Resolution: wontfix
Status: newclosed

Have you considered using mocking to raise a timeout exception? I don't think requiring the run time of a test suite to be increased by the duration of a timeout is a ideal.

Feel free to ask on our support channels and/or the DevelopersMailingList if you need other ideas or if you want to propose this feature, but it doesn't seem needed to me at first glance.

in reply to:  2 comment:3 by roboslone, 8 years ago

Replying to timgraham:

Have you considered using mocking to raise a timeout exception? I don't think requiring the run time of a test suite to be increased by the duration of a timeout is a ideal.

Feel free to ask on our support channels and/or the DevelopersMailingList if you need other ideas or if you want to propose this feature, but it doesn't seem needed to me at first glance.

I need to simulate timeout on client side, not on server side, that's the whole point. My application is hosted on several machines behind load balancer and I need to test app's behaviour under balancer's timeouts/retries.

comment:4 by Tim Graham, 8 years ago

Oh, sorry, I have no idea about that. If you provided an implementation that would help the discussion as I really have no idea if this is a good idea or even feasible. The mailing list is a better for discussion since more people are reading that than this ticket tracker.

in reply to:  4 comment:5 by roboslone, 8 years ago

Replying to timgraham:

Oh, sorry, I have no idea about that. If you provided an implementation that would help the discussion as I really have no idea if this is a good idea or even feasible. The mailing list is a better for discussion since more people are reading that than this ticket tracker.

requests uses python's Queue (https://docs.python.org/3.5/library/queue.html#queue.Queue.get) for that.
I will write on mailing list, thanks for the link. Could you remove "wontfix" for now?

Version 0, edited 8 years ago by roboslone (next)

comment:6 by roboslone, 8 years ago

To anyone with similar use case, I was able to test client timeouts with modified LiveServerTestCase. Discussion can be found in this thread: https://groups.google.com/forum/#!topic/django-developers/PofmsTQ-YGs

tl;dr
Use following code to define ThreadedLiveServerTestCase:

from django.test.testcases import LiveServerThread, QuietWSGIRequestHandler
from django.core.servers.basehttp import WSGIServer
from socketserver import ThreadingMixIn

class ThreadedWSGIServer(ThreadingMixIn, WSGIServer):
    pass


class ThreadedLiveServerThread(LiveServerThread):
    def _create_server(self, port):
        return ThreadedWSGIServer((self.host, port), QuietWSGIRequestHandler)


class ThreadedLiveServerTestCase(LiveServerTestCase):
    @classmethod
    def _create_server_thread(cls, host, possible_ports, connections_override):
        return ThreadedLiveServerThread(
            host,
            possible_ports,
            cls.static_handler,
            connections_override=connections_override,
        )

And use requests to query backends:

import requests
from requests.exceptions import Timeout

...

with self.assertRaises(Timeout):
    requests.get(self.live_server_url + '/something', timeout=1.0)
Note: See TracTickets for help on using tickets.
Back to Top