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

Use timeouts in 2 socket test cases #84030

Merged
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -165,50 +165,56 @@ public async Task SendRecv_Stream_TCP(IPAddress listenAt, bool useMultipleBuffer
[MemberData(nameof(Loopbacks))]
public async Task SendRecv_Stream_TCP_LargeMultiBufferSends(IPAddress listenAt)
{
using (var listener = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
using (var client = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
using var portBlocker = new PortBlocker(() => {
var l = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
l.BindToAnonymousPort(listenAt);
return l;
});

Socket listener = portBlocker.MainSocket;
using var client = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

listener.Listen(1);

Task<Socket> acceptTask = AcceptAsync(listener)
.WaitAsync(TimeSpan.FromMilliseconds(TestSettings.PassingTestTimeout));
await client.ConnectAsync(listener.LocalEndPoint)
.WaitAsync(TimeSpan.FromMilliseconds(TestSettings.PassingTestTimeout));
using Socket server = await acceptTask;

var sentChecksum = new Fletcher32();
var rand = new Random();
int bytesToSend = 0;
var buffers = new List<ArraySegment<byte>>();
const int NumBuffers = 5;
for (int i = 0; i < NumBuffers; i++)
{
listener.BindToAnonymousPort(listenAt);
listener.Listen(1);

Task<Socket> acceptTask = AcceptAsync(listener);
await client.ConnectAsync(listener.LocalEndPoint);
using (Socket server = await acceptTask)
{
var sentChecksum = new Fletcher32();
var rand = new Random();
int bytesToSend = 0;
var buffers = new List<ArraySegment<byte>>();
const int NumBuffers = 5;
for (int i = 0; i < NumBuffers; i++)
{
var sendBuffer = new byte[12345678];
rand.NextBytes(sendBuffer);
bytesToSend += sendBuffer.Length - i; // trim off a few bytes to test offset/count
sentChecksum.Add(sendBuffer, i, sendBuffer.Length - i);
buffers.Add(new ArraySegment<byte>(sendBuffer, i, sendBuffer.Length - i));
}

Task<int> sendTask = SendAsync(client, buffers);
var sendBuffer = new byte[12345678];
rand.NextBytes(sendBuffer);
bytesToSend += sendBuffer.Length - i; // trim off a few bytes to test offset/count
sentChecksum.Add(sendBuffer, i, sendBuffer.Length - i);
buffers.Add(new ArraySegment<byte>(sendBuffer, i, sendBuffer.Length - i));
}

var receivedChecksum = new Fletcher32();
int bytesReceived = 0;
byte[] recvBuffer = new byte[1024];
while (bytesReceived < bytesToSend)
{
int received = await ReceiveAsync(server, new ArraySegment<byte>(recvBuffer));
if (received <= 0)
{
break;
}
bytesReceived += received;
receivedChecksum.Add(recvBuffer, 0, received);
}
Task<int> sendTask = SendAsync(client, buffers).WaitAsync(TimeSpan.FromMilliseconds(TestSettings.PassingTestTimeout));

Assert.Equal(bytesToSend, await sendTask);
Assert.Equal(sentChecksum.Sum, receivedChecksum.Sum);
var receivedChecksum = new Fletcher32();
int bytesReceived = 0;
byte[] recvBuffer = new byte[1024];
while (bytesReceived < bytesToSend)
{
int received = await ReceiveAsync(server, new ArraySegment<byte>(recvBuffer))
.WaitAsync(TimeSpan.FromMilliseconds(TestSettings.PassingTestTimeout));
if (received <= 0)
{
break;
}
bytesReceived += received;
receivedChecksum.Add(recvBuffer, 0, received);
}

Assert.Equal(bytesToSend, await sendTask);
Assert.Equal(sentChecksum.Sum, receivedChecksum.Sum);
}

[OuterLoop]
Expand Down Expand Up @@ -1149,7 +1155,7 @@ await RetryHelper.ExecuteAsync(async () =>
int received;
do
{
received = await ReceiveAsync(socket2, receiveBuffer);
received = await ReceiveAsync(socket2, receiveBuffer).WaitAsync(TimeSpan.FromMilliseconds(TestSettings.PassingTestTimeout));
receivedTotal += received;
} while (received != 0);

Expand Down