Skip to content

Commit

Permalink
Update FixUdp.SendAndReceive.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Jun 5, 2024
1 parent 0cef886 commit 14fa47a
Showing 1 changed file with 133 additions and 2 deletions.
135 changes: 133 additions & 2 deletions test/udp/FixUdp.SendAndReceive.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,139 @@
public partial class FixUdp
{
[Fact]
public void SendAndReceive()
public async void SendAndReceive()

Check warning on line 4 in test/udp/FixUdp.SendAndReceive.cs

View workflow job for this annotation

GitHub Actions / test (8, ubuntu-latest)

Support for 'async void' unit tests is being removed from xUnit.net v3. To simplify upgrading, convert the test to 'async Task' instead. (https://xunit.net/xunit.analyzers/rules/xUnit1048)

Check warning on line 4 in test/udp/FixUdp.SendAndReceive.cs

View workflow job for this annotation

GitHub Actions / test (8, windows-latest)

Support for 'async void' unit tests is being removed from xUnit.net v3. To simplify upgrading, convert the test to 'async Task' instead. (https://xunit.net/xunit.analyzers/rules/xUnit1048)

Check warning on line 4 in test/udp/FixUdp.SendAndReceive.cs

View workflow job for this annotation

GitHub Actions / test (8, macos-latest)

Support for 'async void' unit tests is being removed from xUnit.net v3. To simplify upgrading, convert the test to 'async Task' instead. (https://xunit.net/xunit.analyzers/rules/xUnit1048)
{
// 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
}
}
}
}

0 comments on commit 14fa47a

Please sign in to comment.