|
| 1 | +// Copyright (c) 2026 TrakHound Inc., All Rights Reserved. |
| 2 | +// TrakHound Inc. licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using MTConnect.Clients; |
| 5 | +using MTConnect.Devices; |
| 6 | +using NUnit.Framework; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading; |
| 11 | + |
| 12 | +namespace MTConnect.Tests.Http.Clients |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Drives the full MTConnectHttpClient (the streaming client, not the |
| 16 | + /// single-shot probe client) against the real embedded MTConnectHttpServer |
| 17 | + /// started by AgentRunner, pinning the two surfaces issue #176 added: |
| 18 | + /// the public Devices snapshot accessor (which exposes the post-probe |
| 19 | + /// device cache the client already maintains internally) and the |
| 20 | + /// DeviceReceived event (which historically built an empty list and |
| 21 | + /// iterated it, so it never fired in the field). Both invariants are |
| 22 | + /// exercised end to end through Start()/Stop(), so the probe round trip |
| 23 | + /// and the ProcessProbeDocument hand-off both run. |
| 24 | + /// </summary> |
| 25 | + [TestFixture] |
| 26 | + public class HttpClientDeviceModel : HttpClientFixture |
| 27 | + { |
| 28 | + // Generous CI-safe bounds. The streaming client raises DeviceReceived |
| 29 | + // and populates the snapshot from the first probe, which AgentRunner |
| 30 | + // guarantees is answerable before the fixture returns from Start(). |
| 31 | + private const int ProbeWaitTimeoutMs = 15000; |
| 32 | + private const int ProbeWaitPollMs = 50; |
| 33 | + |
| 34 | + /// <summary>Pins the behaviour expressed by the test name: devices accessor is empty before probe.</summary> |
| 35 | + [Test] |
| 36 | + public void DevicesAccessorIsEmptyBeforeProbe() |
| 37 | + { |
| 38 | + var client = new MTConnectHttpClient(Hostname, Port); |
| 39 | + |
| 40 | + Assert.That(client.Devices, Is.Not.Null, "Devices accessor returned null"); |
| 41 | + Assert.That(client.Devices.Count, Is.EqualTo(0), "Devices accessor was non-empty before any probe ran"); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary>Pins the behaviour expressed by the test name: devices accessor is populated after probe.</summary> |
| 45 | + [Test] |
| 46 | + public void DevicesAccessorIsPopulatedAfterProbe() |
| 47 | + { |
| 48 | + var client = new MTConnectHttpClient(Hostname, Port); |
| 49 | + |
| 50 | + using var probeSignal = new ManualResetEventSlim(false); |
| 51 | + client.ProbeReceived += (_, _) => probeSignal.Set(); |
| 52 | + |
| 53 | + try |
| 54 | + { |
| 55 | + client.Start(); |
| 56 | + |
| 57 | + Assert.That(probeSignal.Wait(ProbeWaitTimeoutMs), Is.True, "ProbeReceived did not fire within the timeout"); |
| 58 | + |
| 59 | + // The accessor returns a snapshot, so capture once and assert |
| 60 | + // against the captured dictionary. |
| 61 | + var snapshot = client.Devices; |
| 62 | + |
| 63 | + Assert.That(snapshot, Is.Not.Null, "Devices accessor returned null after probe"); |
| 64 | + Assert.That(snapshot.Count, Is.EqualTo(ExpectedDocumentEntryCount), "Devices accessor did not surface every probed device"); |
| 65 | + Assert.That(snapshot.Values.Any(d => d.Name == DeviceName), Is.True, $"Devices accessor did not surface the {DeviceName} device"); |
| 66 | + } |
| 67 | + finally |
| 68 | + { |
| 69 | + client.Stop(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary>Pins the behaviour expressed by the test name: devices accessor returns snapshot not live view.</summary> |
| 74 | + [Test] |
| 75 | + public void DevicesAccessorReturnsSnapshotNotLiveView() |
| 76 | + { |
| 77 | + var client = new MTConnectHttpClient(Hostname, Port); |
| 78 | + |
| 79 | + using var probeSignal = new ManualResetEventSlim(false); |
| 80 | + client.ProbeReceived += (_, _) => probeSignal.Set(); |
| 81 | + |
| 82 | + try |
| 83 | + { |
| 84 | + client.Start(); |
| 85 | + |
| 86 | + Assert.That(probeSignal.Wait(ProbeWaitTimeoutMs), Is.True, "ProbeReceived did not fire within the timeout"); |
| 87 | + |
| 88 | + var snapshot = client.Devices; |
| 89 | + |
| 90 | + // The accessor must hand back an independent snapshot, not a |
| 91 | + // reference to the live cache — mutating the returned |
| 92 | + // dictionary must not be possible, and successive reads must |
| 93 | + // not alias one another. |
| 94 | + Assert.That(snapshot, Is.InstanceOf<IReadOnlyDictionary<string, IDevice>>(), "Devices accessor did not return an IReadOnlyDictionary"); |
| 95 | + Assert.That(ReferenceEquals(snapshot, client.Devices), Is.False, "Devices accessor handed back an aliased instance instead of a snapshot"); |
| 96 | + } |
| 97 | + finally |
| 98 | + { |
| 99 | + client.Stop(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary>Pins the behaviour expressed by the test name: device received fires once per device on first probe.</summary> |
| 104 | + [Test] |
| 105 | + public void DeviceReceivedFiresOncePerDeviceOnFirstProbe() |
| 106 | + { |
| 107 | + var client = new MTConnectHttpClient(Hostname, Port); |
| 108 | + |
| 109 | + // Capture both the count and the carried IDevice instances so we |
| 110 | + // can assert the wired model is delivered, not a placeholder. |
| 111 | + var receivedDevices = new List<IDevice>(); |
| 112 | + var receivedLock = new object(); |
| 113 | + client.DeviceReceived += (_, device) => |
| 114 | + { |
| 115 | + lock (receivedLock) { receivedDevices.Add(device); } |
| 116 | + }; |
| 117 | + |
| 118 | + using var probeSignal = new ManualResetEventSlim(false); |
| 119 | + client.ProbeReceived += (_, _) => probeSignal.Set(); |
| 120 | + |
| 121 | + try |
| 122 | + { |
| 123 | + client.Start(); |
| 124 | + |
| 125 | + Assert.That(probeSignal.Wait(ProbeWaitTimeoutMs), Is.True, "ProbeReceived did not fire within the timeout"); |
| 126 | + |
| 127 | + // The event is raised from inside ProcessProbeDocument, which |
| 128 | + // runs on the worker thread; ProbeReceived fires at the end of |
| 129 | + // that same method, so once it has been observed, every |
| 130 | + // DeviceReceived invocation for this probe has already |
| 131 | + // happened. |
| 132 | + List<IDevice> snapshot; |
| 133 | + lock (receivedLock) { snapshot = new List<IDevice>(receivedDevices); } |
| 134 | + |
| 135 | + Assert.That(snapshot.Count, Is.EqualTo(ExpectedDocumentEntryCount), "DeviceReceived did not fire once per probed device"); |
| 136 | + Assert.That(snapshot.All(d => d != null), Is.True, "DeviceReceived carried a null device"); |
| 137 | + Assert.That(snapshot.Any(d => d.Name == DeviceName), Is.True, $"DeviceReceived did not deliver the {DeviceName} device"); |
| 138 | + } |
| 139 | + finally |
| 140 | + { |
| 141 | + client.Stop(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary>Pins the behaviour expressed by the test name: device received carries wired data item back pointers.</summary> |
| 146 | + [Test] |
| 147 | + public void DeviceReceivedCarriesWiredDataItemBackPointers() |
| 148 | + { |
| 149 | + var client = new MTConnectHttpClient(Hostname, Port); |
| 150 | + |
| 151 | + IDevice? targetDevice = null; |
| 152 | + var targetLock = new object(); |
| 153 | + client.DeviceReceived += (_, device) => |
| 154 | + { |
| 155 | + if (device?.Name == DeviceName) |
| 156 | + { |
| 157 | + lock (targetLock) { targetDevice = device; } |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + using var probeSignal = new ManualResetEventSlim(false); |
| 162 | + client.ProbeReceived += (_, _) => probeSignal.Set(); |
| 163 | + |
| 164 | + try |
| 165 | + { |
| 166 | + client.Start(); |
| 167 | + |
| 168 | + Assert.That(probeSignal.Wait(ProbeWaitTimeoutMs), Is.True, "ProbeReceived did not fire within the timeout"); |
| 169 | + |
| 170 | + IDevice? captured; |
| 171 | + lock (targetLock) { captured = targetDevice; } |
| 172 | + |
| 173 | + Assert.That(captured, Is.Not.Null, $"DeviceReceived did not deliver the {DeviceName} device"); |
| 174 | + |
| 175 | + // Walk the DataItem tree and assert the back-pointers the |
| 176 | + // issue calls out are wired through to the caller — the whole |
| 177 | + // point of surfacing the cached IDevice is access to this |
| 178 | + // ancestry without re-parsing. |
| 179 | + var dataItems = captured!.GetDataItems().ToList(); |
| 180 | + Assert.That(dataItems, Is.Not.Empty, $"Device {DeviceName} carried no DataItems"); |
| 181 | + |
| 182 | + foreach (var dataItem in dataItems) |
| 183 | + { |
| 184 | + Assert.That(dataItem.Device, Is.Not.Null, $"DataItem {dataItem.Id} lost its Device back-pointer"); |
| 185 | + Assert.That(dataItem.Container, Is.Not.Null, $"DataItem {dataItem.Id} lost its Container back-pointer"); |
| 186 | + } |
| 187 | + } |
| 188 | + finally |
| 189 | + { |
| 190 | + client.Stop(); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments