'L{IReactorTCP.connectTCP} calls C{Factory.clientConnectionFailed} with
L{error.ConnectBindError} if the bind address specified is already in
use.'

def test_clientBind(self):

    theDeferred = defer.Deferred()
    sf = MyServerFactory()
    sf.startFactory = self._fireWhenDoneFunc(theDeferred, sf.startFactory)
    p = reactor.listenTCP(0, sf, interface='127.0.0.1')
    self.addCleanup(p.stopListening)
    def _connect1(results):
        d = defer.Deferred()
        cf1 = MyClientFactory()
        cf1.buildProtocol = self._fireWhenDoneFunc(d, cf1.buildProtocol)
        reactor.connectTCP('127.0.0.1', p.getHost().port, cf1, bindAddress=('127.0.0.1', 0))
        d.addCallback(_conmade, cf1)
        return d
    def _conmade(results, cf1):
        d = defer.Deferred()
        cf1.protocol.connectionMade = self._fireWhenDoneFunc(d, cf1.protocol.connectionMade)
        d.addCallback(_check1connect2, cf1)
        return d
    def _check1connect2(results, cf1):
        self.assertEqual(cf1.protocol.made, 1)
        d1 = defer.Deferred()
        d2 = defer.Deferred()
        port = cf1.protocol.transport.getHost().port
        cf2 = MyClientFactory()
        cf2.clientConnectionFailed = self._fireWhenDoneFunc(d1, cf2.clientConnectionFailed)
        cf2.stopFactory = self._fireWhenDoneFunc(d2, cf2.stopFactory)
        reactor.connectTCP('127.0.0.1', p.getHost().port, cf2, bindAddress=('127.0.0.1', port))
        d1.addCallback(_check2failed, cf1, cf2)
        d2.addCallback(_check2stopped, cf1, cf2)
        dl = defer.DeferredList([d1, d2])
        dl.addCallback(_stop, cf1, cf2)
        return dl
    def _check2failed(results, cf1, cf2):
        self.assertEqual(cf2.failed, 1)
        cf2.reason.trap(error.ConnectBindError)
        self.assertTrue(cf2.reason.check(error.ConnectBindError))
        return results
    def _check2stopped(results, cf1, cf2):
        self.assertEqual(cf2.stopped, 1)
        return results
    def _stop(results, cf1, cf2):
        d = defer.Deferred()
        d.addCallback(_check1cleanup, cf1)
        cf1.stopFactory = self._fireWhenDoneFunc(d, cf1.stopFactory)
        cf1.protocol.transport.loseConnection()
        return d
    def _check1cleanup(results, cf1):
        self.assertEqual(cf1.stopped, 1)
    theDeferred.addCallback(_connect1)
    return theDeferred
