mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-23 02:00:00 +08:00
0383472ebc
### Summary Three related gaps mean no `PublishedDataSet` can produce a message that is not a key frame or a derived delta frame. `PubSubDataSetMessageType.Event` and the `ua-event` wire tag of Part 14 §7.2.5.4 are modelled but unreachable. This PR also corrects the JSON `DataValue` status member to the Part 6 Table 42 name `Status`. ### 1. A dataset cannot declare what its sample is `PublishedDataSetSnapshot` carries a metadata version, fields and a timestamp. `WriterGroup.BuildDataSetMessageAsync` therefore decides the message kind alone: `KeyFrameCount <= 1` forces `KeyFrame`, otherwise it derives a delta by comparing the snapshot **positionally** against the previous one. That derivation assumes successive snapshots describe the same fields in the same order, which holds only for a source returning its complete current state every cycle. A source whose samples are *occurrences* — an event, a condition snapshot, or a delta it has already computed — cannot satisfy it: consecutive samples carry different fields, so field *i* of one occurrence is compared against field *i* of an unrelated earlier one, and `Math.Min` silently truncates when the counts differ. `PublishedDataSetSnapshot` now carries an optional `MessageType`. When set, the writer publishes exactly the supplied fields under the declared type and leaves its key-frame state untouched. When null — the default, and every existing caller — behaviour is unchanged. ### 2. The event writer was unreachable `EventDataSetWriter` and `EventPublishedDataSet` implement the Part 14 §6.2.4 event model correctly, but nothing could use them: - `EventPublishedDataSet` did not implement `IPublishedDataSet`, so `PubSubApplication` could not register an event dataset; - nothing in the library constructed `EventDataSetWriter`, and it was not an `IDataSetWriter`; - `WriterGroup` appends at most one message per writer per cycle, whereas an event writer produces one per occurrence. Now: `EventPublishedDataSet` is an `IPublishedDataSet` whose general `SampleAsync` drains one occurrence per cycle declared as an event; `EventDataSetWriter` is an `IDataSetWriter`; `PubSubApplication` builds an event dataset when the configured `DataSetSource` is a `PublishedEventsDataType` and the registered source is an `IEventSampler`; and a writer group drains every pending occurrence in one publish cycle. Event-writer sequence numbers now use the WriterGroup checkpoint state, so failover and restart resume above the shared checkpoint. The rows-returning `EventPublishedDataSet.SampleAsync` is renamed `SampleEventsAsync`, both to make room for the interface member and to match `IEventSampler.SampleEventsAsync`. ### 3. A metadata announcement hides its own payload `JsonMetaDataMessage` documents that the payload *"is exposed both on the base `PubSubNetworkMessage.MetaData` property and on `MetaDataPayload` so callers can use whichever accessor matches their fluent style."* `MetaDataPublisher` set only `MetaDataPayload`, so the documented contract was false: any consumer, transform or encoder hook reading the base property saw an announcement as a message carrying nothing. Both are now populated. Dataset subscriptions also use a dispose-safe subscribe/recheck handshake, preventing a concurrent shutdown from leaving a late handler attached. ### 4. JSON DataValue status used the wrong member name Part 6 section 5.4.2.18 Table 42 names the `DataValue` status member `Status`; `StatusCode` is its data type. `JsonEncoder` and `JsonDecoder` instead shared the member name `StatusCode`, so stack-to-stack round-trips passed while standards-conformant peers could not exchange non-Good status values. Both directions now use `Status`. The encoder regression test also verifies that `StatusCode` is absent from the emitted `DataValue` object. Fixes #4111. ### Compatibility The snapshot property is optional and defaults to null, so existing PubSub sources and current-state sampling behavior remain unchanged. The `EventPublishedDataSet.SampleAsync` rename is source-breaking on a type that could not previously be reached from the runtime. The JSON `DataValue` wire member intentionally changes from `StatusCode` to the specification-defined `Status`. No legacy alias is retained: the encoder emits `Status`, and the decoder expects `Status`. ### Testing `Opc.Ua.PubSub.Tests` on `net10.0`: 1341 passed, 1 skipped, 0 failed. New coverage: - `WriterGroupDeclaredMessageTypeTests` — a declared type is honoured, declared samples never become delta frames and are never suppressed as unchanged, and an undeclared source keeps the existing key-frame cadence. - `EventDataSetWriterTests.EventPublishedDataSet_SamplesOneOccurrencePerSnapshotAsync` — the general contract drains one occurrence per call, declares it as an event, and reports nothing when exhausted. - `WriterGroupEventWriterHotStandbyContinuesSequenceNumberFromSharedCheckpointAsync` — event messages share the checkpointed sequence state and continue monotonically after standby promotion. Issue #4111 validation: - `Opc.Ua.Types.Tests` JSON encoder/decoder fixtures on `net10.0`: 274 passed. - `Opc.Ua.Core.Encoders.Tests` JSON conformance cases on `net10.0`: 604 passed. - `Opc.Ua.PubSub.Tests` JSON fixtures on `net10.0`: 88 passed. - Targeted `net48` regression tests: 2 passed; core JSON conformance cases: 572 passed. - Review-feedback fixtures on `net48`: 11 passed. CI-fix validation: - `Opc.Ua.Core.Tests` on `net10.0`: 4145 passed, 86 skipped. - `Opc.Ua.Core.Tests` on `net48`: 4070 passed, 92 skipped. - Patch coverage check passes after adding targeted metadata and event publishing tests. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1a300bc8-8bde-4a82-8fc3-476ef837bc6a Copilot-Session: 9f18bf87-795f-40a9-8494-b6e6334c4a97
283 lines
12 KiB
C#
283 lines
12 KiB
C#
/* ========================================================================
|
|
* Copyright (c) 2005-2026 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.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Time.Testing;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.PubSub.DataSets;
|
|
using Opc.Ua.PubSub.Encoding;
|
|
using Opc.Ua.PubSub.Groups;
|
|
using Opc.Ua.PubSub.MetaData;
|
|
using UadpDataSetMessageV2 = Opc.Ua.PubSub.Encoding.Uadp.UadpDataSetMessage;
|
|
|
|
namespace Opc.Ua.PubSub.Tests.Groups
|
|
{
|
|
/// <summary>
|
|
/// Validates the event-mode publisher: <see cref="EventDataSetWriter"/>
|
|
/// drains pending events from the
|
|
/// <see cref="EventPublishedDataSet"/> sampler, projects them via
|
|
/// <see cref="SimpleAttributeOperand"/>s and emits one
|
|
/// <see cref="PubSubDataSetMessage"/> per event with
|
|
/// <see cref="PubSubDataSetMessageType.Event"/>.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[TestSpec("5.3.3", Summary = "EventDataSetWriter event-message build")]
|
|
[TestSpec("6.2.4")]
|
|
public class EventDataSetWriterTests
|
|
{
|
|
[Test]
|
|
[TestSpec("5.3.3")]
|
|
public async Task BuildEventMessagesAsync_EmitsOneMessagePerEventAsync()
|
|
{
|
|
var clock = new FakeTimeProvider();
|
|
var sampler = new StubSampler();
|
|
sampler.Enqueue([new Variant("A1"), new Variant(1.0)]);
|
|
sampler.Enqueue([new Variant("A2"), new Variant(2.0)]);
|
|
EventDataSetWriter writer = BuildWriter(sampler, clock);
|
|
|
|
ArrayOf<PubSubDataSetMessage> messages =
|
|
await writer.BuildEventMessagesAsync().ConfigureAwait(false);
|
|
|
|
Assert.That(messages, Has.Count.EqualTo(2));
|
|
Assert.That(messages[0].MessageType,
|
|
Is.EqualTo(PubSubDataSetMessageType.Event));
|
|
Assert.That(((DataSetField[]?)messages[0].Fields) ?? [], Has.Length.EqualTo(2));
|
|
Assert.That(messages[0].Fields[0].Value, Is.EqualTo(new Variant("A1")));
|
|
Assert.That(messages[1].Fields[1].Value, Is.EqualTo(new Variant(2.0)));
|
|
Assert.That(messages[0].SequenceNumber, Is.LessThan(messages[1].SequenceNumber));
|
|
}
|
|
|
|
[Test]
|
|
[TestSpec("5.3.3")]
|
|
public async Task BuildEventMessagesAsync_NoEvents_ReturnsEmptyAsync()
|
|
{
|
|
var sampler = new StubSampler();
|
|
EventDataSetWriter writer = BuildWriter(sampler, new FakeTimeProvider());
|
|
ArrayOf<PubSubDataSetMessage> messages =
|
|
await writer.BuildEventMessagesAsync().ConfigureAwait(false);
|
|
Assert.That(messages, Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
[TestSpec("6.2.4")]
|
|
public async Task BuildEventMessagesAsync_HonoursFieldContentMaskAsync()
|
|
{
|
|
var sampler = new StubSampler();
|
|
sampler.Enqueue([new Variant(1.0), new Variant(2.0)]);
|
|
EventDataSetWriter writer = BuildWriter(
|
|
sampler,
|
|
new FakeTimeProvider(),
|
|
contentMask: (uint)DataSetFieldContentMask.StatusCode);
|
|
|
|
ArrayOf<PubSubDataSetMessage> messages =
|
|
await writer.BuildEventMessagesAsync().ConfigureAwait(false);
|
|
|
|
Assert.That(messages, Has.Count.EqualTo(1));
|
|
var dsm = (UadpDataSetMessageV2)messages[0];
|
|
Assert.That(dsm.FieldContentMask & DataSetFieldContentMask.StatusCode,
|
|
Is.EqualTo(DataSetFieldContentMask.StatusCode));
|
|
}
|
|
|
|
[Test]
|
|
[TestSpec("6.2.4")]
|
|
public async Task EventPublishedDataSet_AlignsFieldsToMetaDataAsync()
|
|
{
|
|
var sampler = new StubSampler();
|
|
sampler.Enqueue([new Variant("event"), new Variant(99)]);
|
|
EventPublishedDataSet pds = BuildPublishedDataSet(sampler);
|
|
ArrayOf<ArrayOf<DataSetField>> rows =
|
|
await pds.SampleEventsAsync().ConfigureAwait(false);
|
|
|
|
Assert.That(((ArrayOf<DataSetField>[]?)rows) ?? [], Has.Length.EqualTo(1));
|
|
Assert.That(rows[0][0].Name, Is.EqualTo("Message"));
|
|
Assert.That(rows[0][1].Name, Is.EqualTo("Severity"));
|
|
Assert.That(rows[0][0].Value, Is.EqualTo(new Variant("event")));
|
|
Assert.That(rows[0][1].Value, Is.EqualTo(new Variant(99)));
|
|
}
|
|
|
|
[Test]
|
|
[TestSpec("6.2.4")]
|
|
public async Task EventPublishedDataSet_SamplesOneOccurrencePerSnapshotAsync()
|
|
{
|
|
//
|
|
// An event dataset has no current state to sample, so the general
|
|
// IPublishedDataSet contract drains one occurrence per call and
|
|
// declares it as an event. Without the declaration a writer group
|
|
// would treat the occurrence as a state snapshot and derive delta
|
|
// frames by comparing it against an unrelated earlier occurrence.
|
|
//
|
|
var sampler = new StubSampler();
|
|
sampler.Enqueue([new Variant("first"), new Variant(1)]);
|
|
sampler.Enqueue([new Variant("second"), new Variant(2)]);
|
|
EventPublishedDataSet pds = BuildPublishedDataSet(sampler);
|
|
|
|
PublishedDataSetSnapshot first = await pds.SampleAsync().ConfigureAwait(false);
|
|
PublishedDataSetSnapshot second = await pds.SampleAsync().ConfigureAwait(false);
|
|
PublishedDataSetSnapshot exhausted = await pds.SampleAsync().ConfigureAwait(false);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(first.MessageType, Is.EqualTo(PubSubDataSetMessageType.Event));
|
|
Assert.That(first.Fields[0].Value, Is.EqualTo(new Variant("first")));
|
|
Assert.That(second.MessageType, Is.EqualTo(PubSubDataSetMessageType.Event));
|
|
Assert.That(second.Fields[0].Value, Is.EqualTo(new Variant("second")));
|
|
Assert.That(exhausted.MessageType, Is.Null);
|
|
Assert.That(exhausted.Fields.Count, Is.Zero);
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task EventPublishedDataSetExposesClassIdAndFixedMetadataAsync()
|
|
{
|
|
var sampler = new StubSampler();
|
|
sampler.Enqueue([new Variant("event"), new Variant(1)]);
|
|
EventPublishedDataSet dataSet = BuildPublishedDataSet(sampler);
|
|
var classId = new Uuid(Guid.NewGuid());
|
|
dataSet.MetaData.DataSetClassId = classId;
|
|
bool metadataChanged = false;
|
|
EventHandler<DataSetMetaDataChangedEventArgs> handler =
|
|
(_, _) => metadataChanged = true;
|
|
|
|
dataSet.MetaDataChanged += handler;
|
|
PublishedDataSetSnapshot snapshot =
|
|
await dataSet.SampleAsync().ConfigureAwait(false);
|
|
dataSet.MetaDataChanged -= handler;
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(
|
|
((IPublishedDataSet)dataSet).DataSetClassId,
|
|
Is.EqualTo(classId));
|
|
Assert.That(snapshot.MessageType, Is.EqualTo(PubSubDataSetMessageType.Event));
|
|
Assert.That(metadataChanged, Is.False);
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task EventDataSetWriterExposesContractAndBuildsJsonMessageAsync()
|
|
{
|
|
var sampler = new StubSampler();
|
|
sampler.Enqueue([new Variant("event"), new Variant(1)]);
|
|
EventPublishedDataSet dataSet = BuildPublishedDataSet(sampler);
|
|
var configuration = new DataSetWriterDataType
|
|
{
|
|
Name = "json-event-writer",
|
|
DataSetWriterId = 8
|
|
};
|
|
var writer = new EventDataSetWriter(
|
|
configuration,
|
|
dataSet,
|
|
new FakeTimeProvider(),
|
|
Profiles.PubSubMqttJsonTransport);
|
|
|
|
ArrayOf<PubSubDataSetMessage> messages =
|
|
await writer.BuildEventMessagesAsync().ConfigureAwait(false);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(
|
|
((IDataSetWriter)writer).PublishedDataSet,
|
|
Is.SameAs(dataSet));
|
|
Assert.That(writer.KeyFrameCount, Is.Zero);
|
|
Assert.That(writer.State.ComponentName, Is.EqualTo(configuration.Name));
|
|
Assert.That(messages, Has.Count.EqualTo(1));
|
|
Assert.That(
|
|
messages[0],
|
|
Is.TypeOf<Opc.Ua.PubSub.Encoding.Json.JsonDataSetMessage>());
|
|
});
|
|
}
|
|
|
|
private static EventDataSetWriter BuildWriter(
|
|
IEventSampler sampler,
|
|
TimeProvider clock,
|
|
uint contentMask = 0)
|
|
{
|
|
EventPublishedDataSet pds = BuildPublishedDataSet(sampler);
|
|
var writerCfg = new DataSetWriterDataType
|
|
{
|
|
Name = "evt-writer",
|
|
DataSetWriterId = 7,
|
|
DataSetFieldContentMask = contentMask
|
|
};
|
|
return new EventDataSetWriter(writerCfg, pds, clock);
|
|
}
|
|
|
|
private static EventPublishedDataSet BuildPublishedDataSet(IEventSampler sampler)
|
|
{
|
|
var pubEvents = new PublishedEventsDataType
|
|
{
|
|
EventNotifier = new NodeId("notifier", 1),
|
|
SelectedFields =
|
|
[
|
|
new SimpleAttributeOperand { TypeDefinitionId = new NodeId("Base", 1) },
|
|
new SimpleAttributeOperand { TypeDefinitionId = new NodeId("Base", 1) }
|
|
]
|
|
};
|
|
var pdsCfg = new PublishedDataSetDataType
|
|
{
|
|
Name = "events-pds",
|
|
DataSetMetaData = new DataSetMetaDataType
|
|
{
|
|
Fields =
|
|
[
|
|
new FieldMetaData { Name = "Message" },
|
|
new FieldMetaData { Name = "Severity" }
|
|
]
|
|
},
|
|
DataSetSource = new ExtensionObject(pubEvents)
|
|
};
|
|
return new EventPublishedDataSet(pdsCfg, sampler);
|
|
}
|
|
|
|
private sealed class StubSampler : IEventSampler
|
|
{
|
|
private readonly List<IReadOnlyList<Variant>> m_pending = [];
|
|
public string Name => "stub";
|
|
|
|
public void Enqueue(IReadOnlyList<Variant> row)
|
|
{
|
|
m_pending.Add(row);
|
|
}
|
|
|
|
public ValueTask<IReadOnlyList<IReadOnlyList<Variant>>> SampleEventsAsync(
|
|
ArrayOf<SimpleAttributeOperand> selectedFields,
|
|
ContentFilter? filter,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
IReadOnlyList<IReadOnlyList<Variant>> copy = [.. m_pending];
|
|
m_pending.Clear();
|
|
return new ValueTask<IReadOnlyList<IReadOnlyList<Variant>>>(copy);
|
|
}
|
|
}
|
|
}
|
|
}
|