From 14fa47a35c87b68e0782114e852a77f72c095928 Mon Sep 17 00:00:00 2001 From: "Alecio Furanze (Ale)" Date: Thu, 6 Jun 2024 00:17:03 +0200 Subject: [PATCH] Update FixUdp.SendAndReceive.cs --- test/udp/FixUdp.SendAndReceive.cs | 135 +++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 2 deletions(-) diff --git a/test/udp/FixUdp.SendAndReceive.cs b/test/udp/FixUdp.SendAndReceive.cs index 4745949d..6fad028c 100644 --- a/test/udp/FixUdp.SendAndReceive.cs +++ b/test/udp/FixUdp.SendAndReceive.cs @@ -1,8 +1,139 @@ public partial class FixUdp { [Fact] - public void SendAndReceive() + public async void SendAndReceive() { - // TODO: implement + if (IsMac()) return; + + await Server(); + + async Task Server() + { + var host = HostManager.GenerateLocalHost(); + + UDP.Server server = new(); + + object dataLock = new(); + object eventLock = new(); + int allDataReceived = 0, allEventReceived = 0; + bool isOpen = false, isClose = false, isError = false, isModify = false; + + server.On.Open(() => isOpen = true); + server.On.Close(() => isClose = true); + server.On.Modify(_ => isModify = true); + server.On.Error(exception => + { + isError = true; + output.WriteLine(exception.ToString()); + }); + server.On.Accept(client => + { + client.On.Data(data => + { + client.To.Data(data); + + lock (dataLock) + { + allDataReceived++; + } + }); + + client.On.Event((name, data) => + { + client.To.Event(name, data); + + lock (eventLock) + { + allEventReceived++; + } + }); + }); + + { + Assert.False(server.IsOpened); + Assert.False(isOpen); + Assert.False(isClose); + Assert.False(isError); + Assert.False(isModify); + } + + await server.To.Open(host); + + Thread.Sleep(millisecondsTimeout: 10); + { + Assert.True(server.IsOpened); + Assert.True(isModify); + Assert.True(isOpen); + Assert.False(isClose); + Assert.False(isError); + } + + const int maxConnection = 100; + + for (int i = 0; i < maxConnection; i++) + { + await Client(server.Host); + } + + Thread.Sleep(10); + + Assert.Equal(maxConnection, server.Clients.Length); + Assert.Equal(maxConnection, allDataReceived); + Assert.Equal(maxConnection, allEventReceived); + } + + async Task Client(Host host) + { + UDP.Client client = new(); + + bool isOpen = false, isClose = false, isError = false, isModify = false; + byte[] dataSent = Guid.NewGuid().ToString().GetBytes(); + byte[] dataReceived = []; + (string name, byte[] data) eventSent = (Guid.NewGuid().ToString(), Guid.NewGuid().ToString().GetBytes()); + (string name, byte[] data) eventReceived = (string.Empty, []); + + client.On.Open(() => isOpen = true); + client.On.Close(() => isClose = true); + client.On.Error(_ => isError = true); + client.On.Modify(_ => isModify = true); + client.On.Data(bytes => dataReceived = bytes); + client.On.Event((name, bytes) => eventReceived = (name, bytes)); + { + Assert.False(client.IsOpened); + Assert.False(isOpen); + Assert.False(isClose); + Assert.False(isError); + Assert.False(isModify); + Assert.NotEmpty(dataSent); // data + Assert.NotEmpty(eventSent.name); // event + Assert.NotEmpty(eventSent.data); // event + Assert.Empty(dataReceived); // data + Assert.Empty(eventReceived.name); // event + Assert.Empty(eventReceived.data); // event + } + + await client.To.Open(host); + + client.To.Data(dataSent); + client.To.Event(eventSent.name, eventSent.data); + + Thread.Sleep(millisecondsTimeout: 10); + { + Assert.True(client.IsOpened); + Assert.True(isModify); + Assert.True(isOpen); + Assert.False(isClose); + Assert.False(isError); + Assert.NotEmpty(dataSent); // data + Assert.NotEmpty(eventSent.name); // event + Assert.NotEmpty(eventSent.data); // event + Assert.NotEmpty(dataReceived); // data + Assert.NotEmpty(eventReceived.name); // event + Assert.NotEmpty(eventReceived.data); // event + Assert.Equal(dataSent, dataReceived); // data + Assert.Equal(eventSent.name, eventReceived.name); // event + Assert.Equal(eventSent.data, eventReceived.data); // event + } + } } } \ No newline at end of file