[jboss-cvs] JBoss Messaging SVN: r1815 - trunk/tests/src/org/jboss/test/messaging/util

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Dec 18 17:59:53 EST 2006


Author: clebert.suconic at jboss.com
Date: 2006-12-18 17:59:52 -0500 (Mon, 18 Dec 2006)
New Revision: 1815

Added:
   trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicReadWriteTest.java
   trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicValveTest.java
Modified:
   trunk/tests/src/org/jboss/test/messaging/util/VeryBasicValveTest.java
Log:
adding experimental tests

Added: trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicReadWriteTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicReadWriteTest.java	2006-12-18 22:40:14 UTC (rev 1814)
+++ trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicReadWriteTest.java	2006-12-18 22:59:52 UTC (rev 1815)
@@ -0,0 +1,228 @@
+/*
+   * 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.util;
+
+import junit.framework.TestCase;
+import org.jboss.logging.Logger;
+import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock;
+import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock;
+import EDU.oswego.cs.dl.util.concurrent.ReaderPreferenceReadWriteLock;
+
+/**
+ *
+ * ValidateValveLogicReadWriteTest and ValidateValveLogicValveTest were written
+ * to validate lock mechanism to be used on ValveAspect.
+ *
+ * ValidateValveLogicTestValveTest uses org.jboss.messaging.util.Valve which avoid locking on reading threads
+ * ValidateValveLogicReadWriteTest uses oswego.concurrent.ReadWriteLock which adds synchronization into reading threads also
+ *
+ *
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
+ * @version <tt>$Revision:$</tt>
+ *          <p/>
+ *          $Id:$
+ */
+public class ValidateValveLogicReadWriteTest extends TestCase
+{
+   private static Logger log = Logger.getLogger(ValidateValveLogicReadWriteTest.class);
+
+   boolean useCounterA;
+
+   public synchronized boolean isUseCounterA()
+   {
+      return useCounterA;
+   }
+
+   public synchronized void setUseCounterA(boolean useCounterA)
+   {
+      this.useCounterA = useCounterA;
+   }
+
+
+   boolean keepRunning=true;
+   
+   long counterA = 0;
+   long counterB = 0;
+
+   public synchronized void addCounterA()
+   {
+      counterA++;
+   }
+
+   public synchronized void addCounterB()
+   {
+      counterB++;
+   }
+
+
+   public synchronized long getCounterA()
+   {
+      return counterA;
+   }
+
+   public synchronized long getCounterB()
+   {
+      return counterB;
+   }
+
+   private ReadWriteLock lockValve ;
+   boolean started=false;
+   private Object startSemaphore = new Object();
+
+
+   protected void setUp() throws Exception
+   {
+      super.setUp();    //To change body of overridden methods use File | Settings | File Templates.
+      counterA=0;
+      counterB=0;
+      keepRunning=true;
+      useCounterA=true;
+      
+   }
+
+   public void testValveWithReentrantWriterPreferenceReadWriteLock() throws Exception
+   {
+      log.info("++testValveWithReentrantWriterPreferenceReadWriteLock");
+      lockValve = new ReentrantWriterPreferenceReadWriteLock();
+      internaltestValveLogic();
+   }
+
+   public void testValveWithReaderPreferenceReadWriteLock() throws Exception
+   {
+      log.info("++testValveWithReaderPreferenceReadWriteLock");
+      lockValve = new ReaderPreferenceReadWriteLock();
+      internaltestValveLogic();
+   }
+
+   public void internaltestValveLogic() throws Exception
+   {
+      ThreadRead readThreads [] = new ThreadRead[1000];
+      for (int i=0; i<readThreads.length; i++)
+      {
+         readThreads[i] = new ThreadRead(i);
+      }
+
+      for (int i=0; i<readThreads.length; i++)
+      {
+         readThreads[i].start();
+      }
+
+      synchronized (startSemaphore)
+      {
+         started=true;
+         startSemaphore.notifyAll();
+      }
+
+
+      log.info("Sleeping 10 seconds");
+      Thread.sleep(10000);
+
+      log.info("Acquiring write lock");
+      lockValve.writeLock().acquire();
+      setUseCounterA(false);
+
+      long counterAOriginal = getCounterA();
+      long counterBOriginal = getCounterB();
+
+      log.info ("Waiting 5 seconds");
+      Thread.sleep(5000);
+
+      assertEquals(counterAOriginal, getCounterA());
+      assertEquals(counterBOriginal, getCounterB());
+
+      lockValve.writeLock().release();
+
+
+      log.info("Acquiring read lock");
+      lockValve.readLock().acquire();
+
+      counterBOriginal = getCounterB();
+
+      log.info ("Waiting 5 seconds");
+      Thread.sleep(5000);
+
+      log.info("Threads produced " + (getCounterB() - counterBOriginal));
+      assertEquals(counterAOriginal, getCounterA());
+      assertTrue(getCounterB()>counterBOriginal);
+      lockValve.readLock().release();
+
+      keepRunning = false;
+
+
+      for (int i=0; i<readThreads.length; i++)
+      {
+         readThreads[i].join();
+      }
+
+
+
+
+   }
+
+   // multiple threads opening/closing a thread.
+   // only one should be able to open it
+   public class ThreadRead extends Thread
+   {
+      int threadId;
+      public ThreadRead(int threadId)
+      {
+         this.threadId = threadId;
+      }
+      public void run()
+      {
+         try
+         {
+            //log.info("Starting Thread " + threadId);
+            synchronized (startSemaphore)
+            {
+               if (!started)
+               {
+                  startSemaphore.wait();
+               }
+            }
+
+            while (keepRunning)
+            {
+               lockValve.readLock().acquire();
+               if (isUseCounterA())
+               {
+                  //log.info("Thread " + threadId + " adding A");
+                  addCounterA();
+               }
+               else
+               {
+                  //log.info("Thread " + threadId + " adding B");
+                  addCounterB();
+               }
+               lockValve.readLock().release();
+            }
+
+         }
+         catch (Exception e)
+         {
+            e.printStackTrace();
+         }
+      }
+   }
+
+}

Added: trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicValveTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicValveTest.java	2006-12-18 22:40:14 UTC (rev 1814)
+++ trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicValveTest.java	2006-12-18 22:59:52 UTC (rev 1815)
@@ -0,0 +1,202 @@
+/*
+   * 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.util;
+
+import junit.framework.TestCase;
+import org.jboss.logging.Logger;
+import org.jboss.messaging.util.Valve;
+
+/**
+ *
+ * There is a relevant comment into <@link ValidateValveLogicReadWriteTest> 
+ * @see ValidateValveLogicReadWriteTest
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
+ * @version <tt>$Revision:$</tt>
+ *          <p/>
+ *          $Id:$
+ */
+public class ValidateValveLogicValveTest extends TestCase
+{
+   private static Logger log = Logger.getLogger(ValidateValveLogicValveTest.class);
+
+   boolean useCounterA;
+
+   public synchronized boolean isUseCounterA()
+   {
+      return useCounterA;
+   }
+
+   public synchronized void setUseCounterA(boolean useCounterA)
+   {
+      this.useCounterA = useCounterA;
+   }
+
+
+   boolean keepRunning=true;
+
+   long counterA = 0;
+   long counterB = 0;
+
+   public synchronized void addCounterA()
+   {
+      counterA++;
+   }
+
+   public synchronized void addCounterB()
+   {
+      counterB++;
+   }
+
+
+   public synchronized long getCounterA()
+   {
+      return counterA;
+   }
+
+   public synchronized long getCounterB()
+   {
+      return counterB;
+   }
+
+   Valve valve = new Valve();
+
+   boolean started=false;
+   private Object startSemaphore = new Object();
+
+
+   public void testValveLogic() throws Exception
+   {
+      ValidateValveLogicValveTest.ThreadRead readThreads [] = new ValidateValveLogicValveTest.ThreadRead[1000];
+      for (int i=0; i<readThreads.length; i++)
+      {
+         readThreads[i] = new ValidateValveLogicValveTest.ThreadRead(i);
+      }
+
+      for (int i=0; i<readThreads.length; i++)
+      {
+         readThreads[i].start();
+      }
+
+      synchronized (startSemaphore)
+      {
+         started=true;
+         startSemaphore.notifyAll();
+      }
+
+
+      log.info("Sleeping 10 seconds");
+      Thread.sleep(10000);
+
+      log.info("Acquiring write lock");
+      valve.open();
+
+      // Time to wait current calls to finish
+      // As valve doesn't need synchronization on running threads,
+      // this sleep is necessary to wait everybody to be on the waiting condition.
+      // And this is meant to be this way. We don't want to stop threads ot calls that are already working,
+      // We want to stop new ones only 
+      Thread.sleep(1000);
+
+      setUseCounterA(false);
+
+      long counterAOriginal = getCounterA();
+      long counterBOriginal = getCounterB();
+
+
+      log.info ("Waiting 5 seconds");
+      Thread.sleep(5000);
+
+      assertEquals(counterAOriginal, getCounterA());
+      assertEquals(counterBOriginal, getCounterB());
+
+      valve.reset();
+
+      valve.isOpened(true);
+
+      log.info("Acquiring read lock");
+
+      counterBOriginal = getCounterB();
+
+      log.info ("Waiting 5 seconds");
+      Thread.sleep(5000);
+
+      log.info ("Threads produced " + (getCounterB() - counterBOriginal));
+      assertEquals(counterAOriginal, getCounterA());
+      assertTrue(getCounterB()>counterBOriginal);
+
+      keepRunning = false;
+
+
+      for (int i=0; i<readThreads.length; i++)
+      {
+         readThreads[i].join();
+      }
+
+   }
+
+   // multiple threads opening/closing a thread.
+   // only one should be able to open it
+   public class ThreadRead extends Thread
+   {
+      int threadId;
+      public ThreadRead(int threadId)
+      {
+         this.threadId = threadId;
+      }
+      public void run()
+      {
+         try
+         {
+            log.info("Starting Thread " + threadId);
+            synchronized (startSemaphore)
+            {
+               if (!started)
+               {
+                  startSemaphore.wait();
+               }
+            }
+
+            while (keepRunning)
+            {
+               valve.isOpened(true);
+               if (isUseCounterA())
+               {
+                  //log.info("Thread " + threadId + " adding A");
+                  addCounterA();
+               }
+               else
+               {
+                  //log.info("Thread " + threadId + " adding B");
+                  addCounterB();
+               }
+            }
+
+         }
+         catch (Exception e)
+         {
+            e.printStackTrace();
+         }
+      }
+   }
+
+}

Modified: trunk/tests/src/org/jboss/test/messaging/util/VeryBasicValveTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/util/VeryBasicValveTest.java	2006-12-18 22:40:14 UTC (rev 1814)
+++ trunk/tests/src/org/jboss/test/messaging/util/VeryBasicValveTest.java	2006-12-18 22:59:52 UTC (rev 1815)
@@ -40,6 +40,7 @@
 
    static int counter = 0;
    static int counterWait = 0;
+   static int countIsOpen = 0;
 
    static boolean started=false;
    static Object startSemaphore = new Object();
@@ -66,6 +67,12 @@
                   startSemaphore.wait();
                }
             }
+
+            if (valve.isOpened(true))
+            {
+               countIsOpen++;
+            }
+
             //log.info("Thread " + threadId + "Opening valve");
             if (!valve.open())
             {
@@ -133,6 +140,10 @@
          thread[i].join();
       }
 
+      log.info("CountIsOpened=" + countIsOpen);
+      log.info("CounterWait=" + counterWait);
+      log.info("counter=" + counter);
+
       assertEquals(1, counter);
       assertEquals(thread.length-1, counterWait);
 




More information about the jboss-cvs-commits mailing list