Ticket #18: testmode.patch

File testmode.patch, 4.7 kB (added by fs, 1 year ago)
  • turbomail_tests/test_testmode.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.INFO) 
     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 TestTestmode(unittest.TestCase): 
     41        "Test cases for enabling the TurboGears 'testmode'." 
     42 
     43        def setUp(self): 
     44                server_port = 42042 
     45                test_config = {'mail.on': True, 'mail.timeout': 1, 
     46                                           'mail.server': 'localhost:%d' % server_port, 
     47                                           'mail.testmode': True} 
     48                self._original_config = save_config(test_config.keys()) 
     49                turbogears.config.update(test_config) 
     50                self.sink = SMTPMailsink(host='localhost', port=server_port) 
     51                self.sink.start() 
     52                turbogears.startup.startTurboGears() 
     53 
     54 
     55        def tearDown(self): 
     56                turbogears.startup.stopTurboGears() 
     57                turbogears.config.update(self._original_config) 
     58                self.sink.stop() 
     59 
     60 
     61        def test_enable_testmode(self): 
     62                """Test that a message is not lost if the mail hub was unavailable for 
     63                a short period of time.""" 
     64                message = turbomail.Message('sender@foo.example',  
     65                                                                        'recipient@foo.example', 'foo bar') 
     66                message.plain = 'Hello World!'           
     67                turbomail.enqueue(message) 
     68                time.sleep(1) 
     69                msg = turbomail._queue._queue.get(block=False) 
     70                assert 'Hello World' in msg['message'] 
     71 
  • turbomail/turbomail/pool.py

     
    2525        method. 
    2626        """ 
    2727         
    28         def __init__(self, interval=10, threads=4, jobs=10, timeout=60, polling=False, **kw): 
     28        def __init__(self, interval=10, threads=4, jobs=10, timeout=60, polling=False, testmode=False, **kw): 
    2929                """Initialize the thread pool. 
    3030                 
    3131                @param interval: A delay, in seconds, between spawn runs. 
     
    4646                                mechanism.  Disabled, threads will be created, 
    4747                                as required, when work is enqueued. 
    4848                @type polling: bool 
     49 
     50                @param testmode: Enable or disable the unit testing mode.  
     51                                Disabled, no mail is actually sent but only 
     52                                stored in the queue for later retrieval. 
     53                @type testmode: bool 
    4954                """ 
    5055                 
    5156                super(Pool, self).__init__() 
     
    5863                self._jobs = jobs 
    5964                self._timeout = timeout 
    6065                self._polling = polling 
     66                self._testmode = testmode 
    6167                self._kw = kw 
    6268                 
    6369                log.debug("Thread pool created.") 
     
    8793                        self._queue.put(work(), block=block, timeout=timeout) 
    8894                else: 
    8995                        self._queue.put(work, block=block, timeout=timeout) 
     96 
     97                if self._testmode: 
     98                        log.debug("Testmode enabled, not sending mail.") 
     99                        return 
    90100                 
    91101                optimum_threads = min(self._threads, math.ceil(self._queue.qsize() / float(self._jobs))) 
    92102 
  • turbomail/turbomail/startup.py

     
    4646                        password=turbogears.config.get("mail.password", None), 
    4747                        debug=turbogears.config.get("mail.debug", False), 
    4848                        tls=turbogears.config.get("mail.tls", None), 
    49                         polling=turbogears.config.get("mail.polling", None) 
     49                        polling=turbogears.config.get("mail.polling", None), 
     50                        testmode=turbogears.config.get("mail.testmode", False) 
    5051                ) 
    5152         
    5253        if turbomail._queue._polling: