def test_retryAfterDisconnect(self):
        """
        If the protocol created by L{SMTPSenderFactory} loses its connection
        before receiving confirmation of message delivery, it reconnects and
        tries to deliver the message again.
        """
        recipient = 'alice'
        message = "some message text"
        domain = DummyDomain([recipient])

        class CleanSMTP(smtp.SMTP):
            """
            An SMTP subclass which ensures that its transport will be
            disconnected before the test ends.
            """
            def makeConnection(innerSelf, transport):
                self.addCleanup(transport.loseConnection)
                smtp.SMTP.makeConnection(innerSelf, transport)

        # Create a server which will fail the first message deliver attempt to
        # it with a 500 and a disconnect, but which will accept a message
        # delivered over the 2nd connection to it.
        serverFactory = MultipleDeliveryFactorySMTPServerFactory([
                BrokenMessage,
                lambda user: DummyMessage(domain, user)])
        serverFactory.protocol = CleanSMTP
        serverPort = reactor.listenTCP(0, serverFactory, interface='127.0.0.1')
        serverHost = serverPort.getHost()
        self.addCleanup(serverPort.stopListening)

        # Set up a client to try to deliver a message to the above created
        # server.
        sentDeferred = defer.Deferred()
        clientFactory = smtp.SMTPSenderFactory(
            "bob@example.org", recipient + "@example.com",
            StringIO(message), sentDeferred)
        clientFactory.domain = "example.org"
        clientConnector = reactor.connectTCP(
            serverHost.host, serverHost.port, clientFactory)
        self.addCleanup(clientConnector.disconnect)

        def cbSent(ignored):
            """
            Verify that the message was successfully delivered and flush the
            error which caused the first attempt to fail.
            """
            self.assertEqual(
                domain.messages,
                {recipient: ["\n%s\n" % (message,)]})
            # Flush the RuntimeError that BrokenMessage caused to be logged.
            self.assertEqual(len(self.flushLoggedErrors(RuntimeError)), 1)
        sentDeferred.addCallback(cbSent)
        return sentDeferred
dataset/ETHPy150Open nlloyd/SubliminalCollaborator/libs/twisted/mail/test/test_smtp.py/SMTPSenderFactoryRetryTestCase.test_retryAfterDisconnect/original