You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2025/02/13 20:07:37 UTC
(allura) 03/03: [#8574] removed the async function start_server in favor a more simple approach to keep the server running
This is an automated email from the ASF dual-hosted git repository.
brondsem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git
commit 1c8853f9255dac6f83f901fa1960a56f734fc057
Author: Guillermo Cruz <gu...@slashdotmedia.com>
AuthorDate: Tue Feb 4 15:51:02 2025 -0700
[#8574] removed the async function start_server in favor a more simple approach to keep the server running
---
Allura/allura/command/smtp_server.py | 27 ++++++++++-----------------
Allura/allura/tests/test_mail_util.py | 33 +++++++++++++++++++--------------
Allura/test.ini | 3 +++
AlluraTest/alluratest/smtp_debug.py | 10 +---------
4 files changed, 33 insertions(+), 40 deletions(-)
diff --git a/Allura/allura/command/smtp_server.py b/Allura/allura/command/smtp_server.py
index 3a1b07e42..1ce8e2cf0 100644
--- a/Allura/allura/command/smtp_server.py
+++ b/Allura/allura/command/smtp_server.py
@@ -25,18 +25,10 @@
from allura.lib import helpers as h
from paste.deploy.converters import asint
from aiosmtpd.controller import Controller
-import asyncio
from tg.wsgiapp import RequestLocals
+import time
-async def start_server(loop):
- handler = MailServer()
- hostname = tg.config.get('forgemail.host', '0.0.0.0')
- port = asint(tg.config.get('forgemail.port', 8825))
- controller = Controller(handler, hostname=hostname, port=port)
- controller.start()
-
-
class SMTPServerCommand(base.Command):
min_args = 1
max_args = 1
@@ -50,14 +42,14 @@ class SMTPServerCommand(base.Command):
def command(self):
faulthandler.enable()
self.basic_setup()
- loop = asyncio.get_event_loop()
- task = loop.create_task(start_server(loop=loop))
- try:
- loop.run_forever()
- except KeyboardInterrupt:
- task.cancel()
- finally:
- loop.close()
+ handler = MailServer()
+ hostname = tg.config.get('forgemail.host', '0.0.0.0')
+ port = asint(tg.config.get('forgemail.port', 8825))
+ controller = Controller(handler, hostname=hostname, port=port)
+ controller.start()
+ while True:
+ time.sleep(100000)
+
class MailServer:
async def handle_DATA(self, server, session, envelope):
@@ -68,6 +60,7 @@ async def handle_DATA(self, server, session, envelope):
data = envelope.content
base.log.info('Msg Received from %s for %s', mailfrom, rcpttos)
base.log.info(' (%d bytes)', len(data))
+ # pass the app_globals into the request context again because it's in a different thread
tgl = RequestLocals()
tgl.tmpl_context = EmptyClass()
tgl.app_globals = tg.app_globals
diff --git a/Allura/allura/tests/test_mail_util.py b/Allura/allura/tests/test_mail_util.py
index 19001b8d5..38418ddf3 100644
--- a/Allura/allura/tests/test_mail_util.py
+++ b/Allura/allura/tests/test_mail_util.py
@@ -24,7 +24,6 @@
from tg import config as tg_config
from smtplib import SMTP as SMTPClient
from alluratest.controller import setup_basic_test, setup_global_objects
-from alluratest.smtp_debug import BetterDebuggingServer
from allura.lib.utils import ConfigProxy
from allura.app import Application
from allura.lib.mail_util import (
@@ -39,7 +38,8 @@
from allura.lib.exceptions import AddressException
from allura.tests import decorators as td
from paste.deploy.converters import asint
-from aiosmtpd.handlers import Debugging
+from allura.command.smtp_server import MailServer
+from aiosmtpd.controller import Controller
config = ConfigProxy(
common_suffix='forgemail.domain',
@@ -335,24 +335,29 @@ def setup_method(self, method):
@mock.patch('allura.command.base.log', autospec=True)
def test(self, log):
- mailserver = BetterDebuggingServer()
- controller, handler = mailserver.start_server()
hostname = tg_config.get('forgemail.host', '0.0.0.0')
- port = asint(tg_config.get('forgemail.port', 8825))
+ port = asint(tg_config.get('forgemail.port', 8827))
+ handler = MailServer()
+ controller = Controller(handler, hostname=hostname, port=port)
+ controller.start()
+
with SMTPClient(hostname, port, timeout=0.1) as client:
- resp = client.docmd("HELP", "HELO")
code, msg = client.ehlo("example.com")
assert code == 250
- client.sendmail('aperson@example.com', ['bperson@example.com'],"""
- From: Anne Person <an...@example.com>
- To: Bart Person <ba...@example.com>
+ mailfrom = client.docmd("MAIL FROM: <fr...@example.com>")
+ assert mailfrom == (250, b'OK')
+ rcpt = client.docmd("RCPT TO: <to...@example.com>")
+ assert rcpt == (250, b'OK')
+ data = client.docmd("DATA")
+ assert data == (354, b'End data with <CR><LF>.<CR><LF>')
+
+ with SMTPClient(hostname, port, timeout=0.1) as client:
+ client.sendmail('from@example.com', ['to@example.com'],"""
+ From: From Person <fr...@example.com>
+ To: To Person <to...@example.com>
Subject: A test
Hi Bart, this is Anne.
""")
- assert isinstance(handler, Debugging)
- text = handler.stream.getvalue()
- assert 'From: Anne Person <an...@example.com>' in text
- assert ' To: Bart Person <ba...@example.com>' in text
- assert 'Subject: A test' in text
+ controller.stop()
diff --git a/Allura/test.ini b/Allura/test.ini
index 8c3604c11..c46c0646a 100644
--- a/Allura/test.ini
+++ b/Allura/test.ini
@@ -84,6 +84,9 @@ markdown_render_max_length = 40000
; TODO: update tests and remove this setting override
auth.require_email_addr = false
+forgemail.host = 127.0.0.1
+forgemail.port = 8827
+
[app:task]
use = main
; TurboGears will use controllers/task.py as root controller
diff --git a/AlluraTest/alluratest/smtp_debug.py b/AlluraTest/alluratest/smtp_debug.py
index b6cec8324..f4e1517e8 100644
--- a/AlluraTest/alluratest/smtp_debug.py
+++ b/AlluraTest/alluratest/smtp_debug.py
@@ -21,17 +21,9 @@
from io import StringIO
from paste.deploy.converters import asint
+
class BetterDebuggingServer:
- def start_server(self):
- stream = StringIO()
- handler = Debugging(stream) # throws away the email
- hostname = tg.config.get('forgemail.host', '0.0.0.0')
- port = asint(tg.config.get('forgemail.port', 8825))
- controller = Controller(handler, hostname=hostname, port=port)
- controller.start()
- return controller, handler
-
async def handle_DATA(self, server, session, envelope):
try: