change udpData type from list to queue
This commit is contained in:
@@ -1,27 +1,47 @@
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Xunit.Sdk;
|
||||
using Common;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace server.test;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class UDPServerTest : BeforeAfterTestAttribute
|
||||
public sealed class RepeatAttribute : Xunit.Sdk.DataAttribute
|
||||
{
|
||||
const string address = "127.0.0.1";
|
||||
const int port = 1234;
|
||||
private static readonly UDPServer udpServer = new UDPServer(port);
|
||||
private readonly int count;
|
||||
|
||||
public override void Before(MethodInfo methodUnderTest)
|
||||
public RepeatAttribute(int count)
|
||||
{
|
||||
udpServer.Start();
|
||||
Console.WriteLine("Start UDP Server");
|
||||
if (count < 1)
|
||||
{
|
||||
throw new System.ArgumentOutOfRangeException(
|
||||
paramName: nameof(count),
|
||||
message: "Repeat count must be greater than 0."
|
||||
);
|
||||
}
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public override void After(MethodInfo methodUnderTest)
|
||||
public override System.Collections.Generic.IEnumerable<object[]> GetData(System.Reflection.MethodInfo testMethod)
|
||||
{
|
||||
udpServer.Stop();
|
||||
Console.WriteLine("Stop UDP Server");
|
||||
foreach (var iterationNumber in Enumerable.Range(start: 1, count: this.count))
|
||||
{
|
||||
yield return new object[] { iterationNumber };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class UDPServerTest
|
||||
{
|
||||
const string address = "127.0.0.1";
|
||||
const int port = 33000;
|
||||
private static readonly UDPServer udpServer = new UDPServer(port);
|
||||
|
||||
private readonly ITestOutputHelper output;
|
||||
|
||||
public UDPServerTest(ITestOutputHelper output)
|
||||
{
|
||||
this.output = output;
|
||||
udpServer.Start();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -46,9 +66,15 @@ public class UDPServerTest : BeforeAfterTestAttribute
|
||||
udpData.DateTime = DateTime.Now;
|
||||
udpData.Address = "192.168.1.1";
|
||||
udpData.Port = 33000;
|
||||
udpData.Data = new byte[] { 0x0f, 00, 00, 00 };
|
||||
udpData.Data = new byte[] { 0xFF, 00, 00, 00 };
|
||||
udpData.HasRead = true;
|
||||
|
||||
Assert.NotNull(cloneUdpData.DateTime);
|
||||
Assert.NotNull(cloneUdpData.Address);
|
||||
Assert.NotNull(cloneUdpData.Port);
|
||||
Assert.NotNull(cloneUdpData.Data);
|
||||
Assert.NotNull(cloneUdpData.HasRead);
|
||||
|
||||
Assert.NotEqual(udpData.DateTime, cloneUdpData.DateTime);
|
||||
Assert.NotEqual(udpData.Address, cloneUdpData.Address);
|
||||
Assert.NotEqual(udpData.Port, cloneUdpData.Port);
|
||||
@@ -57,33 +83,80 @@ public class UDPServerTest : BeforeAfterTestAttribute
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Hello World!")]
|
||||
[InlineData("Hello Server!")]
|
||||
public async Task UDPServerFind(string text)
|
||||
[InlineData(new object[] { new string[] { "Hello World!", "Hello Server!", "What is your problem?" } })]
|
||||
public async Task UDPServerFindString(string[] textArray)
|
||||
{
|
||||
Assert.True(udpServer.IsRunning);
|
||||
|
||||
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]));
|
||||
foreach (var text in textArray)
|
||||
{
|
||||
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);
|
||||
Assert.Equal(address, data.Address);
|
||||
Assert.Equal(text, Encoding.ASCII.GetString(data.Data));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new object[] { new UInt32[] { 0xF0_00_00_00, 0xFF_00_00_00, 0xFF_FF_FF_FF } })]
|
||||
public async Task UDPServerFindBytes(UInt32[] bytesArray)
|
||||
{
|
||||
Assert.True(udpServer.IsRunning);
|
||||
|
||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
||||
|
||||
foreach (var number in bytesArray)
|
||||
{
|
||||
Assert.True(await UDPClientPool.SendBytesAsync(serverEP, NumberProcessor.NumberToBytes(number, 4).Value));
|
||||
var ret = await udpServer.FindDataAsync(address);
|
||||
Assert.True(ret.HasValue);
|
||||
var data = ret.Value;
|
||||
Assert.Equal(address, data.Address);
|
||||
Assert.Equal(number, NumberProcessor.BytesToNumber(data.Data));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new object[] { new UInt32[] { 0xF0_00_00_00, 0xF0_01_00_00 } })]
|
||||
public async Task UDPServerWaitResp(UInt32[] bytesArray)
|
||||
{
|
||||
Assert.True(udpServer.IsRunning);
|
||||
|
||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
||||
|
||||
foreach (var number in bytesArray)
|
||||
{
|
||||
Assert.True(await UDPClientPool.SendBytesAsync(serverEP, NumberProcessor.NumberToBytes(number, 4).Value));
|
||||
|
||||
var ret = await udpServer.WaitForAckAsync(address);
|
||||
Assert.True(ret.IsSuccessful);
|
||||
var data = ret.Value;
|
||||
Assert.True(data.IsSuccessful);
|
||||
Assert.Equal(number, NumberProcessor.BytesToNumber(data.ToBytes()));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new object[] { new UInt64[] { 0x0F_00_00_00_01_02_02_02, 0x0F_01_00_00_FF_FF_FF_FF } })]
|
||||
public async Task UDPServerWaitData(UInt64[] bytesArray)
|
||||
{
|
||||
Assert.True(udpServer.IsRunning);
|
||||
|
||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
||||
|
||||
foreach (var number in bytesArray)
|
||||
{
|
||||
Assert.True(await UDPClientPool.SendBytesAsync(serverEP, NumberProcessor.NumberToBytes(number, 8).Value));
|
||||
|
||||
var ret = await udpServer.WaitForDataAsync(address);
|
||||
Assert.True(ret.IsSuccessful);
|
||||
var data = ret.Value;
|
||||
Assert.True(data.IsSuccessful);
|
||||
Assert.Equal(number, NumberProcessor.BytesToNumber(data.ToBytes()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user