NO-JIRA: [System Tests] Restart CPP broker on same listening port and reuse working...
authorKeith Wall <kwall@apache.org>
Wed, 16 May 2018 22:18:40 +0000 (23:18 +0100)
committerKeith Wall <kwall@apache.org>
Thu, 17 May 2018 07:38:23 +0000 (08:38 +0100)
Also allow a modulesDir to be passed to the cppd executable in order to enable built modules (which may include the persistent store)

README.txt
systests/pom.xml
systests/src/main/java/org/apache/qpid/systest/core/AbstractSpawnQpidBrokerAdmin.java
systests/src/main/java/org/apache/qpid/systest/core/LogConsumer.java [new file with mode: 0644]
systests/src/main/java/org/apache/qpid/systest/core/brokerj/SpawnQpidBrokerAdmin.java
systests/src/main/java/org/apache/qpid/systest/core/cpp/SpawnQpidBrokerAdmin.java
systests/src/test/java/org/apache/qpid/systest/connection/FailoverBehaviourTest.java

index 4145e54..c1a077f 100644 (file)
@@ -36,7 +36,7 @@ Execute system tests against cpp broker available on PATH
 
 Execute system tests against cpp broker by providing path to broker executable explicitly
 
-  mvn verify -Pcpp -Dqpid.systest.broker.executable=/home/alex/qpid/qpidd
+  mvn verify -Pcpp -Dqpid.systest.broker.executable=/home/alex/qpid/qpidd -Dqpid.systest.broker.moduleDir=/home/alex/qpid/
 
 Execute the unit tests and then produce the code coverage report
 
index 3376828..fd48b94 100644 (file)
 
             <properties>
                 <qpid.systest.broker.executable>qpidd</qpid.systest.broker.executable>
+                <qpid.systest.broker.moduleDir></qpid.systest.broker.moduleDir>
                 <qpid.systest.broker_admin>org.apache.qpid.systest.core.cpp.SpawnQpidBrokerAdmin</qpid.systest.broker_admin>
             </properties>
             <build>
                         <artifactId>maven-surefire-plugin</artifactId>
                         <configuration>
                             <systemPropertyVariables>
+                                <qpid.systest.broker.moduleDir>${qpid.systest.broker.moduleDir}</qpid.systest.broker.moduleDir>
                                 <qpid.systest.broker.executable>${qpid.systest.broker.executable}</qpid.systest.broker.executable>
                                 <qpid.systest.broker_admin>${qpid.systest.broker_admin}</qpid.systest.broker_admin>
                                 <qpid.systest.broker.clean.between.tests>${qpid.systest.broker.clean.between.tests}</qpid.systest.broker.clean.between.tests>
index 103cbc7..45b5024 100644 (file)
@@ -72,10 +72,9 @@ public abstract class AbstractSpawnQpidBrokerAdmin implements BrokerAdmin
     private volatile List<ListeningPort> _ports;
     private volatile Process _process;
     private volatile Integer _pid;
-    private volatile String _currentWorkDirectory;
     private ExecutorService _executorService;
-    private Class _currentTestClass;
-    private Method _currentTestMethod;
+    protected Class _currentTestClass;
+    protected Method _currentTestMethod;
 
     @Override
     public void beforeTestClass(final Class testClass)
@@ -186,31 +185,31 @@ public abstract class AbstractSpawnQpidBrokerAdmin implements BrokerAdmin
 
     protected abstract ProcessBuilder createBrokerProcessBuilder(final String workDirectory, final Class testClass) throws IOException;
 
+    public LogConsumer getLogConsumer()
+    {
+        return new LogConsumer()
+        {
+            @Override
+            public void accept(final String line)
+            {
+            }
+        };
+    }
 
     protected void runBroker(final Class testClass,
                              final Method method,
                              final String readyLogPattern,
                              final String stopLogPattern,
                              final String portListeningLogPattern,
-                             final String processPIDLogPattern) throws IOException
+                             final String processPIDLogPattern, String currentWorkDirectory) throws IOException
     {
-        String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
-        String test = testClass.getSimpleName();
-        if (method != null)
-        {
-            test += "-" + method.getName();
-        }
-        _currentWorkDirectory =
-                Files.createTempDirectory(String.format("qpid-work-%s-%s-", timestamp, test))
-                     .toString();
-
-        LOGGER.debug("Spawning broker working folder: {}", _currentWorkDirectory);
+        LOGGER.debug("Spawning broker working folder: {}", currentWorkDirectory);
 
         int startUpTime = Integer.getInteger(SYSTEST_PROPERTY_SPAWN_BROKER_STARTUP_TIME, 30000);
 
         LOGGER.debug("Spawning broker permitted start-up time: {}", startUpTime);
 
-        ProcessBuilder processBuilder = createBrokerProcessBuilder(_currentWorkDirectory, testClass);
+        ProcessBuilder processBuilder = createBrokerProcessBuilder(currentWorkDirectory, testClass);
         processBuilder.redirectErrorStream(true);
 
         Map<String, String> processEnvironment = processBuilder.environment();
@@ -223,6 +222,7 @@ public abstract class AbstractSpawnQpidBrokerAdmin implements BrokerAdmin
         _process = processBuilder.start();
 
         BrokerSystemOutputHandler brokerSystemOutputHandler = new BrokerSystemOutputHandler(_process.getInputStream(),
+                                                                                            getLogConsumer(),
                                                                                             readyLogPattern,
                                                                                             stopLogPattern,
                                                                                             processPIDLogPattern,
@@ -312,6 +312,25 @@ public abstract class AbstractSpawnQpidBrokerAdmin implements BrokerAdmin
         }
     }
 
+    protected String getWorkingDirectory(final Class testClass, final Method method)
+    {
+        try
+        {
+            String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
+            String test = testClass.getSimpleName();
+            if (method != null)
+            {
+                test += "-" + method.getName();
+            }
+            return Files.createTempDirectory(String.format("qpid-work-%s-%s-", timestamp, test))
+                         .toString();
+        }
+        catch (IOException e)
+        {
+            throw new IllegalStateException(e);
+        }
+    }
+
     protected void shutdownBroker()
     {
         try
@@ -342,13 +361,14 @@ public abstract class AbstractSpawnQpidBrokerAdmin implements BrokerAdmin
             }
             _pid = null;
             _process = null;
-            if (_currentWorkDirectory != null && Boolean.getBoolean(SYSTEST_PROPERTY_BROKER_CLEAN_BETWEEN_TESTS))
-            {
-                if (FileUtils.delete(new File(_currentWorkDirectory), true))
-                {
-                    _currentWorkDirectory = null;
-                }
-            }
+        }
+    }
+
+    protected void cleanWorkDirectory(final String currentWorkDirectory)
+    {
+        if (currentWorkDirectory != null && Boolean.getBoolean(SYSTEST_PROPERTY_BROKER_CLEAN_BETWEEN_TESTS))
+        {
+            FileUtils.delete(new File(currentWorkDirectory), true);
         }
     }
 
@@ -493,11 +513,13 @@ public abstract class AbstractSpawnQpidBrokerAdmin implements BrokerAdmin
         private final Pattern _pidPattern;
         private final Pattern _amqpPortPattern;
         private final CountDownLatch _readyLatch;
+        private final LogConsumer _logConsumer;
 
         private volatile boolean _seenReady;
         private volatile int _pid;
 
         private BrokerSystemOutputHandler(InputStream in,
+                                          LogConsumer logConsumer,
                                           String readyRegExp,
                                           String stoppedRedExp,
                                           String pidRegExp,
@@ -505,6 +527,7 @@ public abstract class AbstractSpawnQpidBrokerAdmin implements BrokerAdmin
                                           CountDownLatch readyLatch,
                                           String loggerName)
         {
+            _logConsumer = logConsumer;
             _amqpPorts = new ArrayList<>();
             _seenReady = false;
             _in = new BufferedReader(new InputStreamReader(in));
@@ -524,6 +547,7 @@ public abstract class AbstractSpawnQpidBrokerAdmin implements BrokerAdmin
                 String line;
                 while ((line = _in.readLine()) != null)
                 {
+                    _logConsumer.accept(line);
                     _out.info(line);
 
                     checkPortListeningLog(line, _amqpPortPattern, _amqpPorts);
diff --git a/systests/src/main/java/org/apache/qpid/systest/core/LogConsumer.java b/systests/src/main/java/org/apache/qpid/systest/core/LogConsumer.java
new file mode 100644 (file)
index 0000000..43a7b2c
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ *
+ */
+
+package org.apache.qpid.systest.core;
+
+public interface LogConsumer
+{
+    void accept(final String line);
+}
index f635a5d..b3c967f 100644 (file)
@@ -77,6 +77,7 @@ public class SpawnQpidBrokerAdmin extends AbstractSpawnQpidBrokerAdmin
 
     private volatile boolean _isPersistentStore;
     private volatile String _virtualHostNodeName;
+    private volatile String _workingDirectory;
 
     @Override
     public boolean supportsPersistence()
@@ -138,7 +139,8 @@ public class SpawnQpidBrokerAdmin extends AbstractSpawnQpidBrokerAdmin
             String amqpListening = System.getProperty(SYSTEST_PROPERTY_BROKER_LISTENING_LOG,
                                                       "BRK-1002 : Starting : Listening on (\\w*) port ([0-9]+)");
             String process = System.getProperty(SYSTEST_PROPERTY_BROKER_PROCESS_LOG, "BRK-1017 : Process : PID : ([0-9]+)");
-            runBroker(testClass, null, ready, stopped, amqpListening, process);
+            _workingDirectory = getWorkingDirectory(testClass, null);
+            runBroker(testClass, null, ready, stopped, amqpListening, process, _workingDirectory);
         }
         catch (IOException e)
         {
@@ -150,6 +152,8 @@ public class SpawnQpidBrokerAdmin extends AbstractSpawnQpidBrokerAdmin
     protected void cleanUp(final Class testClass)
     {
         shutdownBroker();
+        cleanWorkDirectory(_workingDirectory);
+        _workingDirectory = null;
     }
 
     @Override
index 3c48c2c..5cc27d2 100644 (file)
 
 package org.apache.qpid.systest.core.cpp;
 
+import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 
 import org.apache.qpid.systest.core.AbstractSpawnQpidBrokerAdmin;
 import org.apache.qpid.systest.core.BrokerAdminException;
+import org.apache.qpid.systest.core.LogConsumer;
 
 public class SpawnQpidBrokerAdmin extends AbstractSpawnQpidBrokerAdmin
 {
     private static final String SYSTEST_PROPERTY_BROKER_EXECUTABLE = "qpid.systest.broker.executable";
+    private static final String SYSTEST_PROPERTY_BROKER_MODULE_DIR = "qpid.systest.broker.moduleDir";
+    private static final String SYSTEST_PROPERTY_BROKER_STORE_INITIALIZED = "qpid.systest.broker.storeInitialized";
     private static final String BROKER_OUTPUT_LOG_RUNNING = "Broker \\(pid=([0-9]+)\\) running";
     private static final String BROKER_OUTPUT_LOG_SHUT_DOWN = "Broker \\(pid=([0-9]+)\\) shut-down";
+    private static final String BROKER_OUTPUT_STORE_INITIALIZED = "Store module initialized";
     private static final String BROKER_OUTPUT_LOG_LISTENING = "Listening on (TCP/TCP6) port ([0-9]+)";
+    private final String _storeInitalised;
+    private final String _moduleDir;
+    private final String _ready;
+    private final String _stopped;
+    private final String _amqpListening;
+    private final String _process;
+    private volatile String _workingDirectory;
+    private boolean _supportsPersistence = false;
+    private int _previousPort = 0;
+
+    public SpawnQpidBrokerAdmin()
+    {
+        _storeInitalised = System.getProperty(SYSTEST_PROPERTY_BROKER_STORE_INITIALIZED, BROKER_OUTPUT_STORE_INITIALIZED);
+        _moduleDir = System.getProperty(SYSTEST_PROPERTY_BROKER_MODULE_DIR);
+        _ready = System.getProperty(SYSTEST_PROPERTY_BROKER_READY_LOG, BROKER_OUTPUT_LOG_RUNNING);
+        _stopped = System.getProperty(SYSTEST_PROPERTY_BROKER_STOPPED_LOG, BROKER_OUTPUT_LOG_SHUT_DOWN);
+        _amqpListening = System.getProperty(SYSTEST_PROPERTY_BROKER_LISTENING_LOG,
+                                                  BROKER_OUTPUT_LOG_LISTENING);
+        _process = System.getProperty(SYSTEST_PROPERTY_BROKER_PROCESS_LOG, BROKER_OUTPUT_LOG_RUNNING);
+    }
+
 
     @Override
     public boolean supportsPersistence()
     {
-        return false;
+        return _supportsPersistence;
     }
 
     @Override
@@ -75,16 +104,35 @@ public class SpawnQpidBrokerAdmin extends AbstractSpawnQpidBrokerAdmin
     }
 
     @Override
+    public LogConsumer getLogConsumer()
+    {
+        final LogConsumer superConsumer = super.getLogConsumer();
+        return new LogConsumer()
+        {
+            @Override
+            public void accept(final String line)
+            {
+                superConsumer.accept(line);
+                if (line != null && line.contains(_storeInitalised))
+                {
+                    _supportsPersistence = true;
+                }
+            }
+        };
+    }
+
+    @Override
     protected void begin(final Class testClass, final Method method)
     {
+        _workingDirectory = getWorkingDirectory(testClass, method);
+        doRunBroker(testClass, method);
+    }
+
+    private void doRunBroker(final Class testClass, final Method method)
+    {
         try
         {
-            String ready = System.getProperty(SYSTEST_PROPERTY_BROKER_READY_LOG, BROKER_OUTPUT_LOG_RUNNING);
-            String stopped = System.getProperty(SYSTEST_PROPERTY_BROKER_STOPPED_LOG, BROKER_OUTPUT_LOG_SHUT_DOWN);
-            String amqpListening = System.getProperty(SYSTEST_PROPERTY_BROKER_LISTENING_LOG,
-                                                      BROKER_OUTPUT_LOG_LISTENING);
-            String process = System.getProperty(SYSTEST_PROPERTY_BROKER_PROCESS_LOG, BROKER_OUTPUT_LOG_RUNNING);
-            runBroker(testClass, method, ready, stopped, amqpListening, process);
+            runBroker(testClass, method, _ready, _stopped, _amqpListening, _process, _workingDirectory);
         }
         catch (IOException e)
         {
@@ -93,26 +141,59 @@ public class SpawnQpidBrokerAdmin extends AbstractSpawnQpidBrokerAdmin
     }
 
     @Override
+    public void restart()
+    {
+        try
+        {
+            _previousPort = getBrokerAddress(PortType.AMQP).getPort();
+        }
+        catch (IllegalArgumentException e)
+        {
+            _previousPort = 0;
+        }
+
+        try
+        {
+            shutdownBroker();
+            doRunBroker(_currentTestClass, _currentTestMethod);
+        }
+        finally
+        {
+            _previousPort = 0;
+        }
+    }
+
+    @Override
     protected void end(final Class testClass, final Method method)
     {
         shutdownBroker();
+        cleanWorkDirectory(_workingDirectory);
+        _workingDirectory = null;
     }
 
     @Override
-    protected ProcessBuilder createBrokerProcessBuilder(final String workDirectory, final Class testClass)
-            throws IOException
+    protected ProcessBuilder createBrokerProcessBuilder(final String workDirectory,
+                                                        final Class testClass)
     {
-        String[] cmd = new String[]{
+        List<String> cmd = new ArrayList<>(Arrays.asList(
                 System.getProperty(SYSTEST_PROPERTY_BROKER_EXECUTABLE, "qpidd"),
                 "-p",
-                "0",
+                String.format("%d", _previousPort),
                 "--data-dir",
                 escapePath(workDirectory),
                 "-t",
                 "--auth",
-                "no",
-                "--no-module-dir"
-        };
+                "no"));
+
+        if (_moduleDir != null && _moduleDir.length() > 0  && new File(_moduleDir).isDirectory())
+        {
+            cmd.add("--module-dir");
+            cmd.add(escapePath(_moduleDir));
+        }
+        else
+        {
+            cmd.add("--no-module-dir");
+        }
 
         return new ProcessBuilder(cmd);
     }
index 6520fdb..6654a4a 100644 (file)
@@ -28,6 +28,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeThat;
+import static org.junit.Assume.assumeTrue;
 
 import java.text.MessageFormat;
 import java.util.Collections;
@@ -306,6 +307,7 @@ public class FailoverBehaviourTest extends JmsTestBase implements ExceptionListe
     @Test
     public void testRollbackAfterReceivingAfterFailover() throws Exception
     {
+        assumeTrue(getBrokerAdmin().supportsPersistence());
         init(Session.SESSION_TRANSACTED, true);
 
         produceMessages();