You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2009/02/12 09:33:17 UTC

svn commit: r743655 - in /servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine: AbstractClusterEndpointTest.java AutoFailTestSupport.java

Author: gnodet
Date: Thu Feb 12 08:33:17 2009
New Revision: 743655

URL: http://svn.apache.org/viewvc?rev=743655&view=rev
Log:
Ensure tests won't hang forever

Added:
    servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AutoFailTestSupport.java
Modified:
    servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AbstractClusterEndpointTest.java

Modified: servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AbstractClusterEndpointTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AbstractClusterEndpointTest.java?rev=743655&r1=743654&r2=743655&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AbstractClusterEndpointTest.java (original)
+++ servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AbstractClusterEndpointTest.java Thu Feb 12 08:33:17 2009
@@ -52,7 +52,7 @@
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.beans.factory.DisposableBean;
 
-public abstract class AbstractClusterEndpointTest extends TestCase {
+public abstract class AbstractClusterEndpointTest extends AutoFailTestSupport {
 
     public static final String PROXY_ENDPOINT_NAME = "proxy";
     public static final String RECEIVER_ENDPOINT_NAME = "receiver";
@@ -67,6 +67,7 @@
 
     @Override
     protected void setUp() throws Exception {
+        super.setUp();
         ExchangeImpl.getConverter();
         this.executor = createTaskExecutor();
         this.transactionManager = new GeronimoPlatformTransactionManager();
@@ -99,6 +100,7 @@
         if (broker != null) {
             broker.stop();
         }
+        super.tearDown();
     }
 
     protected TaskExecutor createTaskExecutor() {

Added: servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AutoFailTestSupport.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AutoFailTestSupport.java?rev=743655&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AutoFailTestSupport.java (added)
+++ servicemix/smx4/nmr/trunk/jbi/cluster/engine/src/test/java/org/apache/servicemix/jbi/cluster/engine/AutoFailTestSupport.java Thu Feb 12 08:33:17 2009
@@ -0,0 +1,141 @@
+/**
+ * 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.servicemix.jbi.cluster.engine;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import junit.framework.TestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Enforces a test case to run for only an allotted time to prevent them from
+ * hanging and breaking the whole testing.
+ *
+ * @version $Revision: 1.0 $
+ */
+
+public abstract class AutoFailTestSupport extends TestCase {
+    public static final int EXIT_SUCCESS = 0;
+    public static final int EXIT_ERROR = 1;
+    private static final Log LOG = LogFactory.getLog(AutoFailTestSupport.class);
+
+    private long maxTestTime = 5 * 60 * 1000; // 5 mins by default
+    private Thread autoFailThread;
+
+    private boolean verbose = true;
+    private boolean useAutoFail; // Disable auto fail by default
+    private AtomicBoolean isTestSuccess;
+
+    protected void setUp() throws Exception {
+        // Runs the auto fail thread before performing any setup
+        if (isAutoFail()) {
+            startAutoFailThread();
+        }
+        super.setUp();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+
+        // Stops the auto fail thread only after performing any clean up
+        stopAutoFailThread();
+    }
+
+    /**
+     * Manually start the auto fail thread. To start it automatically, just set
+     * the auto fail to true before calling any setup methods. As a rule, this
+     * method is used only when you are not sure, if the setUp and tearDown
+     * method is propagated correctly.
+     */
+    public void startAutoFailThread() {
+        setAutoFail(true);
+        isTestSuccess = new AtomicBoolean(false);
+        autoFailThread = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    // Wait for test to finish succesfully
+                    Thread.sleep(getMaxTestTime());
+                } catch (InterruptedException e) {
+                    // This usually means the test was successful
+                } finally {
+                    // Check if the test was able to tear down succesfully,
+                    // which usually means, it has finished its run.
+                    if (!isTestSuccess.get()) {
+                        LOG.error("Test case has exceeded the maximum allotted time to run of: " + getMaxTestTime() + " ms.");
+                        LOG.fatal("Test case has exceeded the maximum allotted time to run of: " + getMaxTestTime() + " ms.");
+                        System.exit(EXIT_ERROR);
+                    }
+                }
+            }
+        }, "AutoFailThread");
+
+        if (verbose) {
+            LOG.info("Starting auto fail thread...");
+        }
+
+        LOG.info("Starting auto fail thread...");
+        autoFailThread.start();
+    }
+
+    /**
+     * Manually stops the auto fail thread. As a rule, this method is used only
+     * when you are not sure, if the setUp and tearDown method is propagated
+     * correctly.
+     */
+    public void stopAutoFailThread() {
+        if (isAutoFail() && autoFailThread != null && autoFailThread.isAlive()) {
+            isTestSuccess.set(true);
+
+            if (verbose) {
+                LOG.info("Stopping auto fail thread...");
+            }
+
+            LOG.info("Stopping auto fail thread...");
+            autoFailThread.interrupt();
+        }
+    }
+
+    /**
+     * Sets the auto fail value. As a rule, this should be used only before any
+     * setup methods is called to automatically enable the auto fail thread in
+     * the setup method of the test case.
+     *
+     * @param val
+     */
+    public void setAutoFail(boolean val) {
+        this.useAutoFail = val;
+    }
+
+    public boolean isAutoFail() {
+        return this.useAutoFail;
+    }
+
+    /**
+     * The assigned value will only be reflected when the auto fail thread has
+     * started its run. Value is in milliseconds.
+     *
+     * @param val
+     */
+    public void setMaxTestTime(long val) {
+        this.maxTestTime = val;
+    }
+
+    public long getMaxTestTime() {
+        return this.maxTestTime;
+    }
+}