Skip to content

Commit

Permalink
Test that a broken mailer implementation's exceptions don't abort tra…
Browse files Browse the repository at this point in the history
…nsactions if they happen too late. Add tests for a new 'vote' method that allows a mailer to tell the delivery implementation that it knows it will be unable to proceed.
  • Loading branch information
MatthewWilkes committed Jun 29, 2010
1 parent a45168d commit e66ef1b
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/zope/sendmail/tests/test_delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def __init__(self, *args, **kw):

def send(self, fromaddr, toaddrs, message):
self.sent_messages.append((fromaddr, toaddrs, message))

abort = None
vote = None


class TestMailDataManager(TestCase):
Expand Down Expand Up @@ -137,7 +140,59 @@ def testSend(self):
self.assertEquals(mailer.sent_messages, [])
transaction.abort()
self.assertEquals(mailer.sent_messages, [])

def testBrokenMailerErrorsAreEaten(self):
from zope.sendmail.delivery import DirectMailDelivery
mailer = BrokenMailerStub()
delivery = DirectMailDelivery(mailer)
fromaddr = 'Jim <jim@example.com'
toaddrs = ('Guido <guido@example.com>',
'Steve <steve@examplecom>')
opt_headers = ('From: Jim <jim@example.org>\n'
'To: some-zope-coders:;\n'
'Date: Mon, 19 May 2003 10:17:36 -0400\n'
'Message-Id: <20030519.1234@example.org>\n')
message = ('Subject: example\n'
'\n'
'This is just an example\n')

msgid = delivery.send(fromaddr, toaddrs, opt_headers + message)
try:
transaction.commit()
finally:
# Clean up after ourselves
transaction.abort()

def testRefusingMailerDiesInVote(self):
from zope.sendmail.delivery import DirectMailDelivery
mailer = RefusingMailerStub()
delivery = DirectMailDelivery(mailer)
fromaddr = 'Jim <jim@example.com'
toaddrs = ('Guido <guido@example.com>',
'Steve <steve@examplecom>')
opt_headers = ('From: Jim <jim@example.org>\n'
'To: some-zope-coders:;\n'
'Date: Mon, 19 May 2003 10:17:36 -0400\n'
'Message-Id: <20030519.1234@example.org>\n')
message = ('Subject: example\n'
'\n'
'This is just an example\n')

msgid = delivery.send(fromaddr, toaddrs, opt_headers + message)
try:
transaction.commit()
except:
if transaction.get()._voted:
# We voted for commit then failed, reraise
raise
else:
# We vetoed a commit, that's good.
pass
else:
self.fail("Did not raise an exception in vote")
finally:
# Clean up after ourselves
transaction.abort()

class MaildirWriterStub(object):

Expand Down Expand Up @@ -219,7 +274,24 @@ def __init__(self, *args, **kw):

def send(self, fromaddr, toaddrs, message):
raise BizzarreMailError("bad things happened while sending mail")

vote = None
abort = None


class RefusingMailerStub(object):

implements(IMailer)
def __init__(self, *args, **kw):
pass

def vote(self, fromaddr, toaddrs, message):
raise BizzarreMailError("bad things happened while sending mail")

def send(self, fromaddr, toaddrs, message):
return

abort = None

class SMTPResponseExceptionMailerStub(object):

Expand Down

0 comments on commit e66ef1b

Please sign in to comment.