|
| 1 | +# Copyright (c) 2026 Code Infinity |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +"""Regression for tina4-python#278: the SMTP send timeout is configurable. |
| 8 | +
|
| 9 | +No mocks. A REAL TCP server accepts the connection and then says nothing, so |
| 10 | +send() blocks waiting for the SMTP greeting. With the timeout left at its 30 s |
| 11 | +default that is a 30 s hold; with a 1 s timeout the send must fail in about a |
| 12 | +second. The wall-clock is the instrument: a configured 1 s timeout that is |
| 13 | +honoured finishes well under the default, a hardcoded 30 s does not. |
| 14 | +""" |
| 15 | + |
| 16 | +import os |
| 17 | +import socket |
| 18 | +import threading |
| 19 | +import time |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from tina4_python.messenger import Messenger |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def silent_smtp(): |
| 28 | + """A server that accepts TCP connections and never sends a byte.""" |
| 29 | + server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 30 | + server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 31 | + server.bind(("127.0.0.1", 0)) |
| 32 | + server.listen(8) |
| 33 | + port = server.getsockname()[1] |
| 34 | + held = [] |
| 35 | + stop = threading.Event() |
| 36 | + |
| 37 | + def accept_loop(): |
| 38 | + server.settimeout(0.5) |
| 39 | + while not stop.is_set(): |
| 40 | + try: |
| 41 | + conn, _ = server.accept() |
| 42 | + held.append(conn) # keep it open, never reply |
| 43 | + except OSError: |
| 44 | + pass |
| 45 | + |
| 46 | + thread = threading.Thread(target=accept_loop, daemon=True) |
| 47 | + thread.start() |
| 48 | + try: |
| 49 | + yield port |
| 50 | + finally: |
| 51 | + stop.set() |
| 52 | + thread.join(timeout=2) |
| 53 | + for conn in held: |
| 54 | + conn.close() |
| 55 | + server.close() |
| 56 | + |
| 57 | + |
| 58 | +def _send(**kwargs): |
| 59 | + messenger = Messenger(host="127.0.0.1", encryption="none", |
| 60 | + from_address="app@localhost", **kwargs) |
| 61 | + start = time.monotonic() |
| 62 | + result = messenger.send("someone@localhost", "test", "hello") |
| 63 | + elapsed = time.monotonic() - start |
| 64 | + # The host never speaks SMTP, so the send always fails - what matters is HOW |
| 65 | + # LONG it waited, and that the failure is reported AS a timeout. |
| 66 | + assert result["success"] is False |
| 67 | + return elapsed, result |
| 68 | + |
| 69 | + |
| 70 | +def test_constructor_timeout_bounds_the_send_and_reports_a_timeout(silent_smtp): |
| 71 | + elapsed, result = _send(port=silent_smtp, timeout=1) |
| 72 | + assert elapsed < 8, f"a 1 s timeout should fail fast, not hold ~30 s (took {elapsed:.1f} s)" |
| 73 | + assert "tim" in str(result["message"]).lower(), \ |
| 74 | + f"a silent server must be reported as a timeout, got {result['message']!r}" |
| 75 | + |
| 76 | + |
| 77 | +def test_env_timeout_bounds_the_send(silent_smtp, monkeypatch): |
| 78 | + monkeypatch.setenv("TINA4_MAIL_TIMEOUT", "1") |
| 79 | + elapsed, _ = _send(port=silent_smtp) |
| 80 | + assert elapsed < 8, f"TINA4_MAIL_TIMEOUT=1 should fail fast (took {elapsed:.1f} s)" |
| 81 | + |
| 82 | + |
| 83 | +def test_default_timeout_is_thirty_seconds(): |
| 84 | + # The default is unchanged (no behaviour change for apps that set nothing). |
| 85 | + assert Messenger(host="127.0.0.1", encryption="none").timeout == 30 |
| 86 | + |
| 87 | + |
| 88 | +def test_a_bad_env_value_warns_once_and_falls_back_to_the_default(monkeypatch): |
| 89 | + monkeypatch.setenv("TINA4_MAIL_TIMEOUT", "not-a-number") |
| 90 | + assert Messenger(host="127.0.0.1", encryption="none").timeout == 30 |
| 91 | + |
| 92 | + |
| 93 | +def test_a_zero_env_value_falls_back_to_the_default(monkeypatch): |
| 94 | + # Zero is garbage, not an opt-out: to the socket it means "do not wait at all". |
| 95 | + monkeypatch.setenv("TINA4_MAIL_TIMEOUT", "0") |
| 96 | + assert Messenger(host="127.0.0.1", encryption="none").timeout == 30 |
| 97 | + |
| 98 | + |
| 99 | +def test_an_explicit_sub_second_timeout_is_refused(): |
| 100 | + # An explicit value is the caller's own instruction, so < 1 is a bug, not a default. |
| 101 | + with pytest.raises(ValueError): |
| 102 | + Messenger(host="127.0.0.1", encryption="none", timeout=0) |
| 103 | + |
| 104 | + |
| 105 | +def test_smtp_timeout_env_is_no_longer_honoured(monkeypatch): |
| 106 | + # The SMTP_TIMEOUT fallback was dropped for parity with the PHP master: |
| 107 | + # only TINA4_MAIL_TIMEOUT configures the timeout now. |
| 108 | + monkeypatch.delenv("TINA4_MAIL_TIMEOUT", raising=False) |
| 109 | + monkeypatch.setenv("SMTP_TIMEOUT", "1") |
| 110 | + assert Messenger(host="127.0.0.1", encryption="none").timeout == 30 |
0 commit comments