[Twisted-Python] Trial: Blocking until all deferreds have fired in test

Mark Visser markv at lumierevfx.com
Fri Sep 25 15:47:00 EDT 2009


I have a class that looks something like this:

|class FooListener(object):
    def __init__(self):
        pass

    @inlineCallbacks
    def fooBar():
        try:
            yield doSomeSetup()
            doSomethingThatReturnsADeferred()
            returnValue('success!')
        except:
            returnValue('failure!')

|Note that doSomethingThatReturnsADeferred() has no callbacks.

I also have a unit test that looks something like this:

|def TestFoo(unittest.TestCase):
    def setUp(self):
        timeout = 1000000
        self.listener = FooListener()

    @inlineCallbacks
    def testFooBar(self):
        yield self.listener.fooBar()
|
When I run this, I get this error:

[ERROR]: foo.TestFoo.testFooBar

Traceback (most recent call last):
Failure: twisted.trial.util.DirtyReactorAggregateError: Reactor was unclean.
Selectables:
<twisted.internet.process.ProcessWriter object at 0x3b7b650>
-------------------------------------------------------------------------------
Ran 1 tests in 9.028s

FAILED (errors=1, successes=1)

I'm clever enough to understand that Trial isn't waiting for the 
"backgrounded" Deferreds to fire, but I'm not clever enough to figure 
how to fix it. :-)

I could hack around this by passing a test_mode parameter to 
TestFoo.__init__ and "yield"-ing doSomethingThatReturnsADeferred(), but 
I'd rather not introduce special cases for testing.

Is there a way to instruct Trial to "wait" for Deferreds to fire?

thanks

-- 
Mark Visser, Software Director
Lumière VFX
Email: markv at lumierevfx.com
Phone: +1-514-316-1080 x3030

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090925/67b19849/attachment.htm 


More information about the Twisted-Python mailing list