[jboss-cvs] JBoss Messaging SVN: r1936 - in trunk: tests/src/org/jboss/test/messaging/jms/clustering and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jan 9 19:14:50 EST 2007


Author: clebert.suconic at jboss.com
Date: 2007-01-09 19:14:47 -0500 (Tue, 09 Jan 2007)
New Revision: 1936

Added:
   trunk/tests/src/org/jboss/test/messaging/jms/clustering/FailoverValveTest.java
Modified:
   trunk/src/main/org/jboss/jms/client/FailoverValve.java
Log:
Adding a testcase for FailoverValve and adding a sanity check on FailoverValve

Modified: trunk/src/main/org/jboss/jms/client/FailoverValve.java
===================================================================
--- trunk/src/main/org/jboss/jms/client/FailoverValve.java	2007-01-09 23:29:20 UTC (rev 1935)
+++ trunk/src/main/org/jboss/jms/client/FailoverValve.java	2007-01-10 00:14:47 UTC (rev 1936)
@@ -107,16 +107,15 @@
    {
       lock.readLock().release();
 
-      getCounter().counter--;
+      // sanity check
+      if (getCounter().counter-- < 0)
+      {
+         throw new IllegalStateException("leave() was called without a prior enter() call");
+      }
 
       synchronized (this)
       {
          activeLocks--;
-
-         if (activeLocks < 0)
-         {
-            throw new IllegalStateException("leave() was called without a prior enter() call");
-         }
       }
 
       if (trace)
@@ -260,7 +259,7 @@
       writer.println("Close owners");
 
       // Close should never have more than 1 thread owning, but as this is a debug report we will
-      // consider that as a possibility just to show eventual bugs (just in case thie class is ever
+      // consider that as a possibility just to show eventual bugs (just in case this class is ever
       // changed)
       for (Iterator iter = debugCloses.entrySet().iterator(); iter.hasNext();)
       {

Added: trunk/tests/src/org/jboss/test/messaging/jms/clustering/FailoverValveTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/jms/clustering/FailoverValveTest.java	2007-01-09 23:29:20 UTC (rev 1935)
+++ trunk/tests/src/org/jboss/test/messaging/jms/clustering/FailoverValveTest.java	2007-01-10 00:14:47 UTC (rev 1936)
@@ -0,0 +1,174 @@
+/*
+   * JBoss, Home of Professional Open Source
+   * Copyright 2005, JBoss Inc., and individual contributors as indicated
+   * by the @authors tag. See the copyright.txt in the distribution for a
+   * full listing of individual contributors.
+   *
+   * This is free software; you can redistribute it and/or modify it
+   * under the terms of the GNU Lesser General Public License as
+   * published by the Free Software Foundation; either version 2.1 of
+   * the License, or (at your option) any later version.
+   *
+   * This software is distributed in the hope that it will be useful,
+   * but WITHOUT ANY WARRANTY; without even the implied warranty of
+   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+   * Lesser General Public License for more details.
+   *
+   * You should have received a copy of the GNU Lesser General Public
+   * License along with this software; if not, write to the Free
+   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+   */
+
+package org.jboss.test.messaging.jms.clustering;
+
+import org.jboss.jms.client.FailoverValve;
+import org.jboss.logging.Logger;
+import junit.framework.TestCase;
+
+
+/**
+ * Regression Tests for a dead lock condition fixed on FailoverValve.
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
+ * @version <tt>$Revision$</tt>
+ *          <p/>
+ *          $Id$
+ */
+public class FailoverValveTest extends TestCase
+{
+
+
+   // Constants ------------------------------------------------------------------------------------
+   private static final Logger log = Logger.getLogger(FailoverValveTest.class);
+
+   // Attributes -----------------------------------------------------------------------------------
+
+   Object semaphore = new Object();
+   boolean running = false;
+
+   // Static ---------------------------------------------------------------------------------------
+
+   // Constructors ---------------------------------------------------------------------------------
+
+   // Public ---------------------------------------------------------------------------------------
+
+
+   // You can have multiple threads trying to close the valve at the same time.
+   public void testMultipleThreadsClosingValve() throws Exception
+   {
+      FailoverValve valve = new FailoverValve();
+
+      ValveThread threads[] = new ValveThread[100];
+
+      for (int i = 0; i < threads.length; i++)
+      {
+         threads[i] = new ValveThread(valve);
+      }
+
+      for (int i = 0; i < threads.length; i++)
+      {
+         threads[i].start();
+      }
+
+      // time to a line up
+      Thread.sleep(1000);
+
+      synchronized (semaphore)
+      {
+         running = true;
+         semaphore.notifyAll();
+      }
+
+      for (int i = 0; i < threads.length; i++)
+      {
+         threads[i].join();
+         if (threads[i].failed)
+         {
+            fail("One of threads had a failure, look at logs");
+         }
+      }
+
+      assertEquals (0, valve.getActiveLocks());
+   }
+
+   // Validate weird usages that are supposed to throw exceptions
+   public void testValidateExceptions() throws Exception
+   {
+      try
+      {
+         FailoverValve  valve = new FailoverValve ();
+         valve.open();
+         valve.close();
+         valve.close(); // closing without opening should throw an exception
+         fail("Valve.close didn't generate an exception on an extra close, Valve is not safe!");
+      }
+      catch (Throwable e)
+      {
+         //e.printStackTrace();
+      }
+
+      try
+      {
+         FailoverValve valve = new FailoverValve ();
+         valve.enter();
+         valve.leave();
+         valve.leave(); // extra leave call, should throw an exception
+         fail("Valve.close didn't generate an exception, Valve is not safe!");
+      }
+      catch (Throwable e)
+      {
+         //e.printStackTrace();
+      }
+
+   }
+
+   // Package protected ----------------------------------------------------------------------------
+
+   // Protected ------------------------------------------------------------------------------------
+
+   // Private --------------------------------------------------------------------------------------
+
+   // Inner classes --------------------------------------------------------------------------------
+
+   class ValveThread extends Thread
+   {
+      FailoverValve valve;
+
+      boolean failed;
+
+      public ValveThread(FailoverValve  valve)
+      {
+         this.valve = valve;
+      }
+
+      public void run()
+      {
+         try
+         {
+            synchronized (semaphore)
+            {
+               if (!running)
+               {
+                  semaphore.wait();
+               }
+            }
+            for (int i = 0; i < 10; i++)
+            {
+               valve.enter();
+            }
+            valve.close();
+            valve.open();
+            for (int i = 0; i < 10; i++)
+            {
+               valve.leave();
+            }
+         }
+         catch (Exception e)
+         {
+            e.printStackTrace();
+            failed = true;
+         }
+      }
+   }
+
+}


Property changes on: trunk/tests/src/org/jboss/test/messaging/jms/clustering/FailoverValveTest.java
___________________________________________________________________
Name: svn:keywords
   + Id LastChangedDate Author Revision




More information about the jboss-cvs-commits mailing list