[Twisted-Python] Testing with trial, adbapi questions

Brendon Colby brendoncolby at gmail.com
Mon Jun 25 15:57:50 EDT 2007


On 6/25/07, Christian Simms <christian.simms at gmail.com> wrote:
> I think your biggest hurdle is that you need to get used to using Deferred's
> in adbapi and any functions/methods that call adbapi indirectly.  Unless you
> use a fast embedded database like Sqlite, you have to use Deferred's because
> there's network traffic under the covers to the database server. I'll try to
> include a little advice below for each item:

Yep this is precisely what I am attempting to do - get used to using
deferreds (properly).

> I think you have a race condition because (I assume) self.p.lineReceived
> indirectly did a database insert, and then the self.check did a database
> select.  If that's true, you should make your test use Deferred's like this:

No - I'm just doing a series of runQuerys() when the server starts,
the callbacks of which populate dictionaries with objects. The test
sends a string identifier to the server which checks whether or not it
exists in one of these dictionaries. When the test runs, the dict is
empty. However, the load method IS running, just (seemingly) after the
test is run. I guess I'm not sure how to code a test for this, as in,
"after the database connection has been made and the data loaded, THEN
start running tests."

>   d = defer.succeed(None)
>   d.addCallback(lambda out: self.p.lineReceived('{%s:"PG"}\0' %
> sserverson.base10toN(104))
>   d.addCallback(lambda out: self.check("test\0"))
>   return d
>
> You could also write this code using the new defer.inlineCallbacks (needs
> python 2.5) or the older defer.deferredGenerator, but you might as well get
> used to using callbacks/errbacks and Deferred's.

Hrm yes, I'm using Debian stock 2.4, but wouldn't be opposed to hand
installing a few things (like twisted). I've done it many times. A
quick check of deferredGenerator and inlineCallbacks...these look
_exactly_ like what I'm asking for. I might just stick with 2.4 and
deferredGenerator.

> You're right that you need a new paradigm, like this:
>
>   def cb(data):
>        # use the data here, in the callback function
>   return getSomethingFromDB().addCallback(cb)
>
> This assumes getSomethingFromDB() returns a Deferred which fires with the
> result you wanted.

Yep, that's basically exactly what I'm doing now.

> You really need to run your twisted app with twistd.  That's the abstraction
> for launching twisted programs -- i.e., you define a variable named
> "application" in your .tac file and then use twistd to start your app.  Of
> course, there's nothing stopping you from using daemontools on top of it.

Yep, that's exactly what I had considered doing. It doesn't appear to
difficult to convert to using twistd run out of daemontools.

Thanks!

Brendon




More information about the Twisted-Python mailing list