Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UdpClient with span support #53429

Prev Previous commit
Fix comment: add more test for new SendAsync overloads.
  • Loading branch information
lateapexearlyspeed committed Jun 18, 2021
commit a368fff704ea2be841eb65c123ca233c150033cf
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,12 @@ public async Task SendAsync_ReceiveAsync_Connected_Success()

await sender.SendAsync(new ReadOnlyMemory<byte>(new byte[1]));
await AssertReceiveAsync(receiver);

await sender.SendAsync(new ReadOnlyMemory<byte>(new byte[1]), null);
await AssertReceiveAsync(receiver);

await sender.SendAsync(new ReadOnlyMemory<byte>(new byte[1]), null, 0);
await AssertReceiveAsync(receiver);
}
}

Expand All @@ -693,6 +699,46 @@ private static async Task AssertReceiveAsync(UdpClient receiver)
Assert.InRange(result.Buffer.Length, 1, int.MaxValue);
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // "localhost" resolves to IPv4 & IPV6 on Windows, but may resolve to only one of those on Unix
public async Task SendAsync_Connected_PreCanceled_Throws()
{
using (var receiver = new UdpClient("localhost", 0))
using (var sender = new UdpClient("localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port))
{
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sender.SendAsync(new ReadOnlyMemory<byte>(new byte[1]), new CancellationToken(true)).AsTask());
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
[PlatformSpecific(TestPlatforms.Windows)] // "localhost" resolves to IPv4 & IPV6 on Windows, but may resolve to only one of those on Unix
public async Task SendAsync_With_HostName_PreCanceled_Throws(bool ipv4)
{
IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;

using (var receiver = new UdpClient(new IPEndPoint(address, 0)))
using (var sender = new UdpClient(new IPEndPoint(address, 0)))
{
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sender.SendAsync(new ReadOnlyMemory<byte>(new byte[1]), "localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port, new CancellationToken(true)).AsTask());
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task SendAsync_PreCanceled_Throws(bool ipv4)
{
IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;

using (var receiver = new UdpClient(new IPEndPoint(address, 0)))
using (var sender = new UdpClient(new IPEndPoint(address, 0)))
{
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sender.SendAsync(new ReadOnlyMemory<byte>(new byte[1]), new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port), new CancellationToken(true)).AsTask());
}
}

[Fact]
public void JoinDropMulticastGroup_InvalidArguments_Throws()
{
Expand Down