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.con...@wyss.harvard.edu> >Subject: [Twisted-Python] deferLater with trial issue >To: "twisted-python@twistedmatrix.com" > <twisted-python@twistedmatrix.com> >Message-ID: <cbe2ceb2.14f47%nick.con...@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 <_...@lvh.cc> >Subject: Re: [Twisted-Python] deferLater with trial issue >To: Twisted general discussion <twisted-python@twistedmatrix.com> >Message-ID: <45d427b0-5e6b-4ff0-ab56-9a4f85fe7...@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@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 <ita...@itamarst.org> >Subject: Re: [Twisted-Python] deferLater with trial issue >To: twisted-python@twistedmatrix.com >Message-ID: <4fbd6f1f.4030...@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@twistedmatrix.com >http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python > > >End of Twisted-Python Digest, Vol 98, Issue 16 >********************************************** _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python