mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-21 17:16:16 +08:00
df44b460be
# Description Generated `CreateInstanceOf<Type>` factories build typed node graphs and assign per-instance NodeIds, but intentionally leave the node create lifecycle open. Hand-written callers commonly registered those graphs without calling `CreateAsPredefinedNode`, so `OnBeforeCreate`, `OnAfterCreate`, and change-mask clearing never ran. This change: - adds `NodeState.IsCreated` and makes lifecycle completion idempotent per node; - completes newly materialized children, including children created by `OnAfterCreate`; - bounds late-child lifecycle convergence and fails explicitly if callbacks keep adding uncreated nodes; - completes uncreated nodes automatically before predefined-node registration and behavior wiring in both async and legacy custom node managers; - assigns/rebases async-node-manager NodeIds before lifecycle callbacks; - emits a null-safe Debug diagnostic when registration performs completion; - documents the generated-factory lifecycle contract and migration behavior; - adds regressions for condition/alarm method binding, finite-state-machine namespaces, Fluent state-node rebasing, behavior replacement, cloning, deletion, late children, non-converging callbacks, and synchronous/async registration. The tracked `src/` and `samples/` call-site audit covered 2,953 C# files. No additional ordering-sensitive call sites require explicit completion beyond the three existing DI/Robotics sites. ## Validation - `dotnet build UA.slnx -c Release -p:CustomTestTarget=net10.0`: passed. - `Opc.Ua.Server.Tests` net10.0: 4,811 passed, 5 skipped. - `NodeStateTests` net10.0 and net48: 213 passed on each. - Node-manager lifecycle and null-logger tests net48: 459 passed. - Source-generation core tests net10.0 and net48: 3,802 passed, 8 skipped on each. - DI, Robotics, ISA95, Positioning, Vision, Vision.Intent, and XRegistry suites passed on net10.0 and net48. - Generated alarm lifecycle regression passed on net10.0 and net48. The solution build reports six pre-existing CA warnings in untouched files. The OpenUSD suite has one pre-existing locale-sensitive failure in `StructuredCartesianCoordinatesAreAccepted` (`1.5` expected versus `1,5` under `en-DE`); all other OpenUSD tests passed on both tested frameworks. ## Related Issues - Fixes #4320 ## Checklist - [ ] I have signed the [CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf) and read the [CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md) doc. - [x] I have added tests that prove my fix is effective or that my feature works and increased code coverage. - [x] I have added all necessary documentation. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [ ] I ran **all** tests locally using the **UA.slnx** solution against at least .net **framework** and .net **10**, and all passed. - [ ] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [x] I have addressed **all** PR feedback received. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7ffdc400-0710-4a2e-a9d1-c0e1a9b3b81e
310 lines
13 KiB
C#
310 lines
13 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.Collections.Generic;
|
|
using System.Linq;
|
|
using Generators;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Generators;
|
|
using GeneratorModel = Opc.Ua.Generators;
|
|
|
|
namespace Opc.Ua.OpenUsd.Tests.Generator
|
|
{
|
|
/// <summary>
|
|
/// Holds the protection alarm nodes to the shape a client can actually read.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// <c>ProtectionFunction</c> is mandatory on <c>GeneratorProtectionAlarmType</c>
|
|
/// and so is materialised by the generated factory. <c>IsShutdown</c> and
|
|
/// <c>SubsystemName</c> are <b>optional</b> and are not. Writing to an optional
|
|
/// member with <c>CreateOrReplace</c> alone produces a child with no
|
|
/// <c>ReferenceTypeId</c> and no NodeId - it appears in <c>GetChildren</c>, so
|
|
/// the code looks right, but there is no reference for a browse to follow and
|
|
/// the property is simply absent from a client's view.
|
|
/// </para>
|
|
/// <para>
|
|
/// That was a real defect here, found by reading the server with a client rather
|
|
/// than by reading the code: every write succeeded. These tests assert the node
|
|
/// shape - specifically that each published member carries the reference that
|
|
/// makes it reachable - instead of asserting the assignment.
|
|
/// </para>
|
|
/// </remarks>
|
|
[TestFixture]
|
|
[Category("Generators")]
|
|
public sealed class GeneratorProtectionAlarmNodeTests
|
|
{
|
|
/// <summary>
|
|
/// The factory leaves the optional members unmaterialised.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// If this ever starts failing, the opt-in below became unnecessary rather
|
|
/// than wrong - but it is the premise the rest of this fixture rests on, so
|
|
/// it is worth stating out loud.
|
|
/// </remarks>
|
|
[Test]
|
|
public void TheFactoryDoesNotMaterialiseOptionalMembers()
|
|
{
|
|
SystemContext context = CreateContext();
|
|
GeneratorModel.GeneratorProtectionAlarmState alarm = CreateAlarm(context);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(alarm.IsShutdown, Is.Null);
|
|
Assert.That(alarm.SubsystemName, Is.Null);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Completing the generated alarm instance binds the standard condition
|
|
/// methods supplied by its base classes.
|
|
/// </summary>
|
|
[Test]
|
|
public void CompletingFactoryCreatedAlarmBindsConditionMethods()
|
|
{
|
|
SystemContext context = CreateContext();
|
|
GeneratorModel.GeneratorProtectionAlarmState alarm = CreateAlarm(context);
|
|
|
|
Assert.That(alarm.IsCreated, Is.False);
|
|
Assert.That(alarm.Enable!.OnCallMethod, Is.Null);
|
|
Assert.That(alarm.Disable!.OnCallMethod, Is.Null);
|
|
Assert.That(alarm.AddComment!.OnCall, Is.Null);
|
|
|
|
alarm.CreateAsPredefinedNode(context);
|
|
|
|
Assert.That(alarm.IsCreated, Is.True);
|
|
Assert.That(alarm.Enable.OnCallMethod, Is.Not.Null);
|
|
Assert.That(alarm.Disable.OnCallMethod, Is.Not.Null);
|
|
Assert.That(alarm.AddComment.OnCall, Is.Not.Null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The production path gives every published member a reference a browse can
|
|
/// follow.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This is the assertion that bites. A child with no
|
|
/// <c>ReferenceTypeId</c> is still in <c>GetChildren</c> and still holds the
|
|
/// value that was written to it, so nothing in the code looks wrong - but no
|
|
/// client can reach it.
|
|
/// </remarks>
|
|
[Test]
|
|
public void EveryPublishedMemberCarriesAReferenceAClientCanFollow()
|
|
{
|
|
SystemContext context = CreateContext();
|
|
GeneratorModel.GeneratorProtectionAlarmState alarm = CreateAlarm(context);
|
|
|
|
GeneratorProtections.ApplyDefinition(
|
|
alarm, context, GeneratorProtections.Definitions[0]);
|
|
|
|
var children = new List<BaseInstanceState>();
|
|
alarm.GetChildren(context, children);
|
|
|
|
var unreachable = new List<string>();
|
|
foreach (BaseInstanceState child in children)
|
|
{
|
|
if (child.ReferenceTypeId.IsNull)
|
|
{
|
|
unreachable.Add(child.BrowseName.Name ?? "<unnamed>");
|
|
}
|
|
}
|
|
|
|
Assert.That(
|
|
unreachable,
|
|
Is.Empty,
|
|
"Children with no reference type cannot be browsed to.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// The two optional members are published as properties of the alarm.
|
|
/// </summary>
|
|
[Test]
|
|
public void TheOptionalMembersArePublishedAsProperties()
|
|
{
|
|
SystemContext context = CreateContext();
|
|
GeneratorModel.GeneratorProtectionAlarmState alarm = CreateAlarm(context);
|
|
|
|
GeneratorProtections.ApplyDefinition(
|
|
alarm, context, GeneratorProtections.Definitions[0]);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(
|
|
alarm.IsShutdown!.ReferenceTypeId,
|
|
Is.EqualTo(Opc.Ua.ReferenceTypeIds.HasProperty));
|
|
Assert.That(
|
|
alarm.SubsystemName!.ReferenceTypeId,
|
|
Is.EqualTo(Opc.Ua.ReferenceTypeIds.HasProperty));
|
|
Assert.That(
|
|
alarm.IsShutdown.TypeDefinitionId,
|
|
Is.EqualTo(Opc.Ua.VariableTypeIds.PropertyType));
|
|
Assert.That(
|
|
alarm.SubsystemName.TypeDefinitionId,
|
|
Is.EqualTo(Opc.Ua.VariableTypeIds.PropertyType));
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// The production path makes the optional members real children.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Browsing an alarm is how a client discovers these, so being a child is
|
|
/// the property that matters - not merely being a non-null C# reference.
|
|
/// This asserts the shape <see cref="GeneratorProtections.ApplyDefinition"/>
|
|
/// produces, so removing the opt-in from it fails here.
|
|
/// </remarks>
|
|
[Test]
|
|
public void ApplyingADefinitionMakesTheOptionalMembersBrowsableChildren()
|
|
{
|
|
SystemContext context = CreateContext();
|
|
GeneratorModel.GeneratorProtectionAlarmState alarm = CreateAlarm(context);
|
|
|
|
GeneratorProtections.ApplyDefinition(
|
|
alarm, context, GeneratorProtections.Definitions[0]);
|
|
|
|
List<string> children = ChildNames(context, alarm);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(alarm.IsShutdown, Is.Not.Null);
|
|
Assert.That(alarm.SubsystemName, Is.Not.Null);
|
|
Assert.That(children, Does.Contain("IsShutdown"));
|
|
Assert.That(children, Does.Contain("SubsystemName"));
|
|
Assert.That(children, Does.Contain("ProtectionFunction"));
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Applying a definition twice does not produce two children.
|
|
/// </summary>
|
|
[Test]
|
|
public void ApplyingADefinitionIsIdempotent()
|
|
{
|
|
SystemContext context = CreateContext();
|
|
GeneratorModel.GeneratorProtectionAlarmState alarm = CreateAlarm(context);
|
|
ProtectionDefinition definition = GeneratorProtections.Definitions[0];
|
|
|
|
GeneratorProtections.ApplyDefinition(alarm, context, definition);
|
|
PropertyState<bool>? first = alarm.IsShutdown;
|
|
GeneratorProtections.ApplyDefinition(alarm, context, definition);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(alarm.IsShutdown, Is.SameAs(first));
|
|
Assert.That(
|
|
ChildNames(context, alarm).Count(n => n == "IsShutdown"),
|
|
Is.EqualTo(1));
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Every protection's declared class and subsystem reach the child nodes.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This is the link between "the table says so" and "a client can read it".
|
|
/// The defect this catches is silent: the C# assignment succeeds either way.
|
|
/// </remarks>
|
|
[Test]
|
|
public void EveryProtectionPublishesItsClassAndSubsystem()
|
|
{
|
|
Assert.That(GeneratorProtections.Definitions.Count, Is.GreaterThan(0));
|
|
|
|
SystemContext context = CreateContext();
|
|
for (int i = 0; i < GeneratorProtections.Definitions.Count; i++)
|
|
{
|
|
ProtectionDefinition definition = GeneratorProtections.Definitions[i];
|
|
GeneratorModel.GeneratorProtectionAlarmState alarm = CreateAlarm(context);
|
|
GeneratorProtections.ApplyDefinition(alarm, context, definition);
|
|
|
|
var children = new List<BaseInstanceState>();
|
|
alarm.GetChildren(context, children);
|
|
|
|
BaseInstanceState? shutdown = children.Find(
|
|
c => c.BrowseName.Name == "IsShutdown");
|
|
BaseInstanceState? subsystem = children.Find(
|
|
c => c.BrowseName.Name == "SubsystemName");
|
|
BaseInstanceState? function = children.Find(
|
|
c => c.BrowseName.Name == "ProtectionFunction");
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(
|
|
(shutdown as PropertyState<bool>)?.Value,
|
|
Is.EqualTo(definition.IsShutdown),
|
|
$"{definition.Name} does not publish its shutdown class.");
|
|
Assert.That(
|
|
(subsystem as PropertyState<string>)?.Value,
|
|
Is.EqualTo(definition.Subsystem),
|
|
$"{definition.Name} does not publish its subsystem.");
|
|
Assert.That(
|
|
(function as PropertyState<GeneratorModel.GeneratorProtectionFunctionEnum>)?.Value,
|
|
Is.EqualTo(definition.Function),
|
|
$"{definition.Name} does not publish its protection function.");
|
|
});
|
|
}
|
|
}
|
|
|
|
private static GeneratorModel.GeneratorProtectionAlarmState CreateAlarm(ISystemContext context)
|
|
{
|
|
// The same factory the node manager uses, so the mandatory members are
|
|
// materialised exactly as they are in the running server. Constructing
|
|
// the state directly would leave them unmaterialised too, and the
|
|
// fixture would be testing a shape the server never produces.
|
|
var owner = new BaseObjectState(null)
|
|
{
|
|
NodeId = new NodeId(1, 1),
|
|
BrowseName = new QualifiedName("GeneratorSet_1", 1),
|
|
};
|
|
return context.CreateInstanceOfGeneratorProtectionAlarmType(
|
|
owner, new QualifiedName("LowOilPressureAlarm", 1));
|
|
}
|
|
|
|
private static List<string> ChildNames(
|
|
ISystemContext context,
|
|
GeneratorModel.GeneratorProtectionAlarmState alarm)
|
|
{
|
|
var children = new List<BaseInstanceState>();
|
|
alarm.GetChildren(context, children);
|
|
return children.ConvertAll(
|
|
c => c.BrowseName.IsNull ? string.Empty : c.BrowseName.Name ?? string.Empty);
|
|
}
|
|
|
|
private static SystemContext CreateContext()
|
|
{
|
|
var namespaceUris = new NamespaceTable();
|
|
namespaceUris.GetIndexOrAppend(GeneratorModel.Namespaces.Generators);
|
|
return new SystemContext(null!)
|
|
{
|
|
NamespaceUris = namespaceUris,
|
|
TypeTable = new TypeTable(namespaceUris),
|
|
};
|
|
}
|
|
}
|
|
}
|