Files
natsdotnet/tests/NATS.Server.Mqtt.Tests/Mqtt/MqttKeepAliveTests.cs
Joseph Doherty 845441b32c feat: implement full MQTT Go parity across 5 phases — binary protocol, auth/TLS, cross-protocol bridging, monitoring, and JetStream persistence
Phase 1: Binary MQTT 3.1.1 wire protocol with PipeReader-based parsing,
full packet type dispatch, and MQTT 3.1.1 compliance checks.

Phase 2: Auth pipeline routing MQTT CONNECT through AuthService,
TLS transport with SslStream wrapping, pinned cert validation.

Phase 3: IMessageRouter refactor (NatsClient → INatsClient),
MqttNatsClientAdapter for cross-protocol bridging, MqttTopicMapper
with full Go-parity topic/subject translation.

Phase 4: /connz mqtt_client field population, /varz actual MQTT port.

Phase 5: JetStream persistence — MqttStreamInitializer creates 5
internal streams, MqttConsumerManager for QoS 1/2 consumers,
subject-keyed session/retained lookups replacing linear scans.

All 503 MQTT tests and 1589 Core tests pass.
2026-03-13 10:09:40 -04:00

34 lines
1.3 KiB
C#

using System.Net;
using System.Net.Sockets;
using NATS.Server.Mqtt;
using NATS.Server.TestUtilities;
namespace NATS.Server.Mqtt.Tests.Mqtt;
public class MqttKeepAliveTests
{
[Fact]
public async Task Invalid_mqtt_credentials_or_keepalive_timeout_close_session_with_protocol_error()
{
await using var listener = new MqttListener("127.0.0.1", 0);
listener.UseBinaryProtocol = false;
using var cts = new CancellationTokenSource();
await listener.StartAsync(cts.Token);
using var client = new TcpClient();
await client.ConnectAsync(IPAddress.Loopback, listener.Port);
var stream = client.GetStream();
await MqttRuntimeWire.WriteLineAsync(stream, "CONNECT keepalive-client keepalive=1");
(await MqttRuntimeWire.ReadLineAsync(stream, 1000)).ShouldBe("CONNACK");
// Poll until the server closes the connection due to keepalive expiry (keepalive=1s)
var disconnected = await PollHelper.WaitUntilAsync(async () =>
{
var result = await MqttRuntimeWire.ReadRawAsync(stream, 200);
return result == null;
}, timeoutMs: 5000, intervalMs: 100);
disconnected.ShouldBeTrue("Server should disconnect idle client after keepalive timeout");
}
}