[Twisted-web] NEWBIE: Unittest hangs by deferred
Andrew Bennetts
twisted-web@twistedmatrix.com
Sat, 10 Jan 2004 03:09:56 +1100
On Fri, Jan 09, 2004 at 04:21:55PM +0100, Bart Frackiewicz wrote:
[...]
>
> When i run this from bash, this works as excepted.
>
> ### file: city.py ###
> import twisted_rpc3
>
> def printResult(res):
> print res
>
> c = twisted_rpc3.City()
> c.getCityId(('berlin', 'de')).addCallback(printResult)
>
> reactor.callLater(2, reactor.stop)
> reactor.run()
Gah! You're assuming that the result will arrive in 2 seconds... you
probably should do that like this:
----
# ...
from twisted.python import log
c = twisted_rpc3.City()
d = c.getCityId(('berlin', 'de'))
d.addCallback(printResult)
d.addErrback(log.err)
d.addBoth(lambda _: reactor.stop())
reactor.run()
----
Which will wait until the result (or an error, which will be logged)
arrives, and only then stops. Much better than hard-coding a response
time.
Anyway, that's not your problem, but I just felt compelled to point that
out.
> ### endfile: city.py ###
>
> For the unittest, i create following:
>
> ## file test_twisted_rpc3.py ###
> from twisted.trial import unittest
>
> class TestCaseCity(unittest.TestCase):
>
> def testObject(self):
> c = twisted_rpc3.City()
>
> def testGetCityId(self):
> c = twisted_rpc3.City()
> d = c.getCityId(('Berlin', 'de'))
> self.assertEquals(unittest.deferredResult(d, 5), 'de_berlin')
> ## endfile test_twisted_rpc3.py ###
>
> After starting test ([~/twisted]$ trial test_twisted_rpc3) , i get an
> timeout error, and if i add print statements, i see that getCityId() was
> called, but _searchCityByString() never. Where is my mistake?
I don't see any obvious problems here... but is it possible the dbpools from
the various tests are interfering with each other? So, if you instruct
trial to just run that one test method, i.e.:
trial test_twisted_rpc3.TestCaseCity.testGetCityId
does that work?
If so, perhaps make sure you call .close on the dbpool the your City objects
create before the test is torn down?
(Should trial report on shutdown triggers that are left over after running a
test? They might be a sign of resources not being properly cleaned up after
a test.)
Hmm, actually, because tests never call reactor.run (only reactor.iterate),
it's possible that you need to call dbpool.start() manually in your test.
That would explain your symptoms: the test times out because the dbpool
isn't actually running yet.
-Andrew.