diff options
| author | Barry Warsaw | 2008-03-06 00:29:11 -0500 |
|---|---|---|
| committer | Barry Warsaw | 2008-03-06 00:29:11 -0500 |
| commit | 5ca899a81b547dd46197b8d51c7f51538ecde397 (patch) | |
| tree | 43f1ce1d7b94ff46d8e546ab9abf92e4dd9e494e /mailman/tests/helpers.py | |
| parent | a1c73f6c305c7f74987d99855ba59d8fa823c253 (diff) | |
| download | mailman-5ca899a81b547dd46197b8d51c7f51538ecde397.tar.gz mailman-5ca899a81b547dd46197b8d51c7f51538ecde397.tar.zst mailman-5ca899a81b547dd46197b8d51c7f51538ecde397.zip | |
Diffstat (limited to 'mailman/tests/helpers.py')
| -rw-r--r-- | mailman/tests/helpers.py | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/mailman/tests/helpers.py b/mailman/tests/helpers.py index 92d652236..4dc289458 100644 --- a/mailman/tests/helpers.py +++ b/mailman/tests/helpers.py @@ -32,16 +32,17 @@ import os import time import errno import mailbox -import subprocess +import smtplib +import tempfile +import threading +from Queue import Empty, Queue from datetime import datetime, timedelta from mailman.bin.master import Loop as Master from mailman.configuration import config from mailman.queue import Switchboard - - -WAIT_INTERVAL = timedelta(seconds=3) +from mailman.tests.smtplistener import Server @@ -132,3 +133,49 @@ class TestableMaster(Master): """The pids of all the child qrunner processes.""" for pid in self._started_kids: yield pid + + + +class SMTPServer: + """An smtp server for testing.""" + + host = 'localhost' + port = 9025 + + def __init__(self): + self._messages = [] + self._queue = Queue() + self._server = Server((self.host, self.port), self._queue) + self._thread = threading.Thread(target=self._server.start) + + def start(self): + """Start the smtp server in a thread.""" + self._thread.start() + + def stop(self): + """Stop the smtp server.""" + smtpd = smtplib.SMTP() + smtpd.connect(self.host, self.port) + smtpd.docmd('EXIT') + self.clear() + # Wait for the thread to exit. + self._thread.join() + + @property + def messages(self): + """Return all the messages received by the smtp server.""" + for message in self._messages: + # See if there's anything waiting in the queue. + try: + message = self._queue.get_nowait() + except Empty: + pass + else: + self._messages.append(message) + yield message + + def clear(self): + """Clear all messages from the queue.""" + # Just throw these away. + list(self._messages) + self._messages = [] |
