using System.Net; using AcDream.Core.Net; namespace AcDream.Core.Net.Tests; public class NetClientTests { [Fact] public void SendReceive_LoopbackSelfTest_EchoRoundTrip() { // Two NetClients on loopback that send messages to each other. // Proves Send + Receive actually work end-to-end over real UDP // without depending on any remote server. using var serverSide = new NetClient(new IPEndPoint(IPAddress.Loopback, 0)); using var clientSide = new NetClient(new IPEndPoint(IPAddress.Loopback, serverSide.LocalEndPoint.Port)); byte[] outbound = { 0xDE, 0xAD, 0xBE, 0xEF }; clientSide.Send(outbound); var received = serverSide.Receive(TimeSpan.FromSeconds(2), out var from); Assert.NotNull(received); Assert.Equal(outbound, received); Assert.NotNull(from); Assert.Equal(IPAddress.Loopback, from!.Address); Assert.Equal(clientSide.LocalEndPoint.Port, from.Port); } [Fact] public void Receive_Timeout_ReturnsNullAndNoException() { // Listen for something that will never arrive; the timeout path // must return null cleanly rather than leaking a SocketException. using var client = new NetClient(new IPEndPoint(IPAddress.Loopback, 1)); var result = client.Receive(TimeSpan.FromMilliseconds(100), out var from); Assert.Null(result); Assert.Null(from); } }