[Twisted-Python] Testing function with delayed calls

Ashwini Oruganti ashfall at twistedmatrix.com
Thu Oct 1 15:46:11 MDT 2015


On Thu, Oct 1, 2015 at 1:40 PM, Mashiat Sarker Shakkhar <
mashiat.sarker at gmail.com> wrote:

> Hi
>
> I have a function that uses callLater extensively to schedule a number of
> different tasks. I don't want to get into the rationale behind such a
> design, but here is a contrived example which will help me explain my
> problem:
>
>
>     def b():
>
>         '''Do some work'''
>
>
>     def c():
>
>         '''Do some more work'''
>
>
>     def a(flag):
>
>         if flag:
>
>             return Reactor.callLater(300, b)
>
>         else:
>
>             return Reactor. callLater(100, c)
>
>
> Now I want to test this function. Of course I can't wait for 5 minutes to
> ensure that `b` or `c` will indeed be called. What I need is some sort of
> mock clock which lets me fast forward time. Does any such thing exist in
> Twisted / Trial?
>

Yes there is! `MemoryReactorClock.advance`
https://twistedmatrix.com/documents/current/api/twisted.test.proto_helpers.MemoryReactorClock.html
does this for you.

An example usage can be seen in the tests for `HostnameEndpoint`
https://github.com/twisted/twisted/blob/trunk/twisted/internet/test/test_endpoints.py#L2064

-Ashwini




> Or is there any other approach to test such code?
>







>
> Regards
>
> Shakkhar
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20151001/19bc4706/attachment-0002.html>


More information about the Twisted-Python mailing list