mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 03:43:11 +00:00
The channel used DropWrite, under which TryWrite reports success and discards the email, so a full queue still counted the request and never sent the code. Wait makes TryWrite return false when the queue is full, without blocking, so the request is released and the user can ask again.
43 lines
1.7 KiB
C#
43 lines
1.7 KiB
C#
using Api.SeaHavenIndustries.HostedServices;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Xunit;
|
|
|
|
namespace Api.SeaHavenIndustries.Tests;
|
|
|
|
public class PasswordResetEmailChannelTests
|
|
{
|
|
[Fact]
|
|
public void A_full_queue_refuses_the_email_instead_of_dropping_it_silently()
|
|
{
|
|
var channel = new PasswordResetEmailChannel(NullLogger<PasswordResetEmailChannel>.Instance);
|
|
for (var i = 0; i < PasswordResetEmailChannel.Capacity; i++)
|
|
channel.TryEnqueue($"user{i}@example.com", "subject", "body").Should().BeTrue();
|
|
|
|
var accepted = channel.TryEnqueue("late@example.com", "subject", "body");
|
|
|
|
accepted.Should().BeFalse();
|
|
channel.Pending.Should().Be(PasswordResetEmailChannel.Capacity);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_refused_email_is_never_delivered_and_the_queue_accepts_again_once_drained()
|
|
{
|
|
var channel = new PasswordResetEmailChannel(NullLogger<PasswordResetEmailChannel>.Instance);
|
|
for (var i = 0; i < PasswordResetEmailChannel.Capacity; i++)
|
|
channel.TryEnqueue($"user{i}@example.com", "subject", "body");
|
|
channel.TryEnqueue("late@example.com", "subject", "body").Should().BeFalse();
|
|
|
|
var delivered = new List<string>();
|
|
while (channel.Reader.TryRead(out var email))
|
|
{
|
|
delivered.Add(email.EmailTo);
|
|
channel.MarkHandled();
|
|
}
|
|
|
|
delivered.Should().HaveCount(PasswordResetEmailChannel.Capacity).And.NotContain("late@example.com");
|
|
channel.Pending.Should().Be(0);
|
|
channel.TryEnqueue("retry@example.com", "subject", "body").Should().BeTrue();
|
|
(await channel.Reader.ReadAsync()).EmailTo.Should().Be("retry@example.com");
|
|
}
|
|
}
|