Skip to content

Commit

Permalink
KristianRosenvold: Made portprober use platform-specific detection of…
Browse files Browse the repository at this point in the history
… 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
krosenvold committed Sep 9, 2011
1 parent 56c6d7f commit 965bd13
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 4 deletions.
1 change: 1 addition & 0 deletions .git-fixfiles
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
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 java/client/src/org/openqa/selenium/net/FixedIANAPortRange.java
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;
}
}
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;
}
}
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;
}
}
26 changes: 22 additions & 4 deletions java/client/src/org/openqa/selenium/net/PortProber.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.net;

import org.openqa.selenium.Platform;

import java.io.IOException;
import java.net.ConnectException;
import java.net.ServerSocket;
Expand All @@ -32,8 +34,18 @@
public class PortProber {

private static final Random random = new Random();
private static final EphemeralPortRangeDetector ephemeralRangeDetector;

static {
final Platform current = Platform.getCurrent();

private PortProber() {
if (current.is(Platform.LINUX)) {
ephemeralRangeDetector = LinuxEphemeralPortRangeDetector.getInstance();
} else if (current.is(Platform.XP)){
ephemeralRangeDetector = new OlderWindowsVersionEphemeralPortDetector();
} else {
ephemeralRangeDetector = new FixedIANAPortRange();
}
}

public static int findFreePort() {
Expand Down Expand Up @@ -72,10 +84,16 @@ public Integer call()
*/
private static int createAcceptablePort() {
synchronized (random) {
// avoid protected ports. Ideally this should be platform-specific

final int FIRST_PORT = 5001;
final int LAST_PORT = 32767;
final int FIRST_PORT;
final int LAST_PORT;
if (ephemeralRangeDetector.getHighestEphemeralPort() < 32768){
FIRST_PORT = ephemeralRangeDetector.getHighestEphemeralPort() + 1;
LAST_PORT = 65535;
} else {
FIRST_PORT = 1024;
LAST_PORT = ephemeralRangeDetector.getLowestEphemeralPort() - 1;
}
final int randomInt = random.nextInt();
final int portWithoutOffset = Math.abs(randomInt % (LAST_PORT - FIRST_PORT + 1));
return portWithoutOffset + FIRST_PORT;
Expand Down
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);
}
}

0 comments on commit 965bd13

Please sign in to comment.