55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
|
|
|
public sealed class DeferredServiceLevelPublisherTests
|
|
{
|
|
/// <summary>Verifies that publish before SetInner safely accepts a cached request.</summary>
|
|
[Fact]
|
|
public void Publish_before_SetInner_accepts_a_cached_request()
|
|
{
|
|
var deferred = new DeferredServiceLevelPublisher();
|
|
|
|
Should.NotThrow(() => deferred.Publish(123));
|
|
}
|
|
|
|
/// <summary>Verifies that publish after SetInner routes to the inner publisher.</summary>
|
|
[Fact]
|
|
public void Publish_after_SetInner_routes_to_the_inner()
|
|
{
|
|
var recording = new RecordingPublisher();
|
|
var deferred = new DeferredServiceLevelPublisher();
|
|
deferred.SetInner(recording);
|
|
|
|
deferred.Publish(200);
|
|
|
|
recording.LastValue.ShouldBe((byte)200);
|
|
}
|
|
|
|
/// <summary>Verifies that SetInner with null detaches the previous publisher.</summary>
|
|
[Fact]
|
|
public void SetInner_null_detaches_the_previous_publisher()
|
|
{
|
|
var recording = new RecordingPublisher();
|
|
var deferred = new DeferredServiceLevelPublisher();
|
|
deferred.SetInner(recording);
|
|
deferred.Publish(50);
|
|
|
|
deferred.SetInner(null);
|
|
deferred.Publish(99);
|
|
|
|
recording.LastValue.ShouldBe((byte)50, "writes after SetInner(null) must not reach the previous inner");
|
|
}
|
|
|
|
private sealed class RecordingPublisher : IServiceLevelPublisher
|
|
{
|
|
/// <summary>Gets the last published service level value.</summary>
|
|
public byte? LastValue { get; private set; }
|
|
/// <summary>Publishes a service level value.</summary>
|
|
/// <param name="serviceLevel">The service level to publish.</param>
|
|
public void Publish(byte serviceLevel) => LastValue = serviceLevel;
|
|
}
|
|
}
|