Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/libraries/Common/tests/System/Net/Sockets/TestSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ public static class TestSettings
public const int PassingTestTimeout = 10000;
public const int FailingTestTimeout = 100;

// Number of redundant UDP packets to send to increase test reliability
// Update: was 10, changing to 1 to measure impact of random test failures occurring on *nix.
// Certain random failures appear to be caused by a UDP client sending in a loop (based on UDPRedundancy)
// to a server which was closed but another server created (on a different thread \ test) that happens to
// have the same port #.
// This occurs on *nix but not Windows because *nix uses random values (1024-65535) while Windows increments.
public const int UDPRedundancy = 1;

public static Task WhenAllOrAnyFailedWithTimeout(params Task[] tasks) => tasks.WhenAllOrAnyFailed(PassingTestTimeout);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1090,12 +1090,8 @@ private void DualModeSendTo_IPEndPointToHost_Helper(IPAddress connectTo, IPAddre
using (Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp))
using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out int port))
{
// Send a few packets, in case they aren't delivered reliably.
for (int i = 0; i < (expectedToTimeout ? 1 : TestSettings.UDPRedundancy); i++)
{
int sent = client.SendTo(new byte[1], new IPEndPoint(connectTo, port));
Assert.Equal(1, sent);
}
int sent = client.SendTo(new byte[1], new IPEndPoint(connectTo, port));
Assert.Equal(1, sent);

bool success = server.WaitHandle.WaitOne(expectedToTimeout ? TestSettings.FailingTestTimeout : TestSettings.PassingTestTimeout); // Make sure the bytes were received
if (!success)
Expand Down Expand Up @@ -1189,14 +1185,10 @@ private void DualModeBeginSendTo_EndPointToHost_Helper(IPAddress connectTo, IPAd
using (Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp))
using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out int port))
{
// Send a few packets, in case they aren't delivered reliably.
for (int i = 0; i < (expectedToTimeout ? 1 : TestSettings.UDPRedundancy); i++)
{
IAsyncResult async = client.BeginSendTo(new byte[1], 0, 1, SocketFlags.None, new IPEndPoint(connectTo, port), null, null);
IAsyncResult async = client.BeginSendTo(new byte[1], 0, 1, SocketFlags.None, new IPEndPoint(connectTo, port), null, null);

int sent = client.EndSendTo(async);
Assert.Equal(1, sent);
}
int sent = client.EndSendTo(async);
Assert.Equal(1, sent);

bool success = server.WaitHandle.WaitOne(expectedToTimeout ? TestSettings.FailingTestTimeout : TestSettings.PassingTestTimeout); // Make sure the bytes were received
if (!success)
Expand Down Expand Up @@ -1311,28 +1303,24 @@ private void DualModeSendToAsync_IPEndPointToHost_Helper(IPAddress connectTo, IP
using (Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp))
using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out int port))
{
// Send a few packets, in case they aren't delivered reliably.
for (int i = 0; i < (expectedToTimeout ? 1 : TestSettings.UDPRedundancy); i++)
using (ManualResetEvent waitHandle = new ManualResetEvent(false))
{
using (ManualResetEvent waitHandle = new ManualResetEvent(false))
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(connectTo, port);
args.SetBuffer(new byte[1], 0, 1);
args.UserToken = waitHandle;
args.Completed += AsyncCompleted;

bool async = client.SendToAsync(args);
if (async)
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(connectTo, port);
args.SetBuffer(new byte[1], 0, 1);
args.UserToken = waitHandle;
args.Completed += AsyncCompleted;

bool async = client.SendToAsync(args);
if (async)
{
Assert.True(waitHandle.WaitOne(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");
}

Assert.Equal(1, args.BytesTransferred);
if (args.SocketError != SocketError.Success)
{
throw new SocketException((int)args.SocketError);
}
Assert.True(waitHandle.WaitOne(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");
}

Assert.Equal(1, args.BytesTransferred);
if (args.SocketError != SocketError.Success)
{
throw new SocketException((int)args.SocketError);
}
}

Expand Down Expand Up @@ -1605,7 +1593,7 @@ private void BeginReceiveFrom_Helper(IPAddress listenOn, IPAddress connectTo, bo
// Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
// Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port, redundant: !expectedToTimeout);
SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);
bool success = async.AsyncWaitHandle.WaitOne(expectedToTimeout ? TestSettings.FailingTestTimeout : TestSettings.PassingTestTimeout);
if (!success)
{
Expand Down Expand Up @@ -1750,7 +1738,7 @@ private void ReceiveFromAsync_Helper(IPAddress listenOn, IPAddress connectTo, bo
args.Completed += AsyncCompleted;

bool async = serverSocket.ReceiveFromAsync(args);
SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port, redundant: !expectedToTimeout);
SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);
if (async && !waitHandle.WaitOne(expectedToTimeout ? TestSettings.FailingTestTimeout : TestSettings.PassingTestTimeout))
{
throw new TimeoutException();
Expand Down Expand Up @@ -1978,10 +1966,7 @@ public void ReceiveMessageFromAsync_SocketAsyncEventArgs_Success(bool ipv4)
var ep = new IPEndPoint(loopback, receiverPort);
for (int iters = 0; iters < 5; iters++)
{
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[DataLength], ep);
}
sender.SendTo(new byte[DataLength], ep);

if (!receiver.ReceiveMessageFromAsync(args))
{
Expand Down Expand Up @@ -2203,7 +2188,7 @@ private void BeginReceiveMessageFrom_Helper(IPAddress listenOn, IPAddress connec
// Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
// Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);

SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port, redundant: !expectedToTimeout);
SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);
bool success = async.AsyncWaitHandle.WaitOne(expectedToTimeout ? TestSettings.FailingTestTimeout : TestSettings.PassingTestTimeout);
if (!success)
{
Expand Down Expand Up @@ -2378,7 +2363,7 @@ private void ReceiveMessageFromAsync_Helper(IPAddress listenOn, IPAddress connec
bool async = serverSocket.ReceiveMessageFromAsync(args);
Assert.True(async);

SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port, redundant: !expectedToTimeout);
SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);
if (!waitHandle.WaitOne(serverSocket.ReceiveTimeout))
{
throw new TimeoutException();
Expand Down Expand Up @@ -2708,7 +2693,7 @@ protected class SocketUdpClient
private IPAddress _connectTo;
private Socket _serverSocket;

public SocketUdpClient(ITestOutputHelper output, Socket serverSocket, IPAddress connectTo, int port, bool redundant = true, bool sendNow = true)
public SocketUdpClient(ITestOutputHelper output, Socket serverSocket, IPAddress connectTo, int port, bool sendNow = true)
{
_output = output;

Expand All @@ -2718,25 +2703,22 @@ public SocketUdpClient(ITestOutputHelper output, Socket serverSocket, IPAddress

if (sendNow)
{
Task.Run(() => ClientSend(redundant));
Task.Run(() => ClientSend());
}
}

public void ClientSend(bool redundant = true, int timeout = 3)
public void ClientSend(int timeout = 3)
{
try
{
Socket socket = new Socket(_connectTo.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
socket.SendTimeout = timeout * 1000;

for (int i = 0; i < (redundant ? TestSettings.UDPRedundancy : 1); i++)
{
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.RemoteEndPoint = new IPEndPoint(_connectTo, _port);
e.SetBuffer(new byte[1], 0, 1);
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.RemoteEndPoint = new IPEndPoint(_connectTo, _port);
e.SetBuffer(new byte[1], 0, 1);

socket.SendToAsync(e);
}
socket.SendToAsync(e);
}
catch (SocketException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ private IPPacketInformation GetNonDefaultIPPacketInformation()
Assert.True(receiver.ReceiveMessageFromAsync(receiveArgs), "receiver.ReceiveMessageFromAsync");

// Send a few packets, in case they aren't delivered reliably.
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[1], new IPEndPoint(IPAddress.Loopback, port));
}
sender.SendTo(new byte[1], new IPEndPoint(IPAddress.Loopback, port));

Assert.True(waitHandle.WaitOne(ReceiveTimeout), "waitHandle.WaitOne");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public void Success(bool forceNonBlocking)

sender.ForceNonBlocking(forceNonBlocking);

for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));
}
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));

IPPacketInformation packetInformation;
SocketFlags flags = SocketFlags.None;
Expand Down Expand Up @@ -69,10 +66,7 @@ public void Success_IPv6(bool forceNonBlocking)

sender.ForceNonBlocking(forceNonBlocking);

for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.IPv6Loopback, port));
}
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.IPv6Loopback, port));

IPPacketInformation packetInformation;
SocketFlags flags = SocketFlags.None;
Expand Down Expand Up @@ -122,10 +116,7 @@ public void Success_APM(bool ipv4)
receiver.SetSocketOption(level, SocketOptionName.PacketInformation, true);
sender.Bind(new IPEndPoint(loopback, 0));

for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[1024], new IPEndPoint(loopback, port));
}
sender.SendTo(new byte[1024], new IPEndPoint(loopback, port));

IPPacketInformation packetInformation;
SocketFlags flags = SocketFlags.None;
Expand Down Expand Up @@ -203,10 +194,7 @@ public void Success_EventArgs(bool ipv4, int bufferMode)
saea.Completed += delegate { mres.Set(); };

bool pending = receiver.ReceiveMessageFromAsync(saea);
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[1024], new IPEndPoint(loopback, port));
}
sender.SendTo(new byte[1024], new IPEndPoint(loopback, port));
if (pending) Assert.True(mres.Wait(30000), "Expected operation to complete within timeout");

Assert.Equal(1024, saea.BytesTransferred);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public void ReceiveSentMessages_SocketAsyncEventArgs_Success(bool ipv4, bool cha

for (int iters = 0; iters < 5; iters++)
{
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[DataLength], new IPEndPoint(loopback, port));
}
sender.SendTo(new byte[DataLength], new IPEndPoint(loopback, port));

if (changeReceiveBufferEachCall)
{
Expand Down Expand Up @@ -78,10 +75,7 @@ public async Task ReceiveSentMessages_Tasks_Success(bool ipv4)

for (int iters = 0; iters < 5; iters++)
{
for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[DataLength], new IPEndPoint(loopback, port));
}
sender.SendTo(new byte[DataLength], new IPEndPoint(loopback, port));

SocketReceiveMessageFromResult result = await receiver.ReceiveMessageFromAsync(
new ArraySegment<byte>(new byte[DataLength], 0, DataLength), SocketFlags.None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ public void SelectRead_Single_Success()
int receiverPort = receiver.BindToAnonymousPort(IPAddress.Loopback);
var receiverEndpoint = new IPEndPoint(IPAddress.Loopback, receiverPort);

for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[1], SocketFlags.None, receiverEndpoint);
}
sender.SendTo(new byte[1], SocketFlags.None, receiverEndpoint);

var list = new List<Socket> { receiver };
Socket.Select(list, null, null, SelectSuccessTimeoutMicroseconds);
Expand Down Expand Up @@ -76,11 +73,8 @@ public void SelectRead_Multiple_Success()
int secondReceiverPort = secondReceiver.BindToAnonymousPort(IPAddress.Loopback);
var secondReceiverEndpoint = new IPEndPoint(IPAddress.Loopback, secondReceiverPort);

for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[1], SocketFlags.None, firstReceiverEndpoint);
sender.SendTo(new byte[1], SocketFlags.None, secondReceiverEndpoint);
}
sender.SendTo(new byte[1], SocketFlags.None, firstReceiverEndpoint);
sender.SendTo(new byte[1], SocketFlags.None, secondReceiverEndpoint);

var sw = Stopwatch.StartNew();
Assert.True(SpinWait.SpinUntil(() =>
Expand Down Expand Up @@ -127,10 +121,7 @@ public void SelectRead_Multiple_Mixed()
int secondReceiverPort = secondReceiver.BindToAnonymousPort(IPAddress.Loopback);
var secondReceiverEndpoint = new IPEndPoint(IPAddress.Loopback, secondReceiverPort);

for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[1], SocketFlags.None, secondReceiverEndpoint);
}
sender.SendTo(new byte[1], SocketFlags.None, secondReceiverEndpoint);

var list = new List<Socket> { firstReceiver, secondReceiver };
Socket.Select(list, null, null, SelectSuccessTimeoutMicroseconds);
Expand Down Expand Up @@ -276,10 +267,7 @@ public void PollRead_Single_Success()
int receiverPort = receiver.BindToAnonymousPort(IPAddress.Loopback);
var receiverEndpoint = new IPEndPoint(IPAddress.Loopback, receiverPort);

for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sender.SendTo(new byte[1], SocketFlags.None, receiverEndpoint);
}
sender.SendTo(new byte[1], SocketFlags.None, receiverEndpoint);

Assert.True(receiver.Poll(SelectSuccessTimeoutMicroseconds, SelectMode.SelectRead));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ private async Task MulticastInterface_Set_Helper(int interfaceIndex)
var receiveBuffer = new byte[1024];
var receiveTask = receiveSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), SocketFlags.None);

for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sendSocket.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(multicastAddress, port));
}
sendSocket.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(multicastAddress, port));

var cts = new CancellationTokenSource();
Assert.True(await Task.WhenAny(receiveTask, Task.Delay(30_000, cts.Token)) == receiveTask, "Waiting for received data timed out");
Expand Down Expand Up @@ -213,10 +210,7 @@ private async Task MulticastInterface_Set_IPv6_Helper(int interfaceIndex)
var receiveBuffer = new byte[1024];
var receiveTask = receiveSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), SocketFlags.None);

for (int i = 0; i < TestSettings.UDPRedundancy; i++)
{
sendSocket.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(multicastAddress, port));
}
sendSocket.SendTo(Encoding.UTF8.GetBytes(message), new IPEndPoint(multicastAddress, port));

var cts = new CancellationTokenSource();
Assert.True(await Task.WhenAny(receiveTask, Task.Delay(30_000, cts.Token)) == receiveTask, "Waiting for received data timed out");
Expand Down
Loading