Discussion:
[Twisted-Python] deferLater with trial issue
Conway, Nicholas J
2012-05-23 21:30:45 UTC
Permalink
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
Laurens Van Houtven
2012-05-23 22:04:00 UTC
Permalink
You're supposed to clean up whatever junk you left in the reactor after your test. Do that in the tearDown method.

cheers
lvh
Post by Conway, Nicholas J
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
self.updateParameters()
d = task.deferLater(reactor, 2, self.dosomething)
print "cool"
self.cL = MyLoop()
d = defer.Deferred()
d.addCallback(lambda x: x)
d.callback(None)
return d # return a deferred just in case but happens either way
[ERROR]
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
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Itamar Turner-Trauring
2012-05-23 23:13:35 UTC
Permalink
Post by Laurens Van Houtven
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
Conway, Nicholas J
2012-05-24 17:22:24 UTC
Permalink
task.Clock() definitely worked for making the test pass, by making the
clock a keyword parameter in the MyLoop.__init__ for a class variable.

Additionally, I also experimented with making the deferLater defer a class
variable and I do a d.cancel() in the teardown. Even did an
self.addCleanup(self.cL.d.cancel). All worked.

I get it now. Gotta seriously clean up / cancel / shut down / stop
listening stuff in the tearDown steps.

Thanks for helping me over the conceptual hump guys.

-Nick
Message: 2
Date: Wed, 23 May 2012 17:30:45 -0400
Subject: [Twisted-Python] deferLater with trial issue
Content-Type: text/plain; charset="us-ascii"
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
self.updateParameters()
d = task.deferLater(reactor, 2, self.dosomething)
print "cool"
self.cL = MyLoop()
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
[ERROR]
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
-------------- next part --------------
An HTML attachment was scrubbed...
http://twistedmatrix.com/pipermail/twisted-python/attachments/20120523/3e0
655d0/attachment.html
------------------------------
Message: 3
Date: Thu, 24 May 2012 00:04:00 +0200
Subject: Re: [Twisted-Python] deferLater with trial issue
Content-Type: text/plain; charset=us-ascii
You're supposed to clean up whatever junk you left in the reactor after
your test. Do that in the tearDown method.
cheers
lvh
Post by Conway, Nicholas J
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
self.updateParameters()
d = task.deferLater(reactor, 2, self.dosomething)
print "cool"
self.cL = MyLoop()
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
[ERROR]
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
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
------------------------------
Message: 4
Date: Wed, 23 May 2012 19:13:35 -0400
Subject: Re: [Twisted-Python] deferLater with trial issue
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Post by Conway, Nicholas J
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
------------------------------
_______________________________________________
Twisted-Python mailing list
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
End of Twisted-Python Digest, Vol 98, Issue 16
**********************************************
Loading...