/* ======================================================================== * 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/ * ======================================================================*/ // CA2007: tests run without a SynchronizationContext; ConfigureAwait(false) // adds noise without a behavioural benefit. Disabled file-level for the suite. #pragma warning disable CA2007 using System.Threading.Tasks; using Moq; using NUnit.Framework; using Opc.Ua.Redundancy.Server; namespace Opc.Ua.Server.Tests.Redundancy { /// /// Unit tests for guard paths and the happy-path update of the live /// Server.ServiceLevel node from an . /// [TestFixture] [Category("Distributed")] [Parallelizable(ParallelScope.All)] public class ServiceLevelStartupTaskTests { [Test] public void ConstructorThrowsOnNullProvider() { Assert.That(() => new ServiceLevelStartupTask(null!), Throws.ArgumentNullException); } [Test] public void OnServerStartedThrowsOnNullServer() { var task = new ServiceLevelStartupTask(new ConstantServiceLevelProvider()); Assert.That(async () => await task.OnServerStartedAsync(null!).ConfigureAwait(false), Throws.ArgumentNullException); } [Test] public async Task OnServerStartedNoOpsWhenServerObjectMissingAsync() { var task = new ServiceLevelStartupTask(new ConstantServiceLevelProvider()); var server = new Mock(); server.Setup(s => s.ServerObject).Returns((ServerObjectState)null!); await task.OnServerStartedAsync(server.Object).ConfigureAwait(false); server.VerifyGet(s => s.ServerObject, Times.Once); } [Test] public async Task OnServerStartedAppliesInitialAndChangedServiceLevelAsync() { using DiagnosticsServerHarness harness = await DiagnosticsServerHarness.CreateAsync().ConfigureAwait(false); var provider = new MutableServiceLevelProvider(150); var task = new ServiceLevelStartupTask(provider); await task.OnServerStartedAsync(harness.Server.Object).ConfigureAwait(false); Assert.That(harness.ServerObject.ServiceLevel, Is.Not.Null); Assert.That(harness.ServerObject.ServiceLevel!.Value, Is.EqualTo((byte)150), "the initial level is applied"); provider.Raise(42); Assert.That( harness.ServerObject.ServiceLevel!.Value, Is.EqualTo((byte)42), "a provider change is mirrored onto the live node"); } private sealed class MutableServiceLevelProvider : IServiceLevelProvider { public MutableServiceLevelProvider(byte serviceLevel) { m_serviceLevel = serviceLevel; } public event System.Action? ServiceLevelChanged; public byte GetServiceLevel() { return m_serviceLevel; } public void Raise(byte serviceLevel) { m_serviceLevel = serviceLevel; ServiceLevelChanged?.Invoke(serviceLevel); } private byte m_serviceLevel; } } }