Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions Libraries/Opc.Ua.Client/Subscription/MonitoredItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public virtual void Restore(MonitoredItemState state)
ServerId = state.ServerId;
TriggeringItemId = state.TriggeringItemId;
TriggeredItems = state.TriggeredItems != null ? new UInt32Collection(state.TriggeredItems) : null;
CacheQueueSize = state.CacheQueueSize < 1 ? 1 : state.CacheQueueSize;
}

/// <inheritdoc/>
Expand All @@ -148,7 +149,8 @@ public virtual void Snapshot(out MonitoredItemState state)
ServerId = Status.Id,
ClientId = ClientHandle,
TriggeringItemId = TriggeringItemId,
TriggeredItems = TriggeredItems != null ? new UInt32Collection(TriggeredItems) : null
TriggeredItems = TriggeredItems != null ? new UInt32Collection(TriggeredItems) : null,
CacheQueueSize = CacheQueueSize
};
}

Expand Down Expand Up @@ -401,7 +403,7 @@ public NodeId ResolvedNodeId
/// <summary>
/// Returns the queue size used by the cache.
/// </summary>
public int CacheQueueSize
public uint CacheQueueSize
{
get
{
Expand Down Expand Up @@ -1122,7 +1124,7 @@ public class MonitoredItemDataCache
/// <summary>
/// Constructs a cache for a monitored item.
/// </summary>
public MonitoredItemDataCache(ITelemetryContext? telemetry, int queueSize = 1)
public MonitoredItemDataCache(ITelemetryContext? telemetry, uint queueSize = 1)
{
QueueSize = queueSize;
m_logger = telemetry.CreateLogger<MonitoredItemDataCache>();
Expand All @@ -1139,7 +1141,7 @@ public MonitoredItemDataCache(ITelemetryContext? telemetry, int queueSize = 1)
/// <summary>
/// The size of the queue to maintain.
/// </summary>
public int QueueSize { get; private set; }
public uint QueueSize { get; private set; }

/// <summary>
/// The last value received from the server.
Expand All @@ -1155,12 +1157,8 @@ public IList<DataValue> Publish()
if (m_values != null)
{
values = new List<DataValue>(m_values.Count);
for (int ii = 0; ii < values.Count; ii++)
while (m_values.TryDequeue(out DataValue? dequeued))
{
if (!m_values.TryDequeue(out DataValue? dequeued))
{
break;
}
values.Add(dequeued);
}
}
Expand Down Expand Up @@ -1222,7 +1220,7 @@ public void OnNotification(MonitoredItemNotification notification)
/// <summary>
/// Changes the queue size.
/// </summary>
public void SetQueueSize(int queueSize)
public void SetQueueSize(uint queueSize)
{
if (queueSize == QueueSize)
{
Expand Down Expand Up @@ -1270,7 +1268,7 @@ public class MonitoredItemEventCache
/// <summary>
/// Constructs a cache for a monitored item.
/// </summary>
public MonitoredItemEventCache(int queueSize)
public MonitoredItemEventCache(uint queueSize)
{
QueueSize = queueSize;
m_events = new Queue<EventFieldList>();
Expand All @@ -1279,7 +1277,7 @@ public MonitoredItemEventCache(int queueSize)
/// <summary>
/// The size of the queue to maintain.
/// </summary>
public int QueueSize { get; private set; }
public uint QueueSize { get; private set; }

/// <summary>
/// The last event received.
Expand Down Expand Up @@ -1318,7 +1316,7 @@ public void OnNotification(EventFieldList notification)
/// <summary>
/// Changes the queue size.
/// </summary>
public void SetQueueSize(int queueSize)
public void SetQueueSize(uint queueSize)
{
if (queueSize == QueueSize)
{
Expand Down
6 changes: 6 additions & 0 deletions Libraries/Opc.Ua.Client/Subscription/MonitoredItemState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public MonitoredItemState(MonitoredItemOptions options)
/// </summary>
[DataMember(Order = 17)]
public UInt32Collection? TriggeredItems { get; init; }

/// <summary>
/// The queue size used by the client-side cache.
/// </summary>
[DataMember(Order = 18)]
public uint CacheQueueSize { get; init; }
}

/// <summary>
Expand Down
188 changes: 188 additions & 0 deletions Tests/Opc.Ua.Client.Tests/MonitoredItemTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Opc.Ua.Tests;

namespace Opc.Ua.Client.Tests
{
[TestFixture]
[Category("Client")]
[Category("MonitoredItem")]
[SetCulture("en-us")]
[SetUICulture("en-us")]
public sealed class MonitoredItemTests
{
[Test]
public void SaveValueInCacheShouldOverwriteWithQueueSizeOne()
{
ITelemetryContext telemetry = NUnitTelemetryContext.Create();
var monitoredItem = new MonitoredItem(telemetry) { CacheQueueSize = 1 };

var notification1 = new MonitoredItemNotification
{
ClientHandle = monitoredItem.ClientHandle,
Value = new DataValue(new Variant(100), StatusCodes.Good, DateTime.UtcNow)
};
monitoredItem.SaveValueInCache(notification1);

var notification2 = new MonitoredItemNotification
{
ClientHandle = monitoredItem.ClientHandle,
Value = new DataValue(new Variant(200), StatusCodes.Good, DateTime.UtcNow)
};
monitoredItem.SaveValueInCache(notification2);

IList<DataValue> result = monitoredItem.DequeueValues();

Assert.That(result.Count, Is.EqualTo(1));
Assert.That(result[0].Value, Is.EqualTo(200));
}

[Test]
public void DequeueValuesShouldReturnAllQueuedValues()
{
ITelemetryContext telemetry = NUnitTelemetryContext.Create();
var monitoredItem = new MonitoredItem(telemetry) { CacheQueueSize = 5 };

List<int> expectedValues = [1, 2, 3, 4, 5];
List<MonitoredItemNotification> notifications = expectedValues
.ConvertAll(value => new MonitoredItemNotification
{
ClientHandle = monitoredItem.ClientHandle,
Value = new DataValue(new Variant(value), StatusCodes.Good, DateTime.UtcNow)
});

foreach (MonitoredItemNotification notification in notifications)
{
monitoredItem.SaveValueInCache(notification);
}

IList<DataValue> result = monitoredItem.DequeueValues();

Assert.That(result.Count, Is.EqualTo(expectedValues.Count));
Assert.That(result.Select(x => x.Value), Is.EquivalentTo(expectedValues));

// Ensure the cache is empty after dequeue
IList<DataValue> emptyResult = monitoredItem.DequeueValues();
Assert.That(emptyResult, Is.Empty);
}

[Test]
public void SaveValueInCacheShouldOverwriteOldestValues()
{
ITelemetryContext telemetry = NUnitTelemetryContext.Create();
const int kQueueSize = 5;
var monitoredItem = new MonitoredItem(telemetry) { CacheQueueSize = kQueueSize };

List<int> values = [1, 2, 3, 4, 5, 6, 7];
List<MonitoredItemNotification> notifications = values
.ConvertAll(value => new MonitoredItemNotification
{
ClientHandle = monitoredItem.ClientHandle,
Value = new DataValue(new Variant(value), StatusCodes.Good, DateTime.UtcNow)
});

foreach (MonitoredItemNotification notification in notifications)
{
monitoredItem.SaveValueInCache(notification);
}

IList<DataValue> result = monitoredItem.DequeueValues();

Assert.That(result.Count, Is.EqualTo(kQueueSize));
Assert.That(result.Select(x => x.Value), Is.EquivalentTo(values.Skip(2)));
}

[Test]
public void SerializeDeserializeShouldHaveSameProperties()
{
ITelemetryContext telemetry = NUnitTelemetryContext.Create();

var originalSession = SessionMock.Create();
var originalSubscription = new TestableSubscription(telemetry);

var monitoredItems = new List<MonitoredItem>
{
new(telemetry)
{
DisplayName = "MonitoredItem1", QueueSize = 10, CacheQueueSize = 1, SamplingInterval = 500
},
new(telemetry)
{
DisplayName = "MonitoredItem2", QueueSize = 25, CacheQueueSize = 50, SamplingInterval = 500
},
new(telemetry)
{
DisplayName = "MonitoredItem3", QueueSize = 0, CacheQueueSize = 0, SamplingInterval = 500
}
};

// CacheQueueSize of 0 is invalid and should be set to 1 internally
Assert.That(monitoredItems[^1].CacheQueueSize, Is.EqualTo(1));

originalSubscription.AddItems(monitoredItems);
originalSession.AddSubscription(originalSubscription);

using var stream = new MemoryStream();
originalSession.Save(stream, [originalSubscription]);
stream.Position = 0;

var loadedSession = SessionMock.Create();
loadedSession.Load(stream);
Assert.That(loadedSession.Subscriptions.Count(), Is.EqualTo(1));
Assert.That(loadedSession.Subscriptions.First().MonitoredItems.Count(), Is.EqualTo(monitoredItems.Count));

List<MonitoredItemState> originalStates = monitoredItems
.ConvertAll(item =>
{
item.Snapshot(out MonitoredItemState state);
return state;
});

var loadedItems = loadedSession.Subscriptions.First().MonitoredItems.ToList();
List<MonitoredItemState> loadedStates = loadedItems
.ConvertAll(item =>
{
item.Snapshot(out MonitoredItemState state);
return state;
});

for (int i = 0; i < monitoredItems.Count; i++)
{
Assert.That(loadedStates[i] with { Timestamp = default },
Is.EqualTo(originalStates[i] with { Timestamp = default }));
}
}
}
}
Loading