Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Safar <marek.safar@gmail.com>2017-09-15 12:10:31 +0300
committerMarek Safar <marek.safar@gmail.com>2017-09-18 20:10:00 +0300
commiteaea54ed547571e905f53e44d49ecbbf2920f62b (patch)
treecfe3641f72db2e0fb4a9cb98e6c540088fd139b1 /mcs/class/test-helpers
parent4a20e930afc89aba911f43fb14cce2feeeab65cc (diff)
Update tests network helper to be reliable in multi-threaded tests
Diffstat (limited to 'mcs/class/test-helpers')
-rw-r--r--mcs/class/test-helpers/NetworkHelpers.cs37
1 files changed, 27 insertions, 10 deletions
diff --git a/mcs/class/test-helpers/NetworkHelpers.cs b/mcs/class/test-helpers/NetworkHelpers.cs
index 9d5eb114081..262b8d33232 100644
--- a/mcs/class/test-helpers/NetworkHelpers.cs
+++ b/mcs/class/test-helpers/NetworkHelpers.cs
@@ -1,12 +1,14 @@
using System;
using System.Net;
using System.Net.Sockets;
+using System.Collections.Generic;
namespace MonoTests.Helpers {
public static class NetworkHelpers
{
static Random rndPort = new Random ();
+ static HashSet<int> portsTable = new HashSet<int> ();
public static int FindFreePort ()
{
@@ -15,17 +17,32 @@ namespace MonoTests.Helpers {
public static IPEndPoint LocalEphemeralEndPoint ()
{
- while (true) {
- var ep = new IPEndPoint (IPAddress.Loopback, rndPort.Next (10000, 60000));
-
- try {
- using (var socket = new Socket (ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) {
- socket.Bind (ep);
- socket.Close ();
- }
- return ep;
- } catch (SocketException) { }
+ int counter = 0;
+
+ while (counter < 1000) {
+ var testingPort = rndPort.Next (10000, 60000);
+
+ var ep = new IPEndPoint (IPAddress.Loopback, testingPort);
+
+ lock (portsTable) {
+ if (portsTable.Contains (testingPort))
+ continue;
+
+ ++counter;
+
+ try {
+ using (var socket = new Socket (ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) {
+ socket.Bind (ep);
+ socket.Close ();
+ }
+
+ portsTable.Add (testingPort);
+ return ep;
+ } catch (SocketException) { }
+ }
}
+
+ throw new ApplicationException ($"Could not find available local port after {counter} retries");
}
}
}