Files
lmxopcua/tests/Drivers/Cli/ZB.MOM.WW.OtOpcUa.Driver.S7.Cli.Tests/SubscribeCommandWriteLockTests.cs
T
Joseph Doherty f8bf067243 review(Driver.S7.Cli): endpoint validation + cancellation/flush/write-lock consistency
Re-review at 7286d320. -008 (Medium): S7CommandBase.ValidateEndpoint (port range + timeout>0)
in all commands +tests. -009 clean OperationCanceledException handling; -010 FlushLogging()
in subscribe finally; -011 lock console writes in OnDataChange. -012 (Verdict headline) deferred.
2026-06-19 12:08:45 -04:00

41 lines
1.7 KiB
C#

using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Cli.Tests;
/// <summary>
/// Driver.S7.Cli-011: the <c>subscribe</c> command's <c>OnDataChange</c> handler must
/// serialize its console writes through a lock so that overlapping poll ticks (which
/// arrive on a background thread) cannot interleave partial lines on the output stream.
/// Mirrors the pattern from the Modbus CLI subscribe command.
/// </summary>
[Trait("Category", "Unit")]
public sealed class SubscribeCommandWriteLockTests
{
private static readonly string CommandsDir = LocateCommandsDir();
/// <summary>
/// Verifies that SubscribeCommand uses a lock in the OnDataChange handler to
/// prevent interleaved console writes from overlapping poll ticks.
/// </summary>
[Fact]
public void SubscribeCommand_OnDataChange_uses_write_lock()
{
// Driver.S7.Cli-011: the handler writes via console.Output.WriteLine on a
// background poll thread; concurrent ticks must be serialised with a lock
// to prevent partial-line interleaving.
var source = File.ReadAllText(Path.Combine(CommandsDir, "SubscribeCommand.cs"));
source.ShouldContain("lock (writeLock)");
}
private static string LocateCommandsDir()
{
var dir = new DirectoryInfo(AppContext.BaseDirectory);
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "ZB.MOM.WW.OtOpcUa.slnx")))
dir = dir.Parent;
dir.ShouldNotBeNull("Could not find solution root (ZB.MOM.WW.OtOpcUa.slnx).");
return Path.Combine(
dir!.FullName, "src", "Drivers", "Cli", "ZB.MOM.WW.OtOpcUa.Driver.S7.Cli", "Commands");
}
}