Mail: timeout per stap met fase en verstreken tijd in het log
De SMTP-verzender gaf alle stappen samen een tijdsbudget; nu krijgt elke stap (verbinden met STARTTLS, inloggen, versturen) zijn eigen volledige timeout. Bij een timeout noemt de code de stap (timeout_connect, timeout_auth, timeout_send) en toont het log per stap de verstreken milliseconden. TimeoutSeconds blijft instelbaar (standaard 10), bereik nu 1 tot 120. Infra-doc met veiligheidsmodus, timeout en leeswijzer voor het journal. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,8 +25,12 @@ internal sealed class StubSmtpServer : IDisposable
|
||||
/// <param name="silent">Accept connections but never answer (to provoke the timeout).</param>
|
||||
/// <param name="rejectMailFrom">Reply to MAIL FROM with this full line (for example "553 5.7.1 ...").</param>
|
||||
/// <param name="rejectRcpt">Reply to RCPT TO with this full line.</param>
|
||||
public StubSmtpServer(bool silent = false, string? rejectMailFrom = null, string? rejectRcpt = null)
|
||||
private readonly string? _stallAt;
|
||||
|
||||
/// <param name="stallAt">"auth" advertises AUTH PLAIN and then never answers the login; "send" never answers MAIL FROM.</param>
|
||||
public StubSmtpServer(bool silent = false, string? rejectMailFrom = null, string? rejectRcpt = null, string? stallAt = null)
|
||||
{
|
||||
_stallAt = stallAt;
|
||||
_silent = silent;
|
||||
_rejectMailFrom = rejectMailFrom;
|
||||
_rejectRcpt = rejectRcpt;
|
||||
@@ -47,7 +51,13 @@ internal sealed class StubSmtpServer : IDisposable
|
||||
await writer.WriteLineAsync("220 stub ESMTP");
|
||||
while (await reader.ReadLineAsync() is { } line)
|
||||
{
|
||||
if (line.StartsWith("EHLO", StringComparison.OrdinalIgnoreCase)) await writer.WriteLineAsync("250 stub");
|
||||
if (line.StartsWith("EHLO", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (_stallAt == "auth") { await writer.WriteLineAsync("250-stub"); await writer.WriteLineAsync("250 AUTH PLAIN"); }
|
||||
else await writer.WriteLineAsync("250 stub");
|
||||
}
|
||||
else if (line.StartsWith("AUTH", StringComparison.OrdinalIgnoreCase) && _stallAt == "auth") await Task.Delay(TimeSpan.FromSeconds(30));
|
||||
else if (line.StartsWith("MAIL FROM", StringComparison.OrdinalIgnoreCase) && _stallAt == "send") await Task.Delay(TimeSpan.FromSeconds(30));
|
||||
else if (line.StartsWith("MAIL FROM", StringComparison.OrdinalIgnoreCase)) { MailFrom = line; await writer.WriteLineAsync(_rejectMailFrom ?? "250 ok"); }
|
||||
else if (line.StartsWith("RCPT TO", StringComparison.OrdinalIgnoreCase)) { Recipients.Add(line); await writer.WriteLineAsync(_rejectRcpt ?? "250 ok"); }
|
||||
else if (line.StartsWith("DATA", StringComparison.OrdinalIgnoreCase))
|
||||
@@ -137,7 +147,7 @@ public class SmtpEmailSenderTests
|
||||
var started = DateTime.UtcNow;
|
||||
var result = await Sender(server.Port, timeoutSeconds: 1).SendAsync(new OutgoingMail("to@example.org", "S", "<p>x</p>"));
|
||||
|
||||
result.Should().Be(MailSendResult.Fail(MailSendResult.Timeout));
|
||||
result.Should().Be(MailSendResult.Fail(MailSendResult.TimeoutConnect));
|
||||
(DateTime.UtcNow - started).Should().BeLessThan(TimeSpan.FromSeconds(10));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user