mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-28 22:55:49 +08:00
Convert `DataValue` from a mutable reference type (`class`) to a `readonly struct` to eliminate per-allocation GC pressure on every attribute read, subscription publish, and history sample. ### Key API changes (user-visible) | Before | After | Pattern | |---|---|---| | `class DataValue` | `readonly struct DataValue : INullable` | Foundation | | `Equals(DataValue?)` / `operator ==(DataValue?, DataValue?)` | `Equals(DataValue)` / `operator ==(DataValue, DataValue)` | Value semantics | | `IEncoder.WriteDataValue(string?, DataValue?)` | `IEncoder.WriteDataValue(string?, DataValue)` | Non-nullable wire API | | `IDecoder.ReadDataValue(...) -> DataValue?` | `IDecoder.ReadDataValue(...) -> DataValue` | Same | | `IDataChangeMonitoredItemQueue.PeekLast/Oldest()` | `TryPeekLastValue/Oldest(out DataValue)` | TryGet pattern | | `IAggregateCalculator.GetProcessedValue(bool)` | `TryGetProcessedValue(bool, out DataValue)` | Same | | `IUaPubSubDataStore.ReadPublishedDataItem(...)` | `TryReadPublishedDataItem(NodeId, uint, out DataValue)` | Same | | `Dictionary<uint, DataValue?>` (NodeCacheContext) | `Dictionary<uint, DataValue>` | Default + IsNull | | `IHistoryDataSource.FirstRaw/NextRaw` | `TryFirstRaw/TryNextRaw(out DataValue)` | TryGet pattern | | `NodeState.ReadAttribute(... DataValue)` | `NodeState.ReadAttribute(... ref DataValue)` | Sync mutation via ref | | `NodeState.ReadAttributeAsync(... DataValue)` | `NodeState.ReadAttributeAsync(...) -> ValueTask<(ServiceResult, DataValue)>` | Async return tuple | | `obj as DataValue` (heterogeneous Dictionary<string, object>) | `obj is DataValue dv ? dv : default(DataValue)` | Safe struct cast | | `Assert.That(dv, Is.Not.Null)` (tests) | `Assert.That(dv.IsNull, Is.False)` | Struct test pattern | | `[Obsolete] Value` setter, all property setters | Removed; use ctor or `With*()` | Immutability | | `[DataContract]` + `[DataMember]` | Removed; surrogate via `SerializableDataValue` + `DataValueSurrogateProvider` | Serialization | ### Wire format Wire format is **byte-identical** to the previous class-based implementation across binary / JSON / XML encoders. Default DataValue (`.IsNull == true`) is treated as the equivalent of the old null reference (writes JSON `null`, binary `byte 0`, XML omitted element). ### Why TryGet pattern (instead of leaving `DataValue?`) For methods that previously returned `DataValue?` to signal "no value available" (queue peek, aggregate result, data store lookup, history navigation), the conversion to `bool TryGet(out DataValue)` matches BCL conventions (`Dictionary.TryGetValue`, `Queue.TryPeek`, `Int32.TryParse`) and gives the caller an explicit "may be absent" affordance without `Nullable<DataValue>` overhead. ### Breaking changes summary - `DataValue` is now a `readonly struct` - boxing/unboxing semantics change for callers using `object`-typed APIs. - `IEncoder.WriteDataValue` / `IDecoder.ReadDataValue` / `ReadDataValueArray` interface signatures lose the `Nullable` wrapper. - `IDataChangeMonitoredItemQueue.PeekLastValue` / `PeekOldestValue` -> `TryPeekLastValue` / `TryPeekOldestValue` (out parameter). - `IAggregateCalculator.GetProcessedValue` -> `TryGetProcessedValue` (out parameter). - `IUaPubSubDataStore.ReadPublishedDataItem` -> `TryReadPublishedDataItem` (out parameter). - `NodeState.ReadAttribute` takes `ref DataValue`; `ReadAttributeAsync` returns `ValueTask<(ServiceResult, DataValue)>`. - `DataValue` property setters removed (use constructor + `With*` mutators).
146 lines
5.4 KiB
C#
146 lines
5.4 KiB
C#
/* ====/* ========================================================================
|
|
* 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 NUnit.Framework;
|
|
|
|
namespace Opc.Ua.PubSub.Tests.Configuration
|
|
{
|
|
[TestFixture(Description = "Tests for UaPubSubDataStore class")]
|
|
[Parallelizable]
|
|
public class UaPubSubDataStoreTests
|
|
{
|
|
[Test(Description = "Validate WritePublishedDataItem call with different values")]
|
|
public void ValidateWritePublishedDataItem(
|
|
[Values(
|
|
true,
|
|
(byte)1,
|
|
(ushort)2,
|
|
(short)3,
|
|
(uint)4,
|
|
5,
|
|
(ulong)6,
|
|
(long)7,
|
|
(double)8,
|
|
(float)9,
|
|
"10")]
|
|
object value)
|
|
{
|
|
//Arrange
|
|
var dataStore = new UaPubSubDataStore();
|
|
var nodeId = NodeId.Parse("ns=1;i=1");
|
|
|
|
//Act
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
dataStore.WritePublishedDataItem(
|
|
nodeId,
|
|
Attributes.Value,
|
|
new DataValue(new Variant(value)));
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
|
dataStore.TryReadPublishedDataItem(nodeId, Attributes.Value, out DataValue readDataValue);
|
|
|
|
//Assert
|
|
Assert.That(
|
|
readDataValue.IsNull,
|
|
Is.False,
|
|
"Returned DataValue for written nodeId and attribute is null");
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
Assert.That(
|
|
value,
|
|
Is.EqualTo(readDataValue.Value),
|
|
"Read after write returned different value");
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
|
}
|
|
|
|
[Test(Description = "Validate WritePublishedDataItem call with null NodeId")]
|
|
public void ValidateWritePublishedDataItemNullNodeId()
|
|
{
|
|
//Arrange
|
|
var dataStore = new UaPubSubDataStore();
|
|
|
|
//Assert
|
|
Assert
|
|
.Throws<ArgumentException>(() => dataStore.WritePublishedDataItem(default));
|
|
}
|
|
|
|
[Test(Description = "Validate WritePublishedDataItem call with invalid Attribute")]
|
|
public void ValidateWritePublishedDataItemInvalidAttribute()
|
|
{
|
|
//Arrange
|
|
var dataStore = new UaPubSubDataStore();
|
|
|
|
//Assert
|
|
Assert.Throws<ArgumentException>(() =>
|
|
dataStore.WritePublishedDataItem(
|
|
NodeId.Parse("ns=0;i=2253"),
|
|
Attributes.AccessLevelEx + 1));
|
|
}
|
|
|
|
[Test(Description = "Validate ReadPublishedDataItem call for non existing node id")]
|
|
public void ValidateReadPublishedDataItem()
|
|
{
|
|
//Arrange
|
|
var dataStore = new UaPubSubDataStore();
|
|
var nodeId = NodeId.Parse("ns=1;i=1");
|
|
|
|
//Act
|
|
dataStore.TryReadPublishedDataItem(nodeId, Attributes.Value, out DataValue readDataValue);
|
|
|
|
//Assert
|
|
Assert.That(
|
|
readDataValue.IsNull,
|
|
Is.True,
|
|
"Returned DataValue for written nodeId and attribute is NOT null");
|
|
}
|
|
|
|
[Test(Description = "Validate ReadPublishedDataItem call with null NodeId")]
|
|
public void ValidateReadPublishedDataItemNullNodeId()
|
|
{
|
|
//Arrange
|
|
var dataStore = new UaPubSubDataStore();
|
|
|
|
//Assert
|
|
Assert
|
|
.Throws<ArgumentException>(() => dataStore.TryReadPublishedDataItem(default, Attributes.Value, out _));
|
|
}
|
|
|
|
[Test(Description = "Validate ReadPublishedDataItem call with invalid Attribute")]
|
|
public void ValidateReadPublishedDataIteminvalidAttribute()
|
|
{
|
|
//Arrange
|
|
var dataStore = new UaPubSubDataStore();
|
|
//Assert
|
|
Assert.Throws<ArgumentException>(() =>
|
|
dataStore.TryReadPublishedDataItem(
|
|
NodeId.Parse("ns=0;i=2253"),
|
|
Attributes.AccessLevelEx + 1, out _));
|
|
}
|
|
}
|
|
}
|