qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oru...@apache.org
Subject [qpid-jms-amqp-0-x] branch master updated: NO-JIRA: Use dummy server to accept tcp connections from failover
Date Mon, 01 Jun 2020 20:22:32 GMT
This is an automated email from the ASF dual-hosted git repository.

orudyy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-jms-amqp-0-x.git


The following commit(s) were added to refs/heads/master by this push:
     new 68c8e86  NO-JIRA: Use dummy server to accept tcp connections from failover
68c8e86 is described below

commit 68c8e86c3f00f3c0b72287e8b7c2ec607faedbc7
Author: Alex Rudyy <orudyy@apache.org>
AuthorDate: Mon Jun 1 21:13:58 2020 +0100

    NO-JIRA: Use dummy server to accept tcp connections from failover
---
 .../systest/connection/FailoverMethodTest.java     | 87 +++++++++++++++++++++-
 1 file changed, 84 insertions(+), 3 deletions(-)

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.hamcrest.CoreMatchers.not;
 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.AMQConnection;
 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 @@ public class FailoverMethodTest extends JmsTestBase implements ExceptionListener
     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 @@ public class FailoverMethodTest extends JmsTestBase implements ExceptionListener
     {
         _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();
+            }
+        }
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message