[Twisted-Python] Using a custom reactor in twisted trial for test cases?

Paul Thomas spongelavapaul at googlemail.com
Mon Nov 2 08:14:37 EST 2009

On 2 Nov 2009, at 07:22, Crispin Wellington wrote:

> On Mon, 2009-11-02 at 01:12 -0500, Glyph Lefkowitz wrote:
>> On Nov 1, 2009, at 11:17 PM, Crispin Wellington wrote:
>>> I tried using LoopingCall, but it does not work. It only calls the
>>> scheduler once. I think this has to do with the fact that the
>>> stackless
>>> scheduler needs to be interwoven with the twisted reactor pump.
>> What do you mean "does not work"?
> OK. Having a closer look, its not that looping call doesn't work, its
> that there is some unknown number of "reactor pumps" between starting
> the test, and finishing it. What I need is a way for the reactor to be
> pumping away while a particular test function of a testcase continues
> working.
> Running this under trial, it just hangs, inside the while task.alive:
> loop.
> So I guess my problem is my approach. How do I test long running
> "tasklets" that use twisted calls (unlike this contrived example that
> only sleeps) within the twisted trial framework?

I'm no expert, but I have some tests against objects that use  
spawnProcess() which has the similar problem that I need to pump the  
reactor. The key is to return a deferred from the trial test method -  
then trial does the pump for you. I'm actually using @inlineCallbacks  
and the sleep() function mentioned in a previous post - you'll  
probably want task.deferLater() or similar.

More information about the Twisted-Python mailing list