try to fix bug

This commit is contained in:
2025-04-15 21:38:06 +08:00
parent e8ec4c2a86
commit 84699708d5
3 changed files with 71 additions and 6 deletions

View File

@@ -1,7 +1,29 @@
namespace server.test.UDPServer;
using System.Net;
using System.Reflection;
using System.Text;
using Xunit.Sdk;
public class UnitTest1
namespace server.test;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class UDPServerTest : BeforeAfterTestAttribute
{
const string address = "127.0.0.1";
const int port = 1234;
private static readonly UDPServer udpServer = new UDPServer(port);
public override void Before(MethodInfo methodUnderTest)
{
udpServer.Start();
Console.WriteLine("Start UDP Server");
}
public override void After(MethodInfo methodUnderTest)
{
udpServer.Stop();
Console.WriteLine("Stop UDP Server");
}
[Fact]
public void UDPDataDeepClone()
{
@@ -33,4 +55,35 @@ public class UnitTest1
Assert.NotEqual(udpData.Data, cloneUdpData.Data);
Assert.NotEqual(udpData.HasRead, cloneUdpData.HasRead);
}
[Theory]
[InlineData("Hello World!")]
[InlineData("Hello Server!")]
public async Task UDPServerFind(string text)
{
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
Assert.True(UDPClientPool.SendString(serverEP, [text]));
{
var ret = udpServer.FindData(address);
Assert.True(ret.HasValue);
var data = ret.Value;
Assert.Equal(data.Address, address);
Assert.Equal(data.Port, port);
Assert.Equal(Encoding.ASCII.GetString(data.Data), text);
}
Assert.True(await UDPClientPool.SendStringAsync(serverEP, [text]));
{
var ret = await udpServer.FindDataAsync(address);
Assert.True(ret.HasValue);
var data = ret.Value;
Assert.Equal(data.Address, address);
Assert.Equal(data.Port, port);
Assert.Equal(Encoding.ASCII.GetString(data.Data), text);
}
}
}