Ticket #17: handling_of_temporary_errors.patch

File handling_of_temporary_errors.patch, 3.7 kB (added by fs, 1 year ago)
  • turbomail_tests/test_errorhandling.py

     
     1#!/usr/bin/env python 
     2# -*- coding: UTF-8 -*- 
     3 
     4# Copyright (c) 2007 Felix Schwarz <felix.schwarz@schwarz.eu> 
     5# 
     6# This code is placed under the MIT license: 
     7# 
     8# Permission is hereby granted, free of charge, to any person obtaining a copy  
     9# of this software and associated documentation files (the "Software"), to deal  
     10# in the Software without restriction, including without limitation the rights  
     11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
     12# copies of the Software, and to permit persons to whom the Software is  
     13# furnished to do so, subject to the following conditions: 
     14# 
     15# The above copyright notice and this permission notice shall be included in  
     16# all copies or substantial portions of the Software. 
     17# 
     18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
     19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
     20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
     21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
     22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
     23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  
     24# SOFTWARE. 
     25 
     26import email 
     27import logging 
     28import time 
     29import unittest 
     30 
     31logging.disable(logging.ERROR) 
     32 
     33import turbogears 
     34import turbomail 
     35 
     36from turbomail_tests.lib.smtp_mailsink import SMTPMailsink 
     37from turbomail_tests.lib.utils import get_received_mail, save_config 
     38 
     39 
     40class TestErrorHandling(unittest.TestCase): 
     41        "Test cases for error conditions and error handling inside of TurboMail." 
     42 
     43        def setUp(self): 
     44                self.server_port = 42042 
     45                test_config = {'mail.on': True, 'mail.timeout': 1, 
     46                                           'mail.server': 'localhost:%d' % self.server_port} 
     47                self._original_config = save_config(test_config.keys()) 
     48                turbogears.config.update(test_config) 
     49                self.sink = None 
     50                turbogears.startup.startTurboGears() 
     51 
     52 
     53        def tearDown(self): 
     54                turbogears.startup.stopTurboGears() 
     55                turbogears.config.update(self._original_config) 
     56                if self.sink != None: 
     57                        self.sink.stop() 
     58 
     59 
     60        def test_connection_refused(self): 
     61                """Test that a message is not lost if the mail hub was unavailable for 
     62                a short period of time.""" 
     63                message = turbomail.Message('sender@foo.example',  
     64                                                                        'recipient@foo.example', 'foo bar') 
     65                message.plain = 'Hello World!'           
     66                turbomail.enqueue(message) 
     67                time.sleep(1) 
     68                self.sink = SMTPMailsink(host='localhost', port=self.server_port) 
     69                self.sink.start() 
     70                msginfo = get_received_mail(self.sink) 
     71                msg = email.message_from_string(msginfo['mail'])  
     72                assert 'Hello World' in msg.get_payload()  
     73 
  • turbomail/turbomail/pool.py

     
    33"""Generic Pool and MailPool class definitions.""" 
    44 
    55import logging 
     6import smtplib 
     7import socket 
     8import time 
    69log = logging.getLogger("turbomail.pool") 
    710 
    811import math 
     
    192195                                self.spawn() 
    193196                                break 
    194197                         
     198                        unit = None 
    195199                        try: 
    196200                                unit = self._queue.get(True, self._timeout) 
    197201                                dispatch(unit) 
     
    199203                        except Empty: 
    200204                                log.debug("Worker death from starvation.") 
    201205                                break 
     206                        except (socket.error, smtplib.SMTPException), exception: 
     207                                log.exception(exception) 
     208                                log.error("Exception occured when sending mail. Retrying...") 
     209                                time.sleep(1) 
     210                                self._queue.put(unit, block=False) 
    202211                         
    203212                        count += 1