Path: redb-route/tests/redb.Route.Tests.MqttNet/MqttReconnectTests.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.MqttNet/MqttReconnectTests.cs ---- using System.Collections.Concurrent; using System.Text; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.MqttNet; using redb.Route.MqttNet.Connection; using redb.Route.Tests.Conformance; namespace redb.Route.Tests.MqttNet; /// /// A consumer outlives a broker restart: the restart drops its connection without a failed message to trigger the /// reconnect, and once the broker is back the consumer connects and subscribes again (the broker keeps nothing over the /// restart, its persistence is off), the same route reads the topic again, serial and concurrent, and the endpoint /// counts one reconnect. The restart is on : it runs only /// where REDB_TEST_MQTT_CONTAINER names the broker's container, elsewhere it is skipped. Run it with /// tests/docker/run-fault-suite.sh --broker mosquitto. /// [Trait("Category", "Integration")] [Collection(MqttBrokerRestartCollection.Name)] public sealed class MqttReconnectTests { private const string Broker = "server=localhost&port=11883"; private static readonly MqttStand Stand = MqttStand.Instance; [BrokerFaultTheory(MqttStand.ContainerVariable)] [InlineData(1)] [InlineData(4)] public async Task A_consumer_reads_the_topic_again_after_the_broker_restarts(int concurrentConsumers) { var topic = $"test/restart-{Guid.NewGuid():N}"; var received = new ConcurrentQueue(); await using var context = new RouteContext(); context.AddComponent(new MqttComponent { ClientFactory = new DefaultMqttClientFactory() }); var uri = $"mqtt:{topic}?mode=Subscribe&{Broker}&qos=1&clientId=test-restart-{Guid.NewGuid():N}" + $"&concurrentConsumers={concurrentConsumers}"; context.AddRoutes(r => r.From(uri).RouteId("restarted").Process(e => received.Enqueue(e.In.Body is byte[] bytes ? Encoding.UTF8.GetString(bytes) : e.In.Body?.ToString() ?? ""))); await context.Start(); await Stand.PutAsync(topic, "before"); (await Until(() => received.Contains("before"))).Should().BeTrue("the route reads the topic before the restart"); await Stand.Fault!.RestartAsync(); // A message published while nobody is subscribed is gone: publish only once the subscription is back. var statistics = (IEndpointStatistics)context.GetEndpoint(uri); (await Until(() => statistics.Reconnects > 0)).Should().BeTrue( "the consumer connects and subscribes again after the broker restarts"); await Stand.PutAsync(topic, "after"); (await Until(() => received.Contains("after"))).Should().BeTrue("the consumer reads what was published after the restart"); statistics.Reconnects.Should().Be(1, "one restart is one reconnect"); statistics.HealthStatus.Should().Be(EndpointHealthStatus.Healthy, "the subscription is back"); context.GetRoute("restarted")!.Status.Should().Be(RouteStatus.Started, "a lost connection does not stop the route"); } private static async Task Until(Func condition) { var deadline = DateTime.UtcNow + Stand.Wait; while (!condition()) { if (DateTime.UtcNow > deadline) return false; await Task.Delay(100); } return true; } } /// A broker restart cuts every connection of the suite, so nothing runs beside the restart tests. [CollectionDefinition(Name, DisableParallelization = true)] public sealed class MqttBrokerRestartCollection { /// The collection's name. public const string Name = "MQTT broker restart"; }