diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLeaseLapseWireTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLeaseLapseWireTests.cs index d9c683beb..777b0aaf9 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLeaseLapseWireTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLeaseLapseWireTests.cs @@ -44,9 +44,15 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests var id = ProjectedDataIds.Identifier("map", origin.Raw.Address); resolver.TryResolveProjected(DataRealm.Raw, id, out _).ShouldBeFalse("precondition: the lease has lapsed"); + // A non-Value attribute of the lapsed node is not access-denied either (retryable while the lease renews). + var name = (await client.ReadAsync(new RequestHeader(), 0, TimestampsToReturn.Neither, + [new ReadValueId { NodeId = anchor, AttributeId = Attributes.DisplayName }], ct)).Results[0]; + name.StatusCode.Code.ShouldNotBe(StatusCodes.BadUserAccessDenied.Code); var lapsed = await ReadValue(client, anchor, ct); lapsed.StatusCode.Code.ShouldNotBe(StatusCodes.BadUserAccessDenied.Code, "a lease lapse is not an authorization failure"); - lapsed.StatusCode.Code.ShouldBe(StatusCodes.BadWaitingForInitialData.Code); + // The attribute read above already started the renewal, which may have finished (and fetched the value) by now. + (StatusCode.IsGood(lapsed.StatusCode) || lapsed.StatusCode.Code == StatusCodes.BadWaitingForInitialData.Code || + lapsed.StatusCode.Code == StatusCodes.BadResourceUnavailable.Code).ShouldBeTrue(lapsed.StatusCode.ToString()); (await ReadUntilGood(client, anchor, ct)).ShouldBe(42); resolver.TryResolveProjected(DataRealm.Raw, id, out _).ShouldBeTrue("the read renewed the node's authority"); connections.BrowseCalls.ShouldBe(browses, "the renewal is an exact metadata refresh, not a re-browse"); @@ -66,7 +72,7 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests using var origin = new LocalDataFixture(servingGeneration: 1); origin.Publish(42); var connections = new CountingConnections(origin); - await WithComposedServer(origin, origin.Raw, async (session, _, _, _, _, _, anchor) => + await WithComposedServer(origin, origin.Raw, async (session, _, _, _, access, _, anchor) => { var client = (SessionClient)session; await BrowseAll(client, anchor, 1); @@ -79,6 +85,8 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests await Task.Delay(200, ct); connections.MetadataCalls.ShouldBe(metadata, "an unauthorized caller never triggers a renewal at the origin"); connections.ReadCalls.ShouldBe(0); + // Counted synchronously when a renewal is admitted, so this absence is not a race with a background task. + access.LapseRenewalsStartedForTests.ShouldBe(0); }, connectionSource: connections, localPermissions: BrowseOnlyGrant); } @@ -101,6 +109,30 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests }, localPermissions: 15, allowHistory: true, connectionSource: new PersistentConnections(origin)); } + [Fact(Timeout = 90000), Trait("Issue", "922")] + public async Task A_lapsed_history_read_whose_renewal_cannot_reach_the_origin_is_retryable_not_denied_and_recovers() + { + await using var originServer = await HistoryOrigin.StartAsync(7); + using var origin = new LocalDataFixture(servingGeneration: 1, history: originServer.Port); + var connections = new DownableConnections(new PersistentConnections(origin)); + await WithComposedServer(origin, origin.Raw, async (session, _, _, _, _, _, anchor) => + { + var client = (SessionClient)session; + await BrowseAll(client, anchor, 1); + origin.Clock.Advance(TimeSpan.FromSeconds(16)); + connections.Down = true; + var calls = originServer.Source.Calls; + var down = await RawHistory(client, anchor, 3, default); + down.StatusCode.Code.ShouldBe(StatusCodes.BadResourceUnavailable.Code, "granted, lapsed, origin unreachable: retryable"); + originServer.Source.Calls.ShouldBe(calls, "nothing is read under a lapsed lease"); + + connections.Down = false; + origin.Clock.Advance(ProjectedDataAccess.LapseRenewalBackoff + TimeSpan.FromMilliseconds(1)); // past the failure back-off + var recovered = await RawHistory(client, anchor, 3, default); + StatusCode.IsGood(recovered.StatusCode).ShouldBeTrue(recovered.StatusCode.ToString()); + }, localPermissions: 15, allowHistory: true, connectionSource: connections); + } + [Fact(Timeout = 90000), Trait("Issue", "922")] public async Task Without_the_local_history_grant_a_lapsed_history_read_is_still_BadUserAccessDenied_and_unsent() { @@ -176,6 +208,14 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests throw new ShouldAssertException($"the projected read never became Good (last {value.StatusCode})"); } + /// A connection source that can be switched to "no connection" (an unreachable origin / undeployed route). + private sealed class DownableConnections(IGrpcDataAdapterConnectionSource inner) : IGrpcDataAdapterConnectionSource + { + public volatile bool Down; + public ValueTask AcquireAsync(string connectionId, CancellationToken cancellationToken) => + Down ? ValueTask.FromResult(null) : inner.AcquireAsync(connectionId, cancellationToken); + } + /// Like FakeConnections, counting browse, metadata and read calls separately. private sealed class CountingConnections(LocalDataFixture peer) : IGrpcDataAdapterConnectionSource {