'Check that Protocol.connectionLost is called before factory\'s
clientConnectionLost'

def test_loseOrder(self):

    server = MyServerFactory()
    server.protocolConnectionMade = defer.Deferred().addCallback((lambda proto: self.addCleanup(proto.transport.loseConnection)))
    client = MyClientFactory()
    client.protocolConnectionLost = defer.Deferred()
    client.protocolConnectionMade = defer.Deferred()
    def _cbCM(res):
        '\n                                                protocol.connectionMade    callback\n                                                '
        reactor.callLater(0, client.protocol.transport.loseConnection)
    client.protocolConnectionMade.addCallback(_cbCM)
    port = reactor.listenTCP(0, server, interface='127.0.0.1')
    self.addCleanup(port.stopListening)
    connector = reactor.connectTCP(port.getHost().host, port.getHost().port, client)
    self.addCleanup(connector.disconnect)
    def _cbCCL(res):
        '\n                                                factory.clientConnectionLost    callback\n                                                '
        return 'CCL'
    def _cbCL(res):
        '\n                                                protocol.connectionLost    callback\n                                                '
        return 'CL'
    def _cbGather(res):
        self.assertEqual(res, ['CL', 'CCL'])
    d = defer.gatherResults([client.protocolConnectionLost.addCallback(_cbCL), client.deferred.addCallback(_cbCCL)])
    return d.addCallback(_cbGather)
