Skip to content

Commit d27b51f

Browse files
authored
Implement test for SMTPHandler on send failure
Add test for connection closure on SMTP send failure.
1 parent 86b1b79 commit d27b51f

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Lib/test/test_logging.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,25 @@ def test_basic(self):
11711171
self.assertEndsWith(data, '\n\nHello \u2713')
11721172
h.close()
11731173

1174+
def test_connection_closed_on_send_failure(self):
1175+
handler = logging.handlers.SMTPHandler(
1176+
("localhost", 25),
1177+
"me@example.com",
1178+
"you@example.com",
1179+
"subject",
1180+
timeout=self.TIMEOUT,
1181+
)
1182+
record = logging.makeLogRecord({"msg": "hello"})
1183+
1184+
smtp = unittest.mock.Mock()
1185+
smtp.send_message.side_effect = OSError("send failed")
1186+
1187+
with unittest.mock.patch("smtplib.SMTP", return_value=smtp):
1188+
with support.captured_stderr():
1189+
handler.emit(record)
1190+
1191+
smtp.close.assert_called_once()
1192+
11741193
def process_message(self, *args):
11751194
self.messages.append(args)
11761195
self.handled.set()

0 commit comments

Comments
 (0)