Path: redb-route/tests/redb.Route.Tests.MqttNet/MqttStand.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/MqttStand.cs ---- using System.Buffers; using System.Collections.Concurrent; using System.Text; using MQTTnet; using MQTTnet.Protocol; using redb.Route.Tests.Conformance; namespace redb.Route.Tests.MqttNet; /// /// The MQTT stand: Mosquitto at localhost:11883 (anonymous, no persistence), reached through MQTTnet directly, not /// through the connector. MQTT has no queue: publishes at QoS 1 to a topic, and /// returns what arrives on the topic while it listens. Its container, when /// names it, is broken with ; the broker is ready when it /// answers a subscription to its uptime topic. /// public sealed class MqttStand : IConnectorStand { /// The variable that names the Mosquitto container a test may restart. public const string ContainerVariable = "REDB_TEST_MQTT_CONTAINER"; private const string Host = "localhost"; private const int Port = 11883; /// The stand of this process. public static MqttStand Instance { get; } = new(); private MqttStand() { } /// public BrokerFault? Fault { get; } = BrokerFault.FromEnvironment(ContainerVariable, "mosquitto_sub", "-h", Host, "-p", "11883", "-t", "$SYS/broker/uptime", "-C", "1", "-W", "3"); /// public TimeSpan Wait { get; } = BrokerFault.DefaultWait; /// public async Task PutAsync(string destination, string body, CancellationToken ct = default) { using var client = new MqttClientFactory().CreateMqttClient(); await client.ConnectAsync(Options(), ct); await client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(destination).WithPayload(body) .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce).Build(), ct); await client.DisconnectAsync(cancellationToken: ct); } /// public async Task> DrainAsync(string destination, TimeSpan wait, CancellationToken ct = default) { var bodies = new ConcurrentQueue(); using var client = new MqttClientFactory().CreateMqttClient(); client.ApplicationMessageReceivedAsync += args => { bodies.Enqueue(Encoding.UTF8.GetString(args.ApplicationMessage.Payload.ToArray())); return Task.CompletedTask; }; await client.ConnectAsync(Options(), ct); await client.SubscribeAsync(new MqttClientSubscribeOptionsBuilder() .WithTopicFilter(destination, MqttQualityOfServiceLevel.AtLeastOnce).Build(), ct); await Task.Delay(wait, ct); await client.DisconnectAsync(cancellationToken: ct); return bodies.ToArray(); } private static MqttClientOptions Options() => new MqttClientOptionsBuilder().WithTcpServer(Host, Port) .WithClientId($"stand-{Guid.NewGuid():N}").Build(); }