[System] Refactor tests to use system-assigned ports whenever possible. (#11829)
commitc93db5b04710cfd505c9672ed4f7263319e77abf
authorRolf Bjarne Kvinge <rolf@xamarin.com>
Thu, 29 Nov 2018 13:02:07 +0000 (29 14:02 +0100)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Thu, 29 Nov 2018 13:02:07 +0000 (29 14:02 +0100)
tree9ad7586d97017bb583e757754f437d6f1916beae
parent374925522bc2dd6186a98004969dc3b17320ad36
[System] Refactor tests to use system-assigned ports whenever possible. (#11829)

Refactor tests to use system-assigned ports whenever possible, so that we
avoid the potential race condition in NetworkHelpers.FindFreePort.

This should make it much less probable to run into spurious "Address already
in use" exceptions.

In some cases I kept the call to NetworkHelpers.FindFreePort:

* When the test is not binding/listening on a port, but instead trying to
  connect to an inexistent port (for tests that verifies error handling /
  exceptions). In this case there is still a race condition (if a test tries
  to verify that a connection fails, it may unexpectedly succeed instead).

* Sometimes the test doesn't actually use the port (the test is verifying
  something very different, for instance a ArgumentNullExceptions, which are
  thrown before the port is used).

* Some API doesn't allow for requesting a system assigned port:

    * HttpListener: in this case I elected to re-try a few times in case of
      "Address already in use" exceptions.

    * The 'UdpClient (string, port)' constructor (other constructors work
      fine): these turned out a bit complicated to work around (or would
      modify the test more than would be reasonable), so for these we still
      have to hope for the best.

Hopefully fixes https://github.com/xamarin/maccore/issues/604.
15 files changed:
mcs/class/System/Test/System.Net.Security/SslStreamTest.cs
mcs/class/System/Test/System.Net.Sockets/SocketTest.cs
mcs/class/System/Test/System.Net.Sockets/TcpClientTest.cs
mcs/class/System/Test/System.Net.Sockets/TcpListenerTest.cs
mcs/class/System/Test/System.Net.Sockets/UdpClientTest.cs
mcs/class/System/Test/System.Net.WebSockets/ClientWebSocketTest.cs
mcs/class/System/Test/System.Net/HttpListener2Test.cs
mcs/class/System/Test/System.Net/HttpListenerRequestTest.cs
mcs/class/System/Test/System.Net/HttpListenerTest.cs
mcs/class/System/Test/System.Net/HttpWebRequestTest.cs
mcs/class/System/Test/System.Net/HttpWebResponseTest.cs
mcs/class/System/Test/System.Net/WebClientTest.cs
mcs/class/System/Test/System.Net/WebRequestTest.cs
mcs/class/test-helpers/NetworkHelpers.cs
mcs/class/test-helpers/SocketResponder.cs