[jboss-cvs] JBoss Messaging SVN: r1865 - in trunk: src/main/org/jboss/messaging/util tests/src/org/jboss/test/messaging/util
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Dec 27 23:20:44 EST 2006
Author: clebert.suconic at jboss.com
Date: 2006-12-27 23:20:39 -0500 (Wed, 27 Dec 2006)
New Revision: 1865
Removed:
trunk/src/main/org/jboss/messaging/util/Valve.java
trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicValveTest.java
trunk/tests/src/org/jboss/test/messaging/util/VeryBasicValveTest.java
Modified:
trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicReadWriteTest.java
Log:
Removing non used classes and tests
Deleted: trunk/src/main/org/jboss/messaging/util/Valve.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/Valve.java 2006-12-28 04:02:30 UTC (rev 1864)
+++ trunk/src/main/org/jboss/messaging/util/Valve.java 2006-12-28 04:20:39 UTC (rev 1865)
@@ -1,169 +0,0 @@
-/*
- * 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.messaging.util;
-
-import org.jboss.logging.Logger;
-
-
-/**
- * This class is used to guarantee only one thread will be performing a given function, and if any other
- * thread tries to execute the same functionality it will just ignored
- * <pre>
- Valve valve = new Valve();
- if (valve.open())
- {
- try
- {
- doSomething();
- }
- finally
- {
- valve.close();
- }
- }
- else
- {
- System.out.println("Nothing to be done
- }
- </pre>
-
- <p>Notice that you can call close only once, so only call close if you were able to open the valve.</p>
- <p>After its usage if you decide to reset you should just create a new Valve in a safe synchronized block, so
- if any other thread still using the variable you do it in a safe way </p>
-
- * @see org.jboss.test.messaging.util.VeryBasicValveTest
- * @author <a href="mailto:clebert.suconic at jboss.com">Clebert Suconic</a>
- * */
-public class Valve
-{
- private static final Logger log = Logger.getLogger(Valve.class);
- private boolean trace = log.isTraceEnabled();
-
- boolean opened;
- boolean closed;
-
- Thread threadOwner;
-
- int refereceCountOpen = 0;
-
-
- public synchronized boolean isOpened()
- {
- return opened;
- }
-
- /**
- * If the Valve is opened, will wait until the valve is closed
- */
- public synchronized boolean isOpened(boolean wait) throws Exception
- {
- if (wait && opened)
- {
- if (!closed && threadOwner != Thread.currentThread())
- {
- if (trace) log.trace("threadOwner= " + threadOwner + " and currentThread=" + Thread.currentThread());
- if (trace) log.trace("Waiting valve to be closed");
- this.wait();
- if (trace) log.trace("Valve was closed");
- } else
- {
- if (trace) log.trace("This is ThreadOwner, so Valve won't wait");
- }
- return opened;
- } else
- {
- return false;
- }
-
- }
-
- public boolean open() throws Exception
- {
- return open(true);
- }
-
- public synchronized boolean open(boolean wait) throws Exception
- {
- if (threadOwner == Thread.currentThread())
- {
- if (trace) log.trace("Valve was opened again by thread owner");
- refereceCountOpen++;
- return true;
- }
- // already opened? then needs to wait to be closed
- if (opened)
- {
- if (trace) log.trace("Valve being opened and time.wait");
- // if not closed yet, will wait to be closed
- if (!closed)
- {
- if (wait)
- {
- this.wait();
- }
- }
- return false;
- } else
- {
- if (trace) log.trace("Valve being opened and this thread is the owner for this lock");
- refereceCountOpen++;
- opened = true;
- threadOwner = Thread.currentThread();
- return true;
- }
- }
-
- public synchronized void close()
- {
- if (!opened)
- {
- throw new IllegalStateException("Open must be called first");
- }
- if (closed)
- {
- log.warn("Valve was already closed", new Exception());
- }
- refereceCountOpen--;
- if (refereceCountOpen == 0)
- {
- if (trace) log.trace("Closing Valve");
- closed = true;
- notifyAll();
- } else
- {
- if (trace) log.trace("Valve.close called but there referenceCountOpen=" + refereceCountOpen);
- }
- }
-
- public synchronized void reset()
- {
- if (opened && !closed)
- {
- close();
- }
- opened = false;
- closed = false;
- threadOwner = null;
- refereceCountOpen = 0;
- }
-}
Modified: trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicReadWriteTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicReadWriteTest.java 2006-12-28 04:02:30 UTC (rev 1864)
+++ trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicReadWriteTest.java 2006-12-28 04:20:39 UTC (rev 1865)
@@ -30,13 +30,8 @@
/**
*
- * ValidateValveLogicReadWriteTest and ValidateValveLogicValveTest were written
- * to validate lock mechanism to be used on ValveAspect.
+ * I have written this test just to validate the ReadWriteLock options I had while writing the valve feature on HAAspect
*
- * 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/>
Deleted: trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicValveTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicValveTest.java 2006-12-28 04:02:30 UTC (rev 1864)
+++ trunk/tests/src/org/jboss/test/messaging/util/ValidateValveLogicValveTest.java 2006-12-28 04:20:39 UTC (rev 1865)
@@ -1,202 +0,0 @@
-/*
- * 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();
- }
- }
- }
-
-}
Deleted: trunk/tests/src/org/jboss/test/messaging/util/VeryBasicValveTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/util/VeryBasicValveTest.java 2006-12-28 04:02:30 UTC (rev 1864)
+++ trunk/tests/src/org/jboss/test/messaging/util/VeryBasicValveTest.java 2006-12-28 04:20:39 UTC (rev 1865)
@@ -1,152 +0,0 @@
-/*
- * 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;
-
-/**
- * This verifies the very basic functionality of ConnectionState.Valve.
- * Two functions can't enter at the same time, and this will test that routine
- * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
- * @version <tt>$Revision:$</tt>
- * <p/>
- * $Id:$
- */
-public class VeryBasicValveTest extends TestCase
-{
- private static Logger log = Logger.getLogger(VeryBasicValveTest.class);
-
- static int counter = 0;
- static int counterWait = 0;
- static int countIsOpen = 0;
-
- static boolean started=false;
- static Object startSemaphore = new Object();
- static Valve valve = new Valve();
-
- // multiple threads opening/closing a thread.
- // only one should be able to open it
- public static class SomeThread extends Thread
- {
- int threadId;
- public SomeThread(int threadId)
- {
- this.threadId = threadId;
- }
- public void run()
- {
- try
- {
- log.info("Starting Thread " + threadId);
- synchronized (startSemaphore)
- {
- if (!started)
- {
- startSemaphore.wait();
- }
- }
-
- if (valve.isOpened(true))
- {
- countIsOpen++;
- }
-
- //log.info("Thread " + threadId + "Opening valve");
- if (!valve.open())
- {
- //log.info("Valve couldn't be opened at thread " + threadId);
- synchronized (VeryBasicValveTest.class)
- {
- counterWait ++;
- }
- } else
- {
-
- //log.info("Thread " + threadId + " could open the valve");
-
- //Thread.sleep(1000);
-
- valve.open(); // stack vavles
- if (!valve.isOpened(true))
- {
- fail("Valve should be opened");
- }
-
- synchronized (VeryBasicValveTest.class)
- {
- counter ++;
- }
- valve.close();
- valve.close();
- }
-
- //log.info("Thread " + threadId + " is now closing the valve");
-
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
-
-
- public void testValve() throws Exception
- {
- SomeThread thread[] = new SomeThread[2500];
-
- for (int i=0; i<thread.length; i++)
- {
- thread[i] = new SomeThread(i);
- }
-
- for (int i=0; i<thread.length; i++)
- {
- thread[i].start();
- }
-
- Thread.sleep(1000);
-
- synchronized (startSemaphore)
- {
- started=true;
- startSemaphore.notifyAll();
- }
-
- for (int i = 0; i < thread.length; i++)
- {
- 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