[Twisted-Python] deferLater with trial issue
Hi, I have a class that needs to kick off a method the repeatedly gets call every so many seconds, lets say 2 seconds. I chose to use task.deferLater to do this, but seems like LoopingCall gives similar results.. I've boiled it down to the following example
from twisted.internet import reactor, task from twisted.trial import unittest
class MyLoop(object): def __init__(self): self.updateParameters() def updateParameters(self): d = task.deferLater(reactor, 2, self.dosomething) def dosomething(self): print "cool" class LoopTestCase(unittest.TestCase): def setUp(self): self.cL = MyLoop() def test_dummy(self): d = defer.Deferred() d.addCallback(lambda x: x) d.callback(None) return d # return a deferred just in case but happens either way
MyLoop works when running normally with a reactor.run(), but when I try run this test, I get the following error:
[ERROR] Traceback (most recent call last): Failure: twisted.trial.util.DirtyReactorAggregateError: Reactor was unclean. DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug) <DelayedCall 0x1018fbb90 [1.99891901016s] called=0 cancelled=0 Deferred.callback(None)> So I don't fully understand what's going on to cause this. Any pointers would be appreciated Thanks, -Nick
You're supposed to clean up whatever junk you left in the reactor after your test. Do that in the tearDown method. cheers lvh On 23 May 2012, at 23:30, Conway, Nicholas J wrote:
Hi,
I have a class that needs to kick off a method the repeatedly gets call every so many seconds, lets say 2 seconds.
I chose to use task.deferLater to do this, but seems like LoopingCall gives similar results..
I've boiled it down to the following example
from twisted.internet import reactor, task from twisted.trial import unittest
class MyLoop(object): def __init__(self): self.updateParameters()
def updateParameters(self): d = task.deferLater(reactor, 2, self.dosomething)
def dosomething(self): print "cool"
class LoopTestCase(unittest.TestCase): def setUp(self): self.cL = MyLoop()
def test_dummy(self): d = defer.Deferred() d.addCallback(lambda x: x) d.callback(None) return d # return a deferred just in case but happens either way
MyLoop works when running normally with a reactor.run(), but when I try run this test, I get the following error:
[ERROR] Traceback (most recent call last): Failure: twisted.trial.util.DirtyReactorAggregateError: Reactor was unclean. DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug) <DelayedCall 0x1018fbb90 [1.99891901016s] called=0 cancelled=0 Deferred.callback(None)>
So I don't fully understand what's going on to cause this. Any pointers would be appreciated
Thanks,
-Nick _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On 05/23/2012 06:04 PM, Laurens Van Houtven wrote:
You're supposed to clean up whatever junk you left in the reactor after your test. Do that in the tearDown method.
An excellent way to do that is to not use the reactor for scheduling time in tests; this has the additional benefit of letting you test e.g. 2 hour timeouts without having to wait 2 hours. http://twistedmatrix.com/documents/current/core/howto/trial.html#auto9
participants (3)
-
Conway, Nicholas J -
Itamar Turner-Trauring -
Laurens Van Houtven