[Twisted-Python] Simple way to halt all timers?
Is there a simple way to cancel all Twisted timers (pending reactor.callLater calls)? The reason I want to do this is to clean up a unit test without getting "dirty reactor" complaints. The system is a server that will rarely be shut down and does not have to shut down gracefully (i.e. I don't care if the reactor is dirty). But I want the unit tests to run cleanly, so for now I've been keeping a reference to each timer that I start, so my test cleanup code can manually cancel it. It's a lot of fuss, and makes timers harder to use than they should be. I admit that in many cases I need a reference to the timer anyway, since I may have to cancel it for reasons that matter to the server. But I hate having to do it for *every* timer. If there was a simple way to tell the reactor to cancel any outstanding "callLater" calls, the code would be much cleaner. -- Russell
Hi Russell,
The way to do this is to pass the reactor as a parameter, and use a Clock
instead of the regular parameter for unit testing purposes. See also:
https://twistedmatrix.com/documents/current/core/howto/trial.html ; that
has a complete example of how to write unit tests that use Clock.
That way your unit tests will be fast, deterministic, and also very clean
:-)
hth
lvh
On Thu, Mar 13, 2014 at 10:09 PM, Russell E. Owen
Is there a simple way to cancel all Twisted timers (pending reactor.callLater calls)?
The reason I want to do this is to clean up a unit test without getting "dirty reactor" complaints.
The system is a server that will rarely be shut down and does not have to shut down gracefully (i.e. I don't care if the reactor is dirty). But I want the unit tests to run cleanly, so for now I've been keeping a reference to each timer that I start, so my test cleanup code can manually cancel it. It's a lot of fuss, and makes timers harder to use than they should be. I admit that in many cases I need a reference to the timer anyway, since I may have to cancel it for reasons that matter to the server. But I hate having to do it for *every* timer. If there was a simple way to tell the reactor to cancel any outstanding "callLater" calls, the code would be much cleaner.
-- Russell
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
In article
Hi Russell,
The way to do this is to pass the reactor as a parameter, and use a Clock instead of the regular parameter for unit testing purposes. See also: https://twistedmatrix.com/documents/current/core/howto/trial.html ; that has a complete example of how to write unit tests that use Clock.
That way your unit tests will be fast, deterministic, and also very clean :-)
Thank you. That looks very promising! -- Russell
participants (2)
-
Laurens Van Houtven
-
Russell E. Owen