Auto: focas-f2d — PMC range coalescing

Closes #266
This commit is contained in:
Joseph Doherty
2026-04-25 20:02:10 -04:00
parent 9ebe5bd523
commit 4d3ee47235
7 changed files with 836 additions and 0 deletions

View File

@@ -80,6 +80,33 @@ internal class FakeFocasClient : IFocasClient
return Task.CompletedTask;
}
/// <summary>
/// Per-letter / per-path byte storage the coalesced range path reads from. Tests
/// populate <c>PmcByteRanges[("R", 1)] = new byte[size]</c> + the corresponding values to
/// drive both the per-tag <see cref="ReadAsync"/> + the coalesced
/// <see cref="ReadPmcRangeAsync"/> path against the same source of truth (issue #266).
/// </summary>
public Dictionary<(string Letter, int PathId), byte[]> PmcByteRanges { get; } = new();
/// <summary>
/// Ordered log of <c>pmc_rdpmcrng</c>-shaped range calls observed on this fake
/// session — one entry per coalesced wire call. Tests assert this count to verify
/// coalescing actually collapsed N per-byte reads into one range read (issue #266).
/// </summary>
public List<(string Letter, int PathId, int StartByte, int ByteCount)> RangeReadLog { get; } = new();
public virtual Task<(byte[]? buffer, uint status)> ReadPmcRangeAsync(
string letter, int pathId, int startByte, int byteCount, CancellationToken ct)
{
RangeReadLog.Add((letter, pathId, startByte, byteCount));
if (!PmcByteRanges.TryGetValue((letter.ToUpperInvariant(), pathId), out var src))
return Task.FromResult<(byte[]?, uint)>((new byte[byteCount], FocasStatusMapper.Good));
var buf = new byte[byteCount];
var copy = Math.Min(byteCount, Math.Max(0, src.Length - startByte));
if (copy > 0) Array.Copy(src, startByte, buf, 0, copy);
return Task.FromResult<(byte[]?, uint)>((buf, FocasStatusMapper.Good));
}
public virtual void Dispose()
{
DisposeCount++;

View File

@@ -0,0 +1,232 @@
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS;
namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Tests;
[Trait("Category", "Unit")]
public sealed class FocasPmcCoalescedReadTests
{
private const string Host = "focas://10.0.0.5:8193";
private static (FocasDriver drv, FakeFocasClientFactory factory, FakeFocasClient client) NewDriver(
params FocasTagDefinition[] tags)
{
var client = new FakeFocasClient();
var factory = new FakeFocasClientFactory { Customise = () => client };
var drv = new FocasDriver(new FocasDriverOptions
{
Devices = [new FocasDeviceOptions(Host)],
Tags = tags,
Probe = new FocasProbeOptions { Enabled = false },
}, "drv-coalesce", factory);
return (drv, factory, client);
}
private static FocasTagDefinition Tag(string name, string addr, FocasDataType type) =>
new(name, Host, addr, type);
[Fact]
public async Task Hundred_contiguous_PMC_bytes_collapse_to_one_wire_call()
{
// 100 contiguous R-letter byte-shaped tags → coalescer cap is 256 → one range read.
var tags = new FocasTagDefinition[100];
var refs = new string[100];
for (var i = 0; i < 100; i++)
{
tags[i] = Tag($"r{i}", $"R{i}", FocasDataType.Byte);
refs[i] = $"r{i}";
}
var (drv, _, client) = NewDriver(tags);
client.PmcByteRanges[("R", 1)] = new byte[200];
for (var i = 0; i < 100; i++) client.PmcByteRanges[("R", 1)][i] = (byte)(i + 1);
await drv.InitializeAsync("{}", CancellationToken.None);
var snapshots = await drv.ReadAsync(refs, CancellationToken.None);
snapshots.Count.ShouldBe(100);
foreach (var s in snapshots) s.StatusCode.ShouldBe(FocasStatusMapper.Good);
// sbyte cast: 1 → 1, 100 stays positive
snapshots[0].Value.ShouldBe((sbyte)1);
snapshots[99].Value.ShouldBe((sbyte)100);
client.RangeReadLog.Count.ShouldBe(1);
client.RangeReadLog[0].Letter.ShouldBe("R");
client.RangeReadLog[0].StartByte.ShouldBe(0);
client.RangeReadLog[0].ByteCount.ShouldBe(100);
}
[Fact]
public async Task Gap_larger_than_bridge_threshold_splits_into_two_wire_calls()
{
// R0, R1, then R100, R101 — gap of 98 > bridge cap of 16 → 2 ranges.
var tags = new[]
{
Tag("r0", "R0", FocasDataType.Byte),
Tag("r1", "R1", FocasDataType.Byte),
Tag("r100", "R100", FocasDataType.Byte),
Tag("r101", "R101", FocasDataType.Byte),
};
var (drv, _, client) = NewDriver(tags);
client.PmcByteRanges[("R", 1)] = new byte[200];
client.PmcByteRanges[("R", 1)][0] = 10;
client.PmcByteRanges[("R", 1)][1] = 11;
client.PmcByteRanges[("R", 1)][100] = 20;
client.PmcByteRanges[("R", 1)][101] = 21;
await drv.InitializeAsync("{}", CancellationToken.None);
var snapshots = await drv.ReadAsync(["r0", "r1", "r100", "r101"], CancellationToken.None);
snapshots.Count.ShouldBe(4);
foreach (var s in snapshots) s.StatusCode.ShouldBe(FocasStatusMapper.Good);
snapshots[0].Value.ShouldBe((sbyte)10);
snapshots[3].Value.ShouldBe((sbyte)21);
client.RangeReadLog.Count.ShouldBe(2);
}
[Fact]
public async Task Different_letters_yield_separate_wire_calls()
{
var tags = new[]
{
Tag("r0", "R0", FocasDataType.Byte),
Tag("r1", "R1", FocasDataType.Byte),
Tag("d0", "D0", FocasDataType.Byte),
Tag("d1", "D1", FocasDataType.Byte),
};
var (drv, _, client) = NewDriver(tags);
client.PmcByteRanges[("R", 1)] = new byte[8] { 1, 2, 3, 4, 5, 6, 7, 8 };
client.PmcByteRanges[("D", 1)] = new byte[8] { 9, 10, 11, 12, 13, 14, 15, 16 };
await drv.InitializeAsync("{}", CancellationToken.None);
var snapshots = await drv.ReadAsync(["r0", "r1", "d0", "d1"], CancellationToken.None);
foreach (var s in snapshots) s.StatusCode.ShouldBe(FocasStatusMapper.Good);
snapshots[0].Value.ShouldBe((sbyte)1);
snapshots[2].Value.ShouldBe((sbyte)9);
client.RangeReadLog.Count.ShouldBe(2);
client.RangeReadLog.ShouldContain(c => c.Letter == "R");
client.RangeReadLog.ShouldContain(c => c.Letter == "D");
}
[Fact]
public async Task Different_paths_yield_separate_wire_calls()
{
var tags = new[]
{
Tag("p1a", "R0", FocasDataType.Byte),
Tag("p1b", "R1", FocasDataType.Byte),
Tag("p2a", "R0@2", FocasDataType.Byte),
Tag("p2b", "R1@2", FocasDataType.Byte),
};
var (drv, _, client) = NewDriver(tags);
client.PathCount = 2;
client.PmcByteRanges[("R", 1)] = new byte[] { 1, 2 };
client.PmcByteRanges[("R", 2)] = new byte[] { 7, 8 };
await drv.InitializeAsync("{}", CancellationToken.None);
var snapshots = await drv.ReadAsync(["p1a", "p1b", "p2a", "p2b"], CancellationToken.None);
foreach (var s in snapshots) s.StatusCode.ShouldBe(FocasStatusMapper.Good);
snapshots[0].Value.ShouldBe((sbyte)1);
snapshots[2].Value.ShouldBe((sbyte)7);
client.RangeReadLog.Count.ShouldBe(2);
client.RangeReadLog.ShouldContain(c => c.PathId == 1);
client.RangeReadLog.ShouldContain(c => c.PathId == 2);
}
[Fact]
public async Task Single_PMC_tag_does_not_invoke_range_path()
{
// Single-tag PMC reads aren't worth coalescing — the driver falls through to the
// existing per-tag dispatch so connect/set-path overhead isn't paid twice.
var (drv, _, client) = NewDriver(Tag("only", "R5", FocasDataType.Byte));
client.Values["R5"] = (sbyte)42;
await drv.InitializeAsync("{}", CancellationToken.None);
var snapshots = await drv.ReadAsync(["only"], CancellationToken.None);
snapshots.Single().Value.ShouldBe((sbyte)42);
client.RangeReadLog.ShouldBeEmpty();
}
[Fact]
public async Task Bit_addressed_tags_share_parent_byte_range()
{
// R10.0 + R10.3 + R10.7 + R11.0 — all addressing R10 / R11, one coalesced range.
var tags = new[]
{
Tag("b0", "R10.0", FocasDataType.Bit),
Tag("b3", "R10.3", FocasDataType.Bit),
Tag("b7", "R10.7", FocasDataType.Bit),
Tag("c0", "R11.0", FocasDataType.Bit),
};
var (drv, _, client) = NewDriver(tags);
client.PmcByteRanges[("R", 1)] = new byte[20];
// R10 = 0b1000_1001 → bit0=1, bit3=1, bit7=1
client.PmcByteRanges[("R", 1)][10] = 0b1000_1001;
// R11 = 0b0000_0000 → bit0=0
client.PmcByteRanges[("R", 1)][11] = 0;
await drv.InitializeAsync("{}", CancellationToken.None);
var snapshots = await drv.ReadAsync(["b0", "b3", "b7", "c0"], CancellationToken.None);
snapshots[0].Value.ShouldBe(true);
snapshots[1].Value.ShouldBe(true);
snapshots[2].Value.ShouldBe(true);
snapshots[3].Value.ShouldBe(false);
client.RangeReadLog.Count.ShouldBe(1);
client.RangeReadLog[0].StartByte.ShouldBe(10);
client.RangeReadLog[0].ByteCount.ShouldBe(2);
}
[Fact]
public async Task Wider_types_decode_correctly_from_coalesced_buffer()
{
// R0 Int16 + R2 Int32 + R6 Byte — contiguous (R0..R6 = 7 bytes), one range.
var tags = new[]
{
Tag("w16", "R0", FocasDataType.Int16),
Tag("w32", "R2", FocasDataType.Int32),
Tag("b", "R6", FocasDataType.Byte),
};
var (drv, _, client) = NewDriver(tags);
// Little-endian: R0..R1 = 0x1234 → bytes 0x34, 0x12; R2..R5 = 0x12345678 → 0x78,0x56,0x34,0x12; R6=0x05
client.PmcByteRanges[("R", 1)] = new byte[] { 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, 0x05 };
await drv.InitializeAsync("{}", CancellationToken.None);
var snapshots = await drv.ReadAsync(["w16", "w32", "b"], CancellationToken.None);
snapshots[0].Value.ShouldBe((short)0x1234);
snapshots[1].Value.ShouldBe(0x12345678);
snapshots[2].Value.ShouldBe((sbyte)0x05);
client.RangeReadLog.Count.ShouldBe(1);
client.RangeReadLog[0].ByteCount.ShouldBe(7);
}
[Fact]
public async Task Non_PMC_tags_in_same_batch_use_per_tag_path()
{
// Mix PMC + Parameter + Macro — only the PMC half coalesces.
var tags = new[]
{
Tag("r0", "R0", FocasDataType.Byte),
Tag("r1", "R1", FocasDataType.Byte),
Tag("p", "PARAM:1820", FocasDataType.Int32),
Tag("m", "MACRO:500", FocasDataType.Float64),
};
var (drv, _, client) = NewDriver(tags);
client.PmcByteRanges[("R", 1)] = new byte[] { 11, 22 };
client.Values["PARAM:1820"] = 7777;
client.Values["MACRO:500"] = 2.71828;
await drv.InitializeAsync("{}", CancellationToken.None);
var snapshots = await drv.ReadAsync(["r0", "r1", "p", "m"], CancellationToken.None);
snapshots[0].Value.ShouldBe((sbyte)11);
snapshots[1].Value.ShouldBe((sbyte)22);
snapshots[2].Value.ShouldBe(7777);
snapshots[3].Value.ShouldBe(2.71828);
client.RangeReadLog.Count.ShouldBe(1);
}
}

View File

@@ -0,0 +1,176 @@
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Wire;
namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Tests;
[Trait("Category", "Unit")]
public sealed class FocasPmcCoalescerTests
{
[Fact]
public void Empty_input_yields_no_groups()
{
var groups = FocasPmcCoalescer.Plan(Array.Empty<PmcAddressRequest>());
groups.ShouldBeEmpty();
}
[Fact]
public void Contiguous_same_letter_same_path_coalesces_into_one_group()
{
// 100 contiguous R-letter byte reads at byte 0..99
var requests = new List<PmcAddressRequest>();
for (var i = 0; i < 100; i++)
requests.Add(new PmcAddressRequest("R", PathId: 1, ByteNumber: i, ByteWidth: 1, OriginalIndex: i));
var groups = FocasPmcCoalescer.Plan(requests);
groups.Count.ShouldBe(1);
var g = groups[0];
g.Letter.ShouldBe("R");
g.PathId.ShouldBe(1);
g.StartByte.ShouldBe(0);
g.ByteCount.ShouldBe(100);
g.Members.Count.ShouldBe(100);
g.Members[42].Offset.ShouldBe(42);
g.Members[42].OriginalIndex.ShouldBe(42);
}
[Fact]
public void Range_cap_splits_oversized_runs_into_multiple_groups()
{
// 300 contiguous bytes — must split (cap = 256)
var requests = new List<PmcAddressRequest>();
for (var i = 0; i < 300; i++)
requests.Add(new PmcAddressRequest("R", 1, i, 1, i));
var groups = FocasPmcCoalescer.Plan(requests);
groups.Count.ShouldBe(2);
groups[0].ByteCount.ShouldBe(FocasPmcCoalescer.MaxRangeBytes);
groups[0].StartByte.ShouldBe(0);
groups[1].StartByte.ShouldBe(FocasPmcCoalescer.MaxRangeBytes);
groups[1].ByteCount.ShouldBe(300 - FocasPmcCoalescer.MaxRangeBytes);
}
[Fact]
public void Gap_within_bridge_threshold_is_bridged()
{
// Two runs: R0..R9 then R20..R29 — gap = 10 bytes, within bridge cap of 16.
var requests = new List<PmcAddressRequest>
{
new("R", 1, 0, 1, 0),
new("R", 1, 9, 1, 1),
new("R", 1, 20, 1, 2),
new("R", 1, 29, 1, 3),
};
var groups = FocasPmcCoalescer.Plan(requests);
groups.Count.ShouldBe(1);
groups[0].StartByte.ShouldBe(0);
groups[0].ByteCount.ShouldBe(30);
}
[Fact]
public void Gap_larger_than_bridge_threshold_splits()
{
// Two runs: R0 then R100 — gap of 99 bytes >> 16, must split.
var requests = new List<PmcAddressRequest>
{
new("R", 1, 0, 1, 0),
new("R", 1, 100, 1, 1),
};
var groups = FocasPmcCoalescer.Plan(requests);
groups.Count.ShouldBe(2);
groups[0].StartByte.ShouldBe(0);
groups[1].StartByte.ShouldBe(100);
}
[Fact]
public void Different_letters_split_into_separate_groups()
{
var requests = new List<PmcAddressRequest>
{
new("R", 1, 0, 1, 0),
new("R", 1, 1, 1, 1),
new("D", 1, 0, 1, 2),
new("D", 1, 1, 1, 3),
};
var groups = FocasPmcCoalescer.Plan(requests);
groups.Count.ShouldBe(2);
groups.ShouldContain(g => g.Letter == "R" && g.ByteCount == 2);
groups.ShouldContain(g => g.Letter == "D" && g.ByteCount == 2);
}
[Fact]
public void Different_paths_split_into_separate_groups()
{
var requests = new List<PmcAddressRequest>
{
new("R", 1, 0, 1, 0),
new("R", 1, 1, 1, 1),
new("R", 2, 0, 1, 2),
new("R", 2, 1, 1, 3),
};
var groups = FocasPmcCoalescer.Plan(requests);
groups.Count.ShouldBe(2);
groups.ShouldContain(g => g.Letter == "R" && g.PathId == 1);
groups.ShouldContain(g => g.Letter == "R" && g.PathId == 2);
}
[Fact]
public void Wider_data_types_extend_range_correctly()
{
// R0 is Int32 (4 bytes covers R0..R3), R4 is Byte → contiguous, one group of 5 bytes.
var requests = new List<PmcAddressRequest>
{
new("R", 1, 0, ByteWidth: 4, 0),
new("R", 1, 4, ByteWidth: 1, 1),
};
var groups = FocasPmcCoalescer.Plan(requests);
groups.Count.ShouldBe(1);
groups[0].ByteCount.ShouldBe(5);
groups[0].Members[0].ByteWidth.ShouldBe(4);
groups[0].Members[0].Offset.ShouldBe(0);
groups[0].Members[1].ByteWidth.ShouldBe(1);
groups[0].Members[1].Offset.ShouldBe(4);
}
[Fact]
public void Overlapping_requests_do_not_grow_range_beyond_their_union()
{
// R10 Int32 (R10..R13) + R12 Byte — overlap; range should still be 4 bytes from 10.
var requests = new List<PmcAddressRequest>
{
new("R", 1, 10, 4, 0),
new("R", 1, 12, 1, 1),
};
var groups = FocasPmcCoalescer.Plan(requests);
groups.Count.ShouldBe(1);
groups[0].StartByte.ShouldBe(10);
groups[0].ByteCount.ShouldBe(4);
groups[0].Members[1].Offset.ShouldBe(2); // member at byte 12, offset within range = 2
}
[Fact]
public void ByteWidth_helper_matches_data_type_sizes()
{
FocasPmcCoalescer.ByteWidth(FocasDataType.Bit).ShouldBe(1);
FocasPmcCoalescer.ByteWidth(FocasDataType.Byte).ShouldBe(1);
FocasPmcCoalescer.ByteWidth(FocasDataType.Int16).ShouldBe(2);
FocasPmcCoalescer.ByteWidth(FocasDataType.Int32).ShouldBe(4);
FocasPmcCoalescer.ByteWidth(FocasDataType.Float32).ShouldBe(4);
FocasPmcCoalescer.ByteWidth(FocasDataType.Float64).ShouldBe(8);
}
}