'If L{MXCalculator.getMX} encounters a CNAME chain which is longer than
the length specified, the returned L{Deferred} should errback with
L{CanonicalNameChainTooLong}.'

def test_cnameChain(self):

    class DummyResolver(object, ):
        '\n                                                Fake    resolver    which    generates    a    CNAME    chain    of    infinite    length    in\n                                                response    to    MX    lookups.\n                                                '
        chainCounter = 0
        def lookupMailExchange(self, domain):
            self.chainCounter += 1
            name = ('x-%d.example.com' % (self.chainCounter,))
            return defer.succeed(([RRHeader(name=domain, type=Record_CNAME.TYPE, payload=Record_CNAME(name))], [], []))
    cnameLimit = 3
    self.mx.resolver = DummyResolver()
    d = self.mx.getMX('mail.example.com', cnameLimit)
    self.assertFailure(d, twisted.mail.relaymanager.CanonicalNameChainTooLong)
    def cbChainTooLong(error):
        self.assertEqual(error.args[0], Record_CNAME(('x-%d.example.com' % ((cnameLimit + 1),))))
        self.assertEqual(self.mx.resolver.chainCounter, (cnameLimit + 1))
    d.addCallback(cbChainTooLong)
    return d
