Author: kwall
Date: Thu Jul 10 12:46:42 2014
New Revision: 1609454
URL: http://svn.apache.org/r1609454
Log:
NO-JIRA: [Java Test Framework] PortHelper, apply socket option *before* binding
the socket
* Also removed the datagramsocket bind. We don't used datagram sockets at all
at the moment, so
the complication/overhead seems unjustifiable.
Modified:
qpid/trunk/qpid/java/qpid-test-utils/src/main/java/org/apache/qpid/test/utils/PortHelper.java
Modified:
qpid/trunk/qpid/java/qpid-test-utils/src/main/java/org/apache/qpid/test/utils/PortHelper.java
URL:
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/qpid-test-utils/src/main/java/org/apache/qpid/test/utils/PortHelper.java?rev=1609454&r1=1609453&r2=1609454&view=diff
==============================================================================
---
qpid/trunk/qpid/java/qpid-test-utils/src/main/java/org/apache/qpid/test/utils/PortHelper.java
(original)
+++
qpid/trunk/qpid/java/qpid-test-utils/src/main/java/org/apache/qpid/test/utils/PortHelper.java
Thu Jul 10 12:46:42 2014
@@ -19,7 +19,7 @@
package org.apache.qpid.test.utils;
import java.io.IOException;
-import java.net.DatagramSocket;
+import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.Set;
@@ -31,7 +31,7 @@ public class PortHelper
private static final int DEFAULT_TIMEOUT_MILLIS = 5000;
- private int timeout = DEFAULT_TIMEOUT_MILLIS;
+ private int _timeout = DEFAULT_TIMEOUT_MILLIS;
public void waitUntilPortsAreFree(Set<Integer> ports)
{
@@ -48,14 +48,14 @@ public class PortHelper
private void waitUntilPortIsFree(int port)
{
long startTime = System.currentTimeMillis();
- long deadline = startTime + timeout;
+ long deadline = startTime + _timeout;
boolean alreadyFailed = false;
while (true)
{
if (System.currentTimeMillis() > deadline)
{
- throw new RuntimeException("Timed out after " + timeout + " ms
waiting for port " + port + " to become available");
+ throw new RuntimeException("Timed out after " + _timeout + "
ms waiting for port " + port + " to become available");
}
if (isPortAvailable(port))
@@ -85,14 +85,12 @@ public class PortHelper
public boolean isPortAvailable(int port)
{
ServerSocket serverSocket = null;
- DatagramSocket datagramSocket = null;
-
try
{
- serverSocket = new ServerSocket(port);
+ serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true); // ensures that the port is
subsequently usable
- datagramSocket = new DatagramSocket(port);
- datagramSocket.setReuseAddress(true);
+ serverSocket.bind(new InetSocketAddress(port));
+
return true;
}
catch (IOException e)
@@ -110,18 +108,16 @@ public class PortHelper
}
catch (IOException e)
{
- throw new RuntimeException("Couldn't close port " + port +
" that we created to check its availability", e);
+ throw new RuntimeException("Couldn't close port "
+ + port
+ + " that we created to check
its availability", e);
}
}
- if(datagramSocket != null)
- {
- datagramSocket.close();
- }
}
}
public void setTimeout(int timeout)
{
- this.timeout = timeout;
+ this._timeout = timeout;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]