[Twisted-Python] Creating a new selectable (for Pg async interface)

Andrew Bennetts andrew-twisted at puzzling.org
Sun Dec 15 20:05:49 EST 2002

On Mon, Dec 16, 2002 at 12:47:22AM +0100, Gerhard H?ring wrote:
> > > Next question: I'd like to try my code out. Is there a good way to
> > > test this under Twisted, apart from writing a little server like the
> > > Echo example of the docs, and trying to call PgAsyncConn from it?
> > 
> > Well, look at how we do tests in twisted.test - you can write a unit
> > test suite for your code, with the limitation of course that you'd
> > need a live postgresql server to test it.
> Uh, this seems hard. I believe I'd need to start a reactor in my test
> case, as this is a client-side API only and there's no way I'll just
> quickly implement PostgreSQL in Python. Any hints on how to do this?

Starting a reactor is easy:
    from twisted.internet import reactor
    # Make connections, schedule events, etc, here.

Of course, there's the problem of stopping the reactor.  Most of our test
cases do something more like:
    from twisted.internet import reactor
    self.done = 0
    # Do stuff
    while not self.done:

Then it's just a matter of making sure you set self.done when the test
passes or fails.  You could also set timeouts with something like
"reactor.callLater(3, self.blowUp)", or by calling "d.setTimeout(3,
self.blowUp)" on an appropriate Deferred.  You should make sure you cancel
unfired timeouts, though, or they will interfere with later tests -- this is
a deficiency in the testing framework that we're hoping to fix at some

As to starting PostgreSQL, I'd just make the test assume that PostgreSQL is
running.  Perhaps use the standard synchronous interface to test that it's
running, otherwise skip the tests?


More information about the Twisted-Python mailing list