Skip to content

Define timeout for CreateSocket.Ctor_SafeHandle_Listening_Success #56717

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

Merged
Merged
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
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