|
17 | 17 |
|
18 | 18 | package org.openqa.selenium.net; |
19 | 19 |
|
20 | | -import static java.lang.Math.max; |
21 | | - |
22 | 20 | import org.openqa.selenium.Platform; |
23 | 21 |
|
24 | 22 | import java.io.IOException; |
@@ -66,29 +64,22 @@ public static int findFreePort() { |
66 | 64 | } |
67 | 65 |
|
68 | 66 | /** |
69 | | - * Returns a port that is within a probable free range. <p/> Based on the ports in |
70 | | - * http://en.wikipedia.org/wiki/Ephemeral_ports, this method stays away from all well-known |
71 | | - * ephemeral port ranges, since they can arbitrarily race with the operating system in |
72 | | - * allocations. Due to the port-greedy nature of selenium this happens fairly frequently. |
73 | | - * Staying within the known safe range increases the probability tests will run green quite |
74 | | - * significantly. |
| 67 | + * Returns a random port within the systems ephemeral port range <p/> |
| 68 | + * See https://en.wikipedia.org/wiki/Ephemeral_ports for more information. <p/> |
| 69 | + * If the system provides a too short range (mostly on old windows systems) |
| 70 | + * the port range suggested from Internet Assigned Numbers Authority will be used. |
75 | 71 | * |
76 | 72 | * @return a random port number |
77 | 73 | */ |
78 | 74 | private static int createAcceptablePort() { |
79 | 75 | synchronized (random) { |
80 | | - final int FIRST_PORT; |
81 | | - final int LAST_PORT; |
82 | | - |
83 | | - int freeAbove = HIGHEST_PORT - ephemeralRangeDetector.getHighestEphemeralPort(); |
84 | | - int freeBelow = max(0, ephemeralRangeDetector.getLowestEphemeralPort() - START_OF_USER_PORTS); |
| 76 | + int FIRST_PORT = Math.max(START_OF_USER_PORTS, ephemeralRangeDetector.getLowestEphemeralPort()); |
| 77 | + int LAST_PORT = Math.min(HIGHEST_PORT, ephemeralRangeDetector.getHighestEphemeralPort()); |
85 | 78 |
|
86 | | - if (freeAbove > freeBelow) { |
87 | | - FIRST_PORT = ephemeralRangeDetector.getHighestEphemeralPort(); |
88 | | - LAST_PORT = 65535; |
89 | | - } else { |
90 | | - FIRST_PORT = 1024; |
91 | | - LAST_PORT = ephemeralRangeDetector.getLowestEphemeralPort(); |
| 79 | + if (LAST_PORT - FIRST_PORT < 5000) { |
| 80 | + EphemeralPortRangeDetector ianaRange = new FixedIANAPortRange(); |
| 81 | + FIRST_PORT = ianaRange.getLowestEphemeralPort(); |
| 82 | + LAST_PORT = ianaRange.getHighestEphemeralPort(); |
92 | 83 | } |
93 | 84 |
|
94 | 85 | if (FIRST_PORT == LAST_PORT) { |
|
0 commit comments