[Twisted-Python] What deferred can I use to detect when a server or client is "running"

Jean-Paul Calderone exarkun at divmod.com
Sun Dec 17 17:32:43 EST 2006

On Sun, 17 Dec 2006 15:10:34 -0700, Brian Granger <ellisonbg.net at gmail.com> wrote:
>i am writing some tests using trial and in my setUp methods I start
>various servers and clients using listenTCP/connectTCP.  The problem I
>am having is finding a deferred that will fire when:
>1.  A server is actually listening.  I want to use this deferred to
>trigger clients to connect...

listenTCP complete synchronously, so you don't need to wait before
connecting to a server started this way.

>2.  A client has connected.  I want to return this deferred from my
>setUp method so my tests won't actually run until the client has
>connected to the server.

twisted.internet.protocol.ClientCreator.connectTCP will return a Deferred
which fires with a protocol instance after it is connected.  You can use
that, or look at its implementation to see how to do something similar

>I have some hackish ways that I have been getting around this, but
>they seem a little fickle.  What is the "standard" way of doing this
>in tests?

You might also consider /not/ using real sockets in your unit tests.  It
is often easier to use a simulated transport.

twisted.protocols.loopback.loopback is going to be deprecated in Twisted
2.5, but it will be replaced with something much the same, loopbackAsync.
You can also just create twisted.test.proto_helpers.StringTransport objects
and attach them to protocols directly.  This gives you very precise control
over what you are testing.


More information about the Twisted-Python mailing list