Path: redb-route/tests/redb.Route.Tests.MqttNet/MqttRedeliveryCharacterizationTests.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/MqttRedeliveryCharacterizationTests.cs ---- using System.Text; using MQTTnet; using MQTTnet.Formatter; using MQTTnet.Protocol; namespace redb.Route.Tests.MqttNet; /// /// What MQTTnet and the broker do with a message the client does not acknowledge, pinned against Mosquitto /// (localhost:11883): the consumer's failure rule relies on it. MQTT has no negative acknowledgement; the broker sends /// an unacknowledged QoS 1 or QoS 2 message again only when the client connects again to a session the broker kept. /// Unlike Paho, MQTTnet holds back the PUBREC of a QoS 2 message too while AutoAcknowledge is off, so a /// QoS 2 message comes again as well. /// [Trait("Category", "Integration")] public sealed class MqttRedeliveryCharacterizationTests { private const string Server = "localhost"; private const int Port = 11883; [Theory] [InlineData(MqttQualityOfServiceLevel.AtLeastOnce, MqttProtocolVersion.V311)] [InlineData(MqttQualityOfServiceLevel.ExactlyOnce, MqttProtocolVersion.V311)] [InlineData(MqttQualityOfServiceLevel.AtLeastOnce, MqttProtocolVersion.V500)] [InlineData(MqttQualityOfServiceLevel.ExactlyOnce, MqttProtocolVersion.V500)] public async Task UnacknowledgedMessage_KeptSession_IsSentAgainAfterReconnect( MqttQualityOfServiceLevel qos, MqttProtocolVersion version) { var topic = $"test/redeliver-{Guid.NewGuid():N}"; var clientId = $"test-redeliver-{Guid.NewGuid():N}"; var factory = new MqttClientFactory(); var builder = new MqttClientOptionsBuilder() .WithTcpServer(Server, Port) .WithClientId(clientId) .WithProtocolVersion(version) .WithCleanSession(false); if (version == MqttProtocolVersion.V500) builder.WithSessionExpiryInterval(300); // MQTT 5: an absent interval ends the session with the connection var options = builder.Build(); var deliveries = 0; var first = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var again = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); using var subscriber = factory.CreateMqttClient(); subscriber.ApplicationMessageReceivedAsync += args => { var payload = Encoding.UTF8.GetString(args.ApplicationMessage.Payload); if (Interlocked.Increment(ref deliveries) == 1) { args.AutoAcknowledge = false; // the route failed: no PUBACK / PUBREC first.TrySetResult(payload); } else { again.TrySetResult(payload); } return Task.CompletedTask; }; await subscriber.ConnectAsync(options); await subscriber.SubscribeAsync(new MqttClientSubscribeOptionsBuilder() .WithTopicFilter(topic, qos).Build()); using var publisher = factory.CreateMqttClient(); await publisher.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer(Server, Port) .WithClientId($"test-pub-{Guid.NewGuid():N}").Build()); await publisher.PublishAsync(new MqttApplicationMessageBuilder() .WithTopic(topic).WithPayload("payload-1").WithQualityOfServiceLevel(qos).Build()); (await first.Task.WaitAsync(TimeSpan.FromSeconds(10))).Should().Be("payload-1"); // Within the same connection the broker does not send it again. await Task.Delay(1500); again.Task.IsCompleted.Should().BeFalse("MQTT forbids resending on the same connection"); await subscriber.DisconnectAsync(); var result = await subscriber.ConnectAsync(options); result.IsSessionPresent.Should().BeTrue("the broker kept the session"); (await again.Task.WaitAsync(TimeSpan.FromSeconds(10))).Should().Be("payload-1", $"an unacknowledged QoS {(int)qos} message is sent again after the reconnect"); await subscriber.DisconnectAsync(); await publisher.DisconnectAsync(); } }