Path: redb-route/tests/redb.Route.Tests/Components/ContextRestartQueueTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests/Components/ContextRestartQueueTests.cs
----
using System.Threading.Channels;
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Tests.Components;
///
/// In-memory queues of a route context across Stop() and Start() of the same instance (redb-route issue
/// #12): the context keeps its components and endpoints, so a queue that the stop closed for good left every hop
/// behind it dead while the routes reported Started.
///
public sealed class ContextRestartQueueTests : IAsyncLifetime
{
private readonly RouteContext _context = new();
public Task InitializeAsync() => Task.CompletedTask;
// IAsyncLifetime, not IAsyncDisposable: xUnit 2 never calls DisposeAsync of IAsyncDisposable on a test class.
public async Task DisposeAsync() => await _context.DisposeAsync();
private async Task SendAsync(string uri, object body)
{
var producer = _context.GetEndpoint(uri).CreateProducer();
await producer.Start();
await producer.Process(new Exchange(new Message { Body = body }));
}
private static async Task WaitForAsync(Func condition, string because)
{
var deadline = DateTime.UtcNow.AddSeconds(10);
while (!condition() && DateTime.UtcNow < deadline)
await Task.Delay(20);
condition().Should().BeTrue(because);
}
[Fact]
public async Task Seda_delivers_after_the_context_is_stopped_and_started_again()
{
var received = 0;
_context.AddRoutes(r => r.From("direct://input").To("seda://queue"));
_context.AddRoutes(r => r.From("seda://queue").Process(_ => Interlocked.Increment(ref received)));
await _context.Start();
await SendAsync("direct://input", "first");
await WaitForAsync(() => Volatile.Read(ref received) == 1, "seda delivers before the stop");
await _context.Stop();
await _context.Start();
await SendAsync("direct://input", "second");
await WaitForAsync(() => Volatile.Read(ref received) == 2,
"after Stop and Start of the same context the seda queue delivers again");
}
[Fact]
public async Task Seda_stop_finishes_the_queue_and_the_start_opens_a_new_one()
{
var gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var processed = new List();
_context.AddRoutes(r => r.From("seda://waiting").Process(async (IExchange e, CancellationToken _) =>
{
await gate.Task;
lock (processed) processed.Add((string)e.In.Body!);
}));
await _context.Start();
// The first exchange is in the route when the context stops; the other two wait in the queue.
foreach (var body in new[] { "a", "b", "c" })
await SendAsync("seda://waiting", body);
var stopping = _context.Stop();
gate.SetResult();
await stopping;
lock (processed) processed.Should().Equal(["a", "b", "c"], "the stop finishes what is already in the queue");
var whileStopped = () => SendAsync("seda://waiting", "refused");
await whileStopped.Should().ThrowAsync("a stopped queue refuses visibly");
await _context.Start();
await SendAsync("seda://waiting", "d");
await WaitForAsync(() => { lock (processed) return processed.Count == 4; },
"after the start the queue is new and accepts");
lock (processed) processed.Should().Equal("a", "b", "c", "d");
}
[Fact]
public async Task Seda_refuses_a_send_once_the_context_is_disposed()
{
_context.AddRoutes(r => r.From("seda://closed").Process(_ => { }));
await _context.Start();
var producer = _context.GetEndpoint("seda://closed").CreateProducer();
await producer.Start();
await _context.DisposeAsync();
var send = () => producer.Process(new Exchange(new Message { Body = "late" }));
await send.Should().ThrowAsync("a send to the queue of a closed context is visible, it is not lost");
}
[Fact]
public async Task Threads_delivers_after_the_context_is_stopped_and_started_again()
{
var received = 0;
_context.AddRoutes(r => r.From("direct://pooled")
.Threads(2)
.Process(_ => Interlocked.Increment(ref received))
.End());
await _context.Start();
await SendAsync("direct://pooled", "first");
await WaitForAsync(() => Volatile.Read(ref received) == 1, "the Threads pool processes before the stop");
await _context.Stop();
await _context.Start();
await SendAsync("direct://pooled", "second");
await WaitForAsync(() => Volatile.Read(ref received) == 2,
"after Stop and Start of the same context the Threads pool processes again");
}
}