[Twisted-Python] deferLater with trial issue

Conway, Nicholas J Nick.Conway at wyss.harvard.edu
Thu May 24 13:22:24 EDT 2012


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
>From: "Conway, Nicholas J" <Nick.Conway at wyss.harvard.edu>
>Subject: [Twisted-Python] deferLater with trial issue
>To: "twisted-python at twistedmatrix.com"
>    <twisted-python at twistedmatrix.com>
>Message-ID: <CBE2CEB2.14F47%nick.conway at wyss.harvard.edu>
>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
>
>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
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: 
>http://twistedmatrix.com/pipermail/twisted-python/attachments/20120523/3e0
>655d0/attachment.html
>
>------------------------------
>
>Message: 3
>Date: Thu, 24 May 2012 00:04:00 +0200
>From: Laurens Van Houtven <_ at lvh.cc>
>Subject: Re: [Twisted-Python] deferLater with trial issue
>To: Twisted general discussion <twisted-python at twistedmatrix.com>
>Message-ID: <45D427B0-5E6B-4FF0-AB56-9A4F85FE7A0A at lvh.cc>
>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
>
>
>
>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 at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
>
>
>------------------------------
>
>Message: 4
>Date: Wed, 23 May 2012 19:13:35 -0400
>From: Itamar Turner-Trauring <itamar at itamarst.org>
>Subject: Re: [Twisted-Python] deferLater with trial issue
>To: twisted-python at twistedmatrix.com
>Message-ID: <4FBD6F1F.4030607 at itamarst.org>
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>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
>
>
>
>------------------------------
>
>_______________________________________________
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com
>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
>End of Twisted-Python Digest, Vol 98, Issue 16
>**********************************************




More information about the Twisted-Python mailing list