summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarry Warsaw2011-05-14 07:06:54 +0200
committerBarry Warsaw2011-05-14 07:06:54 +0200
commit7af50e2b346b76daab8206c448e5e67f06cbacbd (patch)
tree2ad9a9b4f5b30c2e36d6be20fb87d224f047c9af
parentaba960f1c537ba38217f0377e58d611fcfd8f0a1 (diff)
downloadmailman-7af50e2b346b76daab8206c448e5e67f06cbacbd.tar.gz
mailman-7af50e2b346b76daab8206c448e5e67f06cbacbd.tar.zst
mailman-7af50e2b346b76daab8206c448e5e67f06cbacbd.zip
-rw-r--r--src/mailman/queue/outgoing.py9
-rw-r--r--src/mailman/queue/tests/__init__.py0
-rw-r--r--src/mailman/queue/tests/test_outgoing.py87
-rw-r--r--src/mailman/testing/helpers.py14
4 files changed, 103 insertions, 7 deletions
diff --git a/src/mailman/queue/outgoing.py b/src/mailman/queue/outgoing.py
index 7ff194219..07d45c0bd 100644
--- a/src/mailman/queue/outgoing.py
+++ b/src/mailman/queue/outgoing.py
@@ -28,6 +28,7 @@ from mailman.interfaces.mailinglist import Personalization
from mailman.interfaces.mta import SomeRecipientsFailed
from mailman.queue import Runner
from mailman.queue.bounce import BounceMixin
+from mailman.utilities.datetime import now
from mailman.utilities.modules import find_name
@@ -56,7 +57,7 @@ class OutgoingRunner(Runner, BounceMixin):
def _dispose(self, mlist, msg, msgdata):
# See if we should retry delivery of this message again.
deliver_after = msgdata.get('deliver_after', datetime.fromtimestamp(0))
- if datetime.now() < deliver_after:
+ if now() < deliver_after:
return True
# Calculate whether we should VERP this message or not. The results of
# this set the 'verp' key in the message metadata.
@@ -118,15 +119,15 @@ class OutgoingRunner(Runner, BounceMixin):
# occasionally move them back here for another shot at
# delivery.
if error.temporary_failures:
- now = datetime.now()
+ current_time = now()
recips = error.temporary_failures
last_recip_count = msgdata.get('last_recip_count', 0)
- deliver_until = msgdata.get('deliver_until', now)
+ deliver_until = msgdata.get('deliver_until', current_time)
if len(recips) == last_recip_count:
# We didn't make any progress, so don't attempt
# delivery any longer. BAW: is this the best
# disposition?
- if now > deliver_until:
+ if current_time > deliver_until:
return False
else:
# Keep trying to delivery this message for a while
diff --git a/src/mailman/queue/tests/__init__.py b/src/mailman/queue/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/mailman/queue/tests/__init__.py
diff --git a/src/mailman/queue/tests/test_outgoing.py b/src/mailman/queue/tests/test_outgoing.py
new file mode 100644
index 000000000..fc4eb3070
--- /dev/null
+++ b/src/mailman/queue/tests/test_outgoing.py
@@ -0,0 +1,87 @@
+# Copyright (C) 2011 by the Free Software Foundation, Inc.
+#
+# This file is part of GNU Mailman.
+#
+# GNU Mailman is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option)
+# any later version.
+#
+# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
+
+"""Test the outgoing queue runner."""
+
+from __future__ import absolute_import, unicode_literals
+
+__metaclass__ = type
+__all__ = [
+ 'test_suite',
+ ]
+
+
+import datetime
+import unittest
+
+from mailman.app.lifecycle import create_list
+from mailman.config import config
+from mailman.queue.outgoing import OutgoingRunner
+from mailman.testing.helpers import (
+ get_queue_messages,
+ make_testable_runner,
+ specialized_message_from_string as message_from_string)
+from mailman.testing.layers import SMTPLayer
+from mailman.utilities.datetime import now
+
+
+
+def run_once(qrunner):
+ """Predicate for make_testable_runner().
+
+ Ensures that the queue runner only runs once.
+ """
+ return True
+
+
+
+class TestOnce(unittest.TestCase):
+ """Test outgoing runner message disposition."""
+
+ layer = SMTPLayer
+
+ def setUp(self):
+ self._mlist = create_list('test@example.com')
+ self._outq = config.switchboards['out']
+ self._runner = make_testable_runner(OutgoingRunner, 'out', run_once)
+ self._msg = message_from_string("""\
+From: anne@example.com
+To: test@example.com
+Message-Id: <first>
+
+""")
+ self._msgdata = {}
+
+ def test_deliver_after(self):
+ # When the metadata has a deliver_after key in the future, the queue
+ # runner will re-enqueue the message rather than delivering it.
+ deliver_after = now() + datetime.timedelta(days=10)
+ self._msgdata['deliver_after'] = deliver_after
+ self._outq.enqueue(self._msg, self._msgdata, tolist=True,
+ listname='test@example.com')
+ self._runner.run()
+ items = get_queue_messages('out')
+ self.assertEqual(len(items), 1)
+ self.assertEqual(items[0].msgdata['deliver_after'], deliver_after)
+ self.assertEqual(items[0].msg['message-id'], '<first>')
+
+
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(TestOnce))
+ return suite
diff --git a/src/mailman/testing/helpers.py b/src/mailman/testing/helpers.py
index 71cddd0f4..f3b0071d0 100644
--- a/src/mailman/testing/helpers.py
+++ b/src/mailman/testing/helpers.py
@@ -48,6 +48,7 @@ from contextlib import contextmanager
from email import message_from_string
from httplib2 import Http
from lazr.config import as_timedelta
+from operator import itemgetter
from urllib import urlencode
from urllib2 import HTTPError
from zope import event
@@ -63,7 +64,7 @@ from mailman.utilities.mailbox import Mailbox
-def make_testable_runner(runner_class, name=None):
+def make_testable_runner(runner_class, name=None, predicate=None):
"""Create a queue runner that runs until its queue is empty.
:param runner_class: The queue runner's class.
@@ -71,6 +72,10 @@ def make_testable_runner(runner_class, name=None):
:param name: Optional queue name; if not given, it is calculated from the
class name.
:type name: string or None
+ :param predicate: Optional alternative predicate for deciding when to stop
+ the queue runner. When None (the default) it stops when the queue is
+ empty.
+ :type predicate: callable that gets one argument, the queue runner.
:return: A runner instance.
"""
@@ -90,7 +95,10 @@ def make_testable_runner(runner_class, name=None):
def _do_periodic(self):
"""Stop when the queue is empty."""
- self._stop = (len(self.switchboard.files) == 0)
+ if predicate is None:
+ self._stop = (len(self.switchboard.files) == 0)
+ else:
+ self._stop = predicate(self)
return EmptyingRunner(name)
@@ -118,7 +126,7 @@ def get_queue_messages(queue_name, sort_on=None):
messages.append(_Bag(msg=msg, msgdata=msgdata))
queue.finish(filebase)
if sort_on is not None:
- messages.sort(key=lambda item: item.msg[sort_on])
+ messages.sort(key=itemgetter(sort_on))
return messages