NO-JIRA: Use dummy server to accept tcp connections from failover
diff --git a/systests/src/test/java/org/apache/qpid/systest/connection/FailoverMethodTest.java b/systests/src/test/java/org/apache/qpid/systest/connection/FailoverMethodTest.java
index 58d140d..61f8a2a 100644
--- a/systests/src/test/java/org/apache/qpid/systest/connection/FailoverMethodTest.java
+++ b/systests/src/test/java/org/apache/qpid/systest/connection/FailoverMethodTest.java
@@ -25,13 +25,19 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeThat;
+import java.io.IOException;
import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import com.google.common.util.concurrent.SettableFuture;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
@@ -41,7 +47,6 @@
import org.apache.qpid.client.AMQConnectionURL;
import org.apache.qpid.systest.core.BrokerAdmin;
import org.apache.qpid.systest.core.JmsTestBase;
-import org.apache.qpid.systest.core.util.PortHelper;
import org.apache.qpid.util.SystemUtils;
public class FailoverMethodTest extends JmsTestBase implements ExceptionListener
@@ -50,16 +55,24 @@
private final SettableFuture<JMSException> _failoverComplete = SettableFuture.create();
private int _freePortWithNoBroker;
private int _port;
+ private DummyServer _dummyServer;
@Before
- public void setUp()
+ public void setUp() throws Exception
{
assumeThat("Test requires redevelopment - timings/behaviour on windows mean it fails",
SystemUtils.isWindows(), is(not(true)));
+ _dummyServer = new DummyServer();
InetSocketAddress brokerAddress = getBrokerAdmin().getBrokerAddress(BrokerAdmin.PortType.AMQP);
_port = brokerAddress.getPort();
- _freePortWithNoBroker = new PortHelper().getNextAvailable();
+ _freePortWithNoBroker = _dummyServer.start();
+ }
+
+ @After
+ public void tearDown()
+ {
+ _dummyServer.stop();
}
/**
* Test that the round robin method has the correct delays.
@@ -178,4 +191,72 @@
{
_failoverComplete.set(e);
}
+
+ class DummyServer implements Runnable
+ {
+ private ExecutorService _executorService;
+ private ServerSocket _serverSocket;
+ private boolean _started;
+
+ synchronized int start() throws IOException
+ {
+ if (!_started)
+ {
+ _executorService = Executors.newSingleThreadExecutor();
+ _serverSocket = new ServerSocket(0);
+ _started = true;
+ _executorService.submit(this);
+ return _serverSocket.getLocalPort();
+ }
+ return -1;
+ }
+
+ synchronized void stop()
+ {
+ if (_started)
+ {
+ _started = false;
+ closeSafely();
+ }
+ }
+
+ public void run()
+ {
+ while (_started)
+ {
+ try
+ {
+ acceptAndClose();
+ }
+ catch (IOException e)
+ {
+ LOGGER.warn("Failed to close client socket", e);
+ stop();
+ }
+ }
+ }
+
+ private void acceptAndClose() throws IOException
+ {
+ final Socket socket = _serverSocket.accept();
+ socket.close();
+ }
+
+ private synchronized void closeSafely()
+ {
+ try
+ {
+ _serverSocket.close();
+ }
+ catch (IOException e)
+ {
+ LOGGER.warn("Failed to close server socket", e);
+ }
+ finally
+ {
+ _executorService.shutdown();
+ }
+ }
+ }
+
}