Skip to content

Commit

Permalink
Define timeout for CreateSocket.Ctor_SafeHandle_Listening_Success (#5…
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfirsov authored Aug 6, 2021
1 parent 2bc925a commit ebc0f05
Showing 1 changed file with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,35 +449,38 @@ public async Task Ctor_SafeHandle_Tcp_SendReceive_Success(AddressFamily addressF
[ActiveIssue("https://github.com/dotnet/runtime/issues/52124", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
public async Task Ctor_SafeHandle_Listening_Success(bool shareSafeHandle)
{
using var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listener.Listen();

using var listenerCopy = new Socket(shareSafeHandle ? listener.SafeHandle : new SafeSocketHandle(listener.Handle, ownsHandle: false));
Assert.False(listenerCopy.Connected);
// This will throw if _isListening is set internally. (before reaching any real code)
Assert.Throws<InvalidOperationException>(() => listenerCopy.Connect(new IPEndPoint(IPAddress.Loopback,0)));

Assert.Equal(listener.AddressFamily, listenerCopy.AddressFamily);
Assert.Equal(listener.Handle, listenerCopy.Handle);
Assert.Equal(listener.IsBound, listenerCopy.IsBound);
Assert.Equal(listener.LocalEndPoint, listenerCopy.LocalEndPoint);
Assert.Equal(listener.ProtocolType, listenerCopy.ProtocolType);
Assert.Equal(listener.SocketType, listenerCopy.SocketType);

foreach (Socket listenerSocket in new[] { listener, listenerCopy })
await Task.Run(async () =>
{
using (var client1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
using var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listener.Listen();
using var listenerCopy = new Socket(shareSafeHandle ? listener.SafeHandle : new SafeSocketHandle(listener.Handle, ownsHandle: false));
Assert.False(listenerCopy.Connected);
// This will throw if _isListening is set internally. (before reaching any real code)
Assert.Throws<InvalidOperationException>(() => listenerCopy.Connect(new IPEndPoint(IPAddress.Loopback, 0)));
Assert.Equal(listener.AddressFamily, listenerCopy.AddressFamily);
Assert.Equal(listener.Handle, listenerCopy.Handle);
Assert.Equal(listener.IsBound, listenerCopy.IsBound);
Assert.Equal(listener.LocalEndPoint, listenerCopy.LocalEndPoint);
Assert.Equal(listener.ProtocolType, listenerCopy.ProtocolType);
Assert.Equal(listener.SocketType, listenerCopy.SocketType);
foreach (Socket listenerSocket in new[] { listener, listenerCopy })
{
Task connect1 = client1.ConnectAsync(listenerSocket.LocalEndPoint);
using (Socket server1 = listenerSocket.Accept())
using (var client1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
await connect1;
server1.Send(new byte[] { 42 });
Assert.Equal(1, client1.Receive(new byte[1]));
Task connect1 = client1.ConnectAsync(listenerSocket.LocalEndPoint);
using (Socket server1 = listenerSocket.Accept())
{
await connect1;
server1.Send(new byte[] { 42 });
Assert.Equal(1, client1.Receive(new byte[1]));
}
}
}
}
}).WaitAsync(TestSettings.PassingTestTimeout);
}

[DllImport("libc")]
Expand Down

0 comments on commit ebc0f05

Please sign in to comment.