[Twisted-Python] Getting rid of "DirtyReactorWarning"

Itamar Turner-Trauring itamar at itamarst.org
Tue May 15 04:26:24 MDT 2012

I have a ticket open for allowing tests that look like this, where each 
run has a new reactor instance:

def test_somethingShouldHappen(self, reactor):
      result = []
      self.assertEqual(result, ["hooray"])

Further thought suggests however that this doesn't add much, 
necessarily, over the current "return a Deferred" idiom for most users. 
Experience with ReactorBuilder framework that has similar testing 
mechanism suggests it's far too easy to fail to stop the reactor. 
Moreover, things like coiterate() don't work since they rely on 
pre-imported global reactor, and given the "from twisted.internet import 
reactor" idiom this testing style won't work well for many people's code.

But! ReactorBuilder tests are superior in one way: no 
DirtyReactorWarning. You don't have to wait until all connections are 
closed, which is difficult, or make sure to cancel every timeout. This 
makes test writing simpler.

Perhaps we should get rid of DirtyReactorWarning for regular 
Deferred-returning tests as well, then. We have all the infrastructure 
we need, after all, for canceling scheduled events and disconnecting all 
descriptors. We might want to log when we do so, to help debug things, 
but we already have cleanup code mostly implemented in trial anyway, and 
it seems like DirtyReactorWarning doesn't usually make future tests fail 
anyway since we do run cleanup.

Threaded code might still be a problem, but that's the case for current 
tests now, and even switching to a new style of tests as above would 
only somewhat mitigate the issue.


More information about the Twisted-Python mailing list