test+docs(mqtt): end-to-end Bad-then-identical-replay recovery
This commit is contained in:
@@ -100,6 +100,79 @@ public sealed class MqttDriverSessionLifecycleTests
|
||||
"the authored 2s budget must still bound the connect, not the 15s default");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b>End-to-end through <see cref="MqttDriver"/> (archreview 05/STAB-18).</b> Deliver a value,
|
||||
/// let the real broker drop the real session, and the driver publishes
|
||||
/// <c>BadNoCommunication</c> with the value retained; feed the <b>identical</b> payload back —
|
||||
/// the retained replay a broker sends on reconnect — and it publishes Good again.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>What this adds over <c>MqttSubscriptionManagerRecoveryTests</c>:</b> the wiring. Those
|
||||
/// drive <c>OnConnectionLost()</c> directly, so they stay green even if
|
||||
/// <c>MqttSubscriptionManager.AttachTo</c> never subscribes to
|
||||
/// <see cref="MqttConnection.ConnectionLost"/> — an unforwarded seam is this repo's
|
||||
/// best-documented way to ship an inert fix. Here the edge comes from a genuine socket drop
|
||||
/// through MQTTnet's own <c>DisconnectedAsync</c>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Vacuity rule:</b> the recovery payload is byte-identical to the pre-outage one. A test
|
||||
/// that replays a <i>different</i> value passes against a build with no stale at all — the
|
||||
/// value change alone would defeat the suppression — and is not the guard.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Ingest is fed through <c>Subscriptions.HandleMessage</c>, the method MQTTnet's dispatcher
|
||||
/// calls, because <see cref="MiniBroker"/> answers CONNECT and PINGREQ and nothing else: it
|
||||
/// can drop a session (which is what this test needs from it) but cannot PUBLISH.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
[Fact]
|
||||
public async Task ABrokerDrop_MarksCachedRefsBadNoCommunication_AndTheIdenticalReplayRecovers()
|
||||
{
|
||||
const string rawPath = "Plant/Mqtt/dev1/Temp";
|
||||
const string topic = "factory/oven/temp";
|
||||
const uint good = 0x00000000u;
|
||||
const uint badNoCommunication = 0x80310000u;
|
||||
|
||||
using var broker = new MiniBroker();
|
||||
var options = BrokerOptions(broker.Port) with
|
||||
{
|
||||
RawTags =
|
||||
[
|
||||
new RawTagEntry(
|
||||
rawPath,
|
||||
$$"""{"topic":"{{topic}}","payloadFormat":"Json","jsonPath":"$.value","dataType":"Float64"}""",
|
||||
WriteIdempotent: false),
|
||||
],
|
||||
};
|
||||
await using var driver = new MqttDriver(options, "d", null);
|
||||
await driver.InitializeAsync(Json(options), TestContext.Current.CancellationToken);
|
||||
await driver.SubscribeAsync([rawPath], TimeSpan.Zero, TestContext.Current.CancellationToken);
|
||||
|
||||
var raised = new List<DataValueSnapshot>();
|
||||
driver.OnDataChange += (_, e) => { lock (raised) raised.Add(e.Snapshot); };
|
||||
|
||||
driver.Subscriptions.HandleMessage(topic, """{"value":21.5}"""u8.ToArray(), retained: false);
|
||||
lock (raised) raised.Count.ShouldBe(1, "the first value must publish");
|
||||
|
||||
broker.DropAll();
|
||||
await WaitForAsync(
|
||||
() => { lock (raised) return raised.Count == 2; },
|
||||
"the broker drop to publish a BadNoCommunication snapshot");
|
||||
DataValueSnapshot staled;
|
||||
lock (raised) staled = raised[1];
|
||||
staled.StatusCode.ShouldBe(badNoCommunication);
|
||||
staled.Value.ShouldBe(21.5, "the last known value is retained through the outage");
|
||||
|
||||
// The reconnect's retained replay — the IDENTICAL payload.
|
||||
driver.Subscriptions.HandleMessage(topic, """{"value":21.5}"""u8.ToArray(), retained: true);
|
||||
|
||||
DataValueSnapshot recovered;
|
||||
lock (raised) { raised.Count.ShouldBe(3); recovered = raised[2]; }
|
||||
recovered.StatusCode.ShouldBe(good);
|
||||
recovered.Value.ShouldBe(21.5);
|
||||
}
|
||||
|
||||
private static async Task WaitForAsync(Func<bool> condition, string what)
|
||||
{
|
||||
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(10);
|
||||
|
||||
Reference in New Issue
Block a user