From f1e1ffcbcdf9f3e9c1e8b1ecdcbd3247db77aa14 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 13:32:40 +0000 Subject: [PATCH 1/2] Initial plan From 18a1ff9a192cb4227d8a5d88d03f6401f976a7ca Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 26 Jan 2026 13:52:16 +0000 Subject: [PATCH 2/2] Implement OperationLimits min logic and add tests Co-authored-by: romanett <7413710+romanett@users.noreply.github.com> --- Libraries/Opc.Ua.Client/Session/Session.cs | 101 ++++++++--- Tests/Opc.Ua.Client.Tests/SessionMock.cs | 2 +- Tests/Opc.Ua.Client.Tests/SessionTests.cs | 198 +++++++++++++++++++++ 3 files changed, 273 insertions(+), 28 deletions(-) diff --git a/Libraries/Opc.Ua.Client/Session/Session.cs b/Libraries/Opc.Ua.Client/Session/Session.cs index 06c3c5ceb2..f9dc7953dd 100644 --- a/Libraries/Opc.Ua.Client/Session/Session.cs +++ b/Libraries/Opc.Ua.Client/Session/Session.cs @@ -223,6 +223,25 @@ private Session( DefaultSubscription.MinLifetimeInterval = (uint)m_configuration.ClientConfiguration .MinSubscriptionLifetime; + // initialize operation limits from client configuration. + if (m_configuration.ClientConfiguration.OperationLimits != null) + { + OperationLimits clientLimits = m_configuration.ClientConfiguration.OperationLimits; + OperationLimits.MaxNodesPerRead = clientLimits.MaxNodesPerRead; + OperationLimits.MaxNodesPerHistoryReadData = clientLimits.MaxNodesPerHistoryReadData; + OperationLimits.MaxNodesPerHistoryReadEvents = clientLimits.MaxNodesPerHistoryReadEvents; + OperationLimits.MaxNodesPerWrite = clientLimits.MaxNodesPerWrite; + OperationLimits.MaxNodesPerHistoryUpdateData = clientLimits.MaxNodesPerHistoryUpdateData; + OperationLimits.MaxNodesPerHistoryUpdateEvents = clientLimits.MaxNodesPerHistoryUpdateEvents; + OperationLimits.MaxNodesPerMethodCall = clientLimits.MaxNodesPerMethodCall; + OperationLimits.MaxNodesPerBrowse = clientLimits.MaxNodesPerBrowse; + OperationLimits.MaxNodesPerRegisterNodes = clientLimits.MaxNodesPerRegisterNodes; + OperationLimits.MaxNodesPerTranslateBrowsePathsToNodeIds = + clientLimits.MaxNodesPerTranslateBrowsePathsToNodeIds; + OperationLimits.MaxNodesPerNodeManagement = clientLimits.MaxNodesPerNodeManagement; + OperationLimits.MaxMonitoredItemsPerCall = clientLimits.MaxMonitoredItemsPerCall; + } + NamespaceUris = messageContext.NamespaceUris; ServerUris = messageContext.ServerUris; Factory = messageContext.Factory; @@ -1835,6 +1854,35 @@ public async Task FetchTypeTreeAsync( public async Task FetchOperationLimitsAsync(CancellationToken ct) { using Activity? activity = m_telemetry.StartActivity(); + + // Helper extraction + static T Get(ref int index, IList values, IList errors) + where T : struct + { + DataValue value = values[index]; + ServiceResult error = errors.Count > 0 ? errors[index] : ServiceResult.Good; + index++; + if (ServiceResult.IsNotBad(error) && value.Value is T retVal) + { + return retVal; + } + return default; + } + + // Apply operation limit logic: if client value is 0, use server value; otherwise use min(client, server) + static uint ApplyOperationLimit(uint clientLimit, uint serverLimit) + { + if (clientLimit == 0) + { + return serverLimit; + } + if (serverLimit == 0) + { + return clientLimit; + } + return Math.Min(clientLimit, serverLimit); + } + // First we read the node read max to optimize the second read. var nodeIds = new List { @@ -1843,7 +1891,7 @@ public async Task FetchOperationLimitsAsync(CancellationToken ct) (DataValueCollection values, IList errors) = await this.ReadValuesAsync(nodeIds, ct).ConfigureAwait(false); int index = 0; - OperationLimits.MaxNodesPerRead = Get(ref index, values, errors); + OperationLimits.MaxNodesPerRead = ApplyOperationLimit(OperationLimits.MaxNodesPerRead, Get(ref index, values, errors)); nodeIds = [ @@ -1878,18 +1926,31 @@ public async Task FetchOperationLimitsAsync(CancellationToken ct) (values, errors) = await this.ReadValuesAsync(nodeIds, ct).ConfigureAwait(false); index = 0; - OperationLimits.MaxNodesPerHistoryReadData = Get(ref index, values, errors); - OperationLimits.MaxNodesPerHistoryReadEvents = Get(ref index, values, errors); - OperationLimits.MaxNodesPerWrite = Get(ref index, values, errors); - OperationLimits.MaxNodesPerRead = Get(ref index, values, errors); - OperationLimits.MaxNodesPerHistoryUpdateData = Get(ref index, values, errors); - OperationLimits.MaxNodesPerHistoryUpdateEvents = Get(ref index, values, errors); - OperationLimits.MaxNodesPerMethodCall = Get(ref index, values, errors); - OperationLimits.MaxNodesPerBrowse = Get(ref index, values, errors); - OperationLimits.MaxNodesPerRegisterNodes = Get(ref index, values, errors); - OperationLimits.MaxNodesPerNodeManagement = Get(ref index, values, errors); - OperationLimits.MaxMonitoredItemsPerCall = Get(ref index, values, errors); - OperationLimits.MaxNodesPerTranslateBrowsePathsToNodeIds = Get(ref index, values, errors); + OperationLimits.MaxNodesPerHistoryReadData = ApplyOperationLimit( + OperationLimits.MaxNodesPerHistoryReadData, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerHistoryReadEvents = ApplyOperationLimit( + OperationLimits.MaxNodesPerHistoryReadEvents, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerWrite = ApplyOperationLimit( + OperationLimits.MaxNodesPerWrite, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerRead = ApplyOperationLimit( + OperationLimits.MaxNodesPerRead, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerHistoryUpdateData = ApplyOperationLimit( + OperationLimits.MaxNodesPerHistoryUpdateData, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerHistoryUpdateEvents = ApplyOperationLimit( + OperationLimits.MaxNodesPerHistoryUpdateEvents, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerMethodCall = ApplyOperationLimit( + OperationLimits.MaxNodesPerMethodCall, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerBrowse = ApplyOperationLimit( + OperationLimits.MaxNodesPerBrowse, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerRegisterNodes = ApplyOperationLimit( + OperationLimits.MaxNodesPerRegisterNodes, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerNodeManagement = ApplyOperationLimit( + OperationLimits.MaxNodesPerNodeManagement, Get(ref index, values, errors)); + OperationLimits.MaxMonitoredItemsPerCall = ApplyOperationLimit( + OperationLimits.MaxMonitoredItemsPerCall, Get(ref index, values, errors)); + OperationLimits.MaxNodesPerTranslateBrowsePathsToNodeIds = ApplyOperationLimit( + OperationLimits.MaxNodesPerTranslateBrowsePathsToNodeIds, + Get(ref index, values, errors)); ServerCapabilities.MaxBrowseContinuationPoints = Get(ref index, values, errors); ServerCapabilities.MaxHistoryContinuationPoints = Get(ref index, values, errors); ServerCapabilities.MaxQueryContinuationPoints = Get(ref index, values, errors); @@ -1906,20 +1967,6 @@ public async Task FetchOperationLimitsAsync(CancellationToken ct) ServerCapabilities.MaxWhereClauseParameters = Get(ref index, values, errors); ServerCapabilities.MaxSelectClauseParameters = Get(ref index, values, errors); - // Helper extraction - static T Get(ref int index, IList values, IList errors) - where T : struct - { - DataValue value = values[index]; - ServiceResult error = errors.Count > 0 ? errors[index] : ServiceResult.Good; - index++; - if (ServiceResult.IsNotBad(error) && value.Value is T retVal) - { - return retVal; - } - return default; - } - uint maxByteStringLength = (uint?)m_configuration.TransportQuotas?.MaxByteStringLength ?? 0u; if (maxByteStringLength != 0 && (ServerCapabilities.MaxByteStringLength == 0 || diff --git a/Tests/Opc.Ua.Client.Tests/SessionMock.cs b/Tests/Opc.Ua.Client.Tests/SessionMock.cs index 8f9d981da0..e89361cd1b 100644 --- a/Tests/Opc.Ua.Client.Tests/SessionMock.cs +++ b/Tests/Opc.Ua.Client.Tests/SessionMock.cs @@ -52,7 +52,7 @@ public sealed class SessionMock : Session /// /// Create the mock /// - private SessionMock( + internal SessionMock( Mock channel, ApplicationConfiguration configuration, ConfiguredEndpoint endpoint) diff --git a/Tests/Opc.Ua.Client.Tests/SessionTests.cs b/Tests/Opc.Ua.Client.Tests/SessionTests.cs index 32947fea60..703b983d25 100644 --- a/Tests/Opc.Ua.Client.Tests/SessionTests.cs +++ b/Tests/Opc.Ua.Client.Tests/SessionTests.cs @@ -275,6 +275,204 @@ public void FetchOperationLimitsAsyncShouldHandleTimeout() sut.Channel.Verify(); } + [Test] + public async Task FetchOperationLimitsAsyncShouldApplyClientLimitsWhenSmaller() + { + ITelemetryContext telemetry = NUnitTelemetryContext.Create(); + var channel = new Mock(); + channel + .SetupGet(s => s.MessageContext) + .Returns(new ServiceMessageContext(telemetry)); + channel + .SetupGet(s => s.SupportedFeatures) + .Returns(TransportChannelFeatures.Reconnect); + + // Configure client with smaller limits than server + var configuration = new ApplicationConfiguration(telemetry) + { + ClientConfiguration = new ClientConfiguration + { + OperationLimits = new OperationLimits + { + MaxNodesPerRead = 500, + MaxNodesPerWrite = 2000, + MaxNodesPerBrowse = 4000 + } + } + }; + + var endpoint = new EndpointDescription + { + SecurityMode = MessageSecurityMode.None, + SecurityPolicyUri = SecurityPolicies.None, + EndpointUrl = "opc.tcp://localhost:4840" + }; + + var sut = new SessionMock( + channel, + configuration, + new ConfiguredEndpoint(null, endpoint)); + + CancellationToken ct = CancellationToken.None; + + // Server returns larger values + var dataValues = new DataValueCollection + { + new DataValue(new Variant(1000u)), // MaxNodesPerHistoryReadData - no client limit + new DataValue(new Variant(1000u)), // MaxNodesPerHistoryReadEvents - no client limit + new DataValue(new Variant(5000u)), // MaxNodesPerWrite - client has 2000 + new DataValue(new Variant(1000u)), // MaxNodesPerRead - client has 500 + new DataValue(new Variant(1000u)), // MaxNodesPerHistoryUpdateData + new DataValue(new Variant(1000u)), // MaxNodesPerHistoryUpdateEvents + new DataValue(new Variant(1000u)), // MaxNodesPerMethodCall + new DataValue(new Variant(10000u)), // MaxNodesPerBrowse - client has 4000 + new DataValue(new Variant(1000u)), // MaxNodesPerRegisterNodes + new DataValue(new Variant(1000u)), // MaxNodesPerNodeManagement + new DataValue(new Variant(1000u)), // MaxMonitoredItemsPerCall + new DataValue(new Variant(1000u)), // MaxNodesPerTranslateBrowsePathsToNodeIds + new DataValue(new Variant((ushort)100)), + new DataValue(new Variant((ushort)100)), + new DataValue(new Variant((ushort)100)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(100.0)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)) + }; + + channel + .SetupSequence(c => c.SendRequestAsync( + It.IsAny(), + It.IsAny())) + .Returns(new ValueTask(new ReadResponse + { + Results = [new DataValue(new Variant(1000u))], + DiagnosticInfos = [] + })) + .Returns(new ValueTask(new ReadResponse + { + Results = dataValues, + DiagnosticInfos = [] + })); + + // Act + await sut.FetchOperationLimitsAsync(ct).ConfigureAwait(false); + + // Assert - should use smaller of client and server values + Assert.That(sut.OperationLimits.MaxNodesPerRead, Is.EqualTo(500)); // min(500, 1000) + Assert.That(sut.OperationLimits.MaxNodesPerWrite, Is.EqualTo(2000)); // min(2000, 5000) + Assert.That(sut.OperationLimits.MaxNodesPerBrowse, Is.EqualTo(4000)); // min(4000, 10000) + Assert.That(sut.OperationLimits.MaxNodesPerHistoryReadData, Is.EqualTo(1000)); // 0 -> server + Assert.That(sut.OperationLimits.MaxNodesPerHistoryReadEvents, Is.EqualTo(1000)); // 0 -> server + + channel.Verify(); + } + + [Test] + public async Task FetchOperationLimitsAsyncShouldApplyServerLimitsWhenSmaller() + { + ITelemetryContext telemetry = NUnitTelemetryContext.Create(); + var channel = new Mock(); + channel + .SetupGet(s => s.MessageContext) + .Returns(new ServiceMessageContext(telemetry)); + channel + .SetupGet(s => s.SupportedFeatures) + .Returns(TransportChannelFeatures.Reconnect); + + // Configure client with larger limits than server + var configuration = new ApplicationConfiguration(telemetry) + { + ClientConfiguration = new ClientConfiguration + { + OperationLimits = new OperationLimits + { + MaxNodesPerRead = 2000, + MaxNodesPerWrite = 10000, + MaxNodesPerBrowse = 8000 + } + } + }; + + var endpoint = new EndpointDescription + { + SecurityMode = MessageSecurityMode.None, + SecurityPolicyUri = SecurityPolicies.None, + EndpointUrl = "opc.tcp://localhost:4840" + }; + + var sut = new SessionMock( + channel, + configuration, + new ConfiguredEndpoint(null, endpoint)); + + CancellationToken ct = CancellationToken.None; + + // Server returns smaller values + var dataValues = new DataValueCollection + { + new DataValue(new Variant(1000u)), // MaxNodesPerHistoryReadData + new DataValue(new Variant(1000u)), // MaxNodesPerHistoryReadEvents + new DataValue(new Variant(5000u)), // MaxNodesPerWrite - server has 5000, client has 10000 + new DataValue(new Variant(1000u)), // MaxNodesPerRead - server has 1000, client has 2000 + new DataValue(new Variant(1000u)), // MaxNodesPerHistoryUpdateData + new DataValue(new Variant(1000u)), // MaxNodesPerHistoryUpdateEvents + new DataValue(new Variant(1000u)), // MaxNodesPerMethodCall + new DataValue(new Variant(5000u)), // MaxNodesPerBrowse - server has 5000, client has 8000 + new DataValue(new Variant(1000u)), // MaxNodesPerRegisterNodes + new DataValue(new Variant(1000u)), // MaxNodesPerNodeManagement + new DataValue(new Variant(1000u)), // MaxMonitoredItemsPerCall + new DataValue(new Variant(1000u)), // MaxNodesPerTranslateBrowsePathsToNodeIds + new DataValue(new Variant((ushort)100)), + new DataValue(new Variant((ushort)100)), + new DataValue(new Variant((ushort)100)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(100.0)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)), + new DataValue(new Variant(1000u)) + }; + + channel + .SetupSequence(c => c.SendRequestAsync( + It.IsAny(), + It.IsAny())) + .Returns(new ValueTask(new ReadResponse + { + Results = [new DataValue(new Variant(1000u))], + DiagnosticInfos = [] + })) + .Returns(new ValueTask(new ReadResponse + { + Results = dataValues, + DiagnosticInfos = [] + })); + + // Act + await sut.FetchOperationLimitsAsync(ct).ConfigureAwait(false); + + // Assert - should use smaller of client and server values + Assert.That(sut.OperationLimits.MaxNodesPerRead, Is.EqualTo(1000)); // min(2000, 1000) + Assert.That(sut.OperationLimits.MaxNodesPerWrite, Is.EqualTo(5000)); // min(10000, 5000) + Assert.That(sut.OperationLimits.MaxNodesPerBrowse, Is.EqualTo(5000)); // min(8000, 5000) + + channel.Verify(); + } + [Test] public async Task FetchNamespaceTablesAsyncShouldFetchAndUpdateTablesAsync() {