コード例 #1
0
class TestConnectionCount(unittest.TestCase):
    layer = SMTPLayer

    def setUp(self):
        self.connection = Connection(config.mta.smtp_host,
                                     int(config.mta.smtp_port), 0)
        self.msg_text = """\
From: [email protected]
To: [email protected]
Subject: aardvarks

"""

    def test_count_0(self):
        # So far, no connections.
        self.assertEqual(SMTPLayer.smtpd.get_connection_count(), 0)

    def test_count_1(self):
        self.connection.sendmail('*****@*****.**', ['*****@*****.**'],
                                 self.msg_text)
        self.assertEqual(SMTPLayer.smtpd.get_connection_count(), 1)

    def test_count_2(self):
        self.connection.sendmail('*****@*****.**', ['*****@*****.**'],
                                 self.msg_text)
        self.connection.quit()
        self.connection.sendmail('*****@*****.**', ['*****@*****.**'],
                                 self.msg_text)
        self.connection.quit()
        self.assertEqual(SMTPLayer.smtpd.get_connection_count(), 2)

    def test_count_2_no_quit(self):
        self.connection.sendmail('*****@*****.**', ['*****@*****.**'],
                                 self.msg_text)
        self.connection.sendmail('*****@*****.**', ['*****@*****.**'],
                                 self.msg_text)
        self.connection.quit()
        self.assertEqual(SMTPLayer.smtpd.get_connection_count(), 1)

    def test_count_reset(self):
        self.connection.sendmail('*****@*****.**', ['*****@*****.**'],
                                 self.msg_text)
        self.connection.quit()
        self.connection.sendmail('*****@*****.**', ['*****@*****.**'],
                                 self.msg_text)
        self.connection.quit()
        # Issue the fake SMTP command to reset the count.
        client = SMTP()
        client.connect(config.mta.smtp_host, int(config.mta.smtp_port))
        client.docmd('RSET')
        self.assertEqual(SMTPLayer.smtpd.get_connection_count(), 0)