[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: