public async Task Dispose_CancelsConnectAsync(bool connectByName)
{
using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
// Set up a server socket to which to connect
server.Bind(new IPEndPoint(IPAddress.Loopback, 0));
server.Listen(1);
var endpoint = (IPEndPoint)server.LocalEndPoint;
// Connect asynchronously...
var client = new TcpClient();
Task connectTask = connectByName ?
client.ConnectAsync("localhost", endpoint.Port) :
client.ConnectAsync(endpoint.Address, endpoint.Port);
// ...and hopefully before it's completed connecting, dispose.
var sw = Stopwatch.StartNew();
client.Dispose();
// There is a race condition here. If the connection succeeds before the
// disposal, then the task will complete successfully. Otherwise, it should
// fail with an ObjectDisposedException.
try
{
await connectTask;
}
catch (ObjectDisposedException) { }
sw.Stop();
Assert.Null(client.Client); // should be nulled out after Dispose
}
}
}