forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KristianRosenvold: Made portprober use platform-specific detection of…
… ephemeral socket ports Due to the floppy-disk themorem (the two free floppies at the back of the box are by definition defect, otherwise they would've been in-use and hence not at the back of the box), the likelihood of getting race conditions with ephemeral ports increases significantly the longer the build runs. This typically results is tests failing if you run the entire suite, but not one by one. The current solution has a real good linux implementation, and some half-nice defaults for windows xp. UnitTest for linux added. r13813
- Loading branch information
1 parent
56c6d7f
commit 965bd13
Showing
7 changed files
with
245 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
#!/bin/sh | ||
git update-index --assume-unchanged cpp/prebuilt/**/*.so | ||
git update-index --assume-unchanged cpp/IEDriver/Generated/atoms.h |
45 changes: 45 additions & 0 deletions
45
java/client/src/org/openqa/selenium/net/EphemeralPortRangeDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.openqa.selenium.net; | ||
|
||
/* | ||
Copyright 2011 WebDriver committers | ||
Copyright 2011 Google Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Identifies the ephemeral port range for a given environment. | ||
* | ||
* When trying to locate a "random" free port, it is important | ||
* to not allocate within the ephemeral range, since these | ||
* can be allocated at any time, and the probability of | ||
* race conditions increases as the number of recently used ports | ||
* increases, something which is quite common when running the | ||
* webdriver tests. | ||
* | ||
*/ | ||
public interface EphemeralPortRangeDetector | ||
{ | ||
|
||
/** | ||
* Returns the first port in the ephemeral range | ||
* @return The first ephemeral port | ||
*/ | ||
public int getLowestEphemeralPort(); | ||
/** | ||
* Returns the last port that could be searched for free ports | ||
* @return The first port that may be free | ||
*/ | ||
public int getHighestEphemeralPort(); | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
java/client/src/org/openqa/selenium/net/FixedIANAPortRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.openqa.selenium.net; | ||
/* | ||
Copyright 2011 WebDriver committers | ||
Copyright 2011 Google Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
|
||
public class FixedIANAPortRange implements EphemeralPortRangeDetector | ||
{ | ||
public int getLowestEphemeralPort() { | ||
return 49152; | ||
} | ||
|
||
public int getHighestEphemeralPort() { | ||
return 65535; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
java/client/src/org/openqa/selenium/net/LinuxEphemeralPortRangeDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.openqa.selenium.net; | ||
|
||
/* | ||
Copyright 2011 WebDriver committers | ||
Copyright 2011 Google Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
|
||
public class LinuxEphemeralPortRangeDetector | ||
implements EphemeralPortRangeDetector { | ||
|
||
final int firstEphemeralPort; | ||
final int lastEphemeralPort; | ||
|
||
public static LinuxEphemeralPortRangeDetector getInstance() { | ||
File file = new File("/proc/sys/net/ipv4/ip_local_port_range"); | ||
if (file.exists() && file.canRead()) { | ||
Reader inputFil = null; | ||
try { | ||
inputFil = new FileReader(file); | ||
} catch (FileNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return new LinuxEphemeralPortRangeDetector(inputFil); | ||
} | ||
return new LinuxEphemeralPortRangeDetector(new StringReader("49152 65535")); | ||
} | ||
|
||
LinuxEphemeralPortRangeDetector(Reader inputFil) { | ||
FixedIANAPortRange defaultRange = new FixedIANAPortRange(); | ||
int lowPort = defaultRange.getLowestEphemeralPort(); | ||
int highPort = defaultRange.getHighestEphemeralPort(); | ||
try { | ||
BufferedReader in = new BufferedReader(inputFil); | ||
final String s; | ||
s = in.readLine(); | ||
final String[] split = s.split("\\s"); | ||
lowPort = Integer.parseInt(split[0]); | ||
highPort = Integer.parseInt(split[1]); | ||
} catch (IOException ignore) { | ||
} | ||
firstEphemeralPort = lowPort; | ||
lastEphemeralPort = highPort; | ||
} | ||
|
||
public int getLowestEphemeralPort() { | ||
return firstEphemeralPort; | ||
} | ||
|
||
public int getHighestEphemeralPort() { | ||
return lastEphemeralPort; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
java/client/src/org/openqa/selenium/net/OlderWindowsVersionEphemeralPortDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.openqa.selenium.net; | ||
|
||
/* | ||
Copyright 2011 WebDriver committers | ||
Copyright 2011 Google Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
public class OlderWindowsVersionEphemeralPortDetector implements EphemeralPortRangeDetector | ||
{ | ||
public int getLowestEphemeralPort() { | ||
// This could read the registry to get effective values | ||
return 1025; | ||
} | ||
|
||
public int getHighestEphemeralPort() { | ||
return 5000; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
java/client/test/org/openqa/selenium/net/LinuxEphemeralPortRangeDetectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.openqa.selenium.net; | ||
/* | ||
Copyright 2011 WebDriver committers | ||
Copyright 2011 Google Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import org.junit.Assume; | ||
import org.junit.Test; | ||
import org.openqa.selenium.Platform; | ||
|
||
import java.io.StringReader; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertTrue; | ||
import static org.openqa.selenium.Platform.LINUX; | ||
|
||
public class LinuxEphemeralPortRangeDetectorTest { | ||
|
||
@Test | ||
public void decodeEphemeralPorts() throws Exception { | ||
String range ="1234 65533"; | ||
EphemeralPortRangeDetector ephemeralEphemeralPortDetector = new LinuxEphemeralPortRangeDetector(new StringReader(range)); | ||
assertEquals( 1234, ephemeralEphemeralPortDetector.getLowestEphemeralPort()); | ||
assertEquals( 65533, ephemeralEphemeralPortDetector.getHighestEphemeralPort()); | ||
} | ||
|
||
@Test | ||
public void currentValues(){ | ||
Assume.assumeTrue(Platform.getCurrent().is(LINUX)); | ||
LinuxEphemeralPortRangeDetector detector = LinuxEphemeralPortRangeDetector.getInstance(); | ||
assertTrue( detector.getLowestEphemeralPort() > 1024); | ||
assertTrue( detector.getHighestEphemeralPort() < 65536); | ||
} | ||
} |