[jboss-cvs] JBoss Messaging SVN: r5139 - in trunk: tests/src/org/jboss/messaging/tests/timing/util and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Oct 17 14:53:24 EDT 2008
Author: clebert.suconic at jboss.com
Date: 2008-10-17 14:53:24 -0400 (Fri, 17 Oct 2008)
New Revision: 5139
Modified:
trunk/src/main/org/jboss/messaging/util/VariableLatch.java
trunk/tests/src/org/jboss/messaging/tests/timing/util/VariableLatchTest.java
trunk/tests/src/org/jboss/messaging/tests/unit/util/VariableLatchTest.java
Log:
Just cleanup and format
Modified: trunk/src/main/org/jboss/messaging/util/VariableLatch.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/VariableLatch.java 2008-10-17 18:08:38 UTC (rev 5138)
+++ trunk/src/main/org/jboss/messaging/util/VariableLatch.java 2008-10-17 18:53:24 UTC (rev 5139)
@@ -18,14 +18,13 @@
* 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 java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
-
/**
*
* <p>This class will use the framework provided to by AbstractQueuedSynchronizer.</p>
@@ -48,22 +47,23 @@
* @see AbstractQueuedSynchronizer*/
@SuppressWarnings("serial")
private static class CountSync extends AbstractQueuedSynchronizer
- {
- public CountSync ()
+ {
+ public CountSync()
{
setState(0);
}
-
+
public int getCount()
{
return getState();
}
-
+
+ @Override
public int tryAcquireShared(final int numberOfAqcquires)
{
- return getState()==0 ? 1 : -1;
+ return getState() == 0 ? 1 : -1;
}
-
+
public void add()
{
for (;;)
@@ -76,7 +76,8 @@
}
}
}
-
+
+ @Override
public boolean tryReleaseShared(final int numberOfReleases)
{
for (;;)
@@ -86,39 +87,39 @@
{
return true;
}
-
+
int newState = actualState - numberOfReleases;
-
+
if (compareAndSetState(actualState, newState))
{
- return newState == 0;
+ return newState == 0;
}
}
}
}
-
+
private final CountSync control = new CountSync();
-
+
public int getCount()
{
return control.getCount();
}
-
+
public void up()
{
control.add();
}
-
+
public void down()
{
control.releaseShared(1);
}
-
+
public void waitCompletion() throws InterruptedException
{
control.acquireSharedInterruptibly(1);
}
-
+
public boolean waitCompletion(final long milliseconds) throws InterruptedException
{
return control.tryAcquireSharedNanos(1, TimeUnit.MILLISECONDS.toNanos(milliseconds));
Modified: trunk/tests/src/org/jboss/messaging/tests/timing/util/VariableLatchTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/timing/util/VariableLatchTest.java 2008-10-17 18:08:38 UTC (rev 5138)
+++ trunk/tests/src/org/jboss/messaging/tests/timing/util/VariableLatchTest.java 2008-10-17 18:53:24 UTC (rev 5139)
@@ -28,7 +28,7 @@
* @author <a href="csuconic at redhat.com">Clebert Suconic</a>
*/
public class VariableLatchTest extends UnitTestCase
-{
+{
public void testTimeout() throws Exception
{
VariableLatch latch = new VariableLatch();
@@ -39,7 +39,6 @@
assertFalse(latch.waitCompletion(1000));
long end = System.currentTimeMillis();
- assertTrue("Timeout didn't work correctly", end - start >= 1000
- && end - start < 2000);
+ assertTrue("Timeout didn't work correctly", end - start >= 1000 && end - start < 2000);
}
}
Modified: trunk/tests/src/org/jboss/messaging/tests/unit/util/VariableLatchTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/util/VariableLatchTest.java 2008-10-17 18:08:38 UTC (rev 5138)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/util/VariableLatchTest.java 2008-10-17 18:53:24 UTC (rev 5139)
@@ -37,27 +37,27 @@
public class VariableLatchTest extends TestCase
{
private static final Logger log = Logger.getLogger(VariableLatchTest.class);
-
+
public void testLatchOnSingleThread() throws Exception
{
VariableLatch latch = new VariableLatch();
-
+
for (int i = 1; i <= 100; i++)
{
latch.up();
assertEquals(i, latch.getCount());
}
-
+
for (int i = 100; i > 0; i--)
{
assertEquals(i, latch.getCount());
latch.down();
assertEquals(i - 1, latch.getCount());
}
-
+
latch.waitCompletion();
}
-
+
/**
*
* This test will open numberOfThreads threads, and add numberOfAdds on the
@@ -72,16 +72,17 @@
public void testLatchOnMultiThread() throws Exception
{
final VariableLatch latch = new VariableLatch();
-
+
latch.up(); // We hold at least one, so ThreadWaits won't go away
-
+
final int numberOfThreads = 100;
final int numberOfAdds = 100;
-
+
class ThreadWait extends Thread
{
private volatile boolean waiting = true;
-
+
+ @Override
public void run()
{
try
@@ -90,7 +91,7 @@
{
log.error("Latch timed out");
}
- }
+ }
catch (Exception e)
{
log.error(e);
@@ -98,18 +99,20 @@
waiting = false;
}
}
-
+
class ThreadAdd extends Thread
{
private final CountDownLatch latchReady;
+
private final CountDownLatch latchStart;
-
+
ThreadAdd(final CountDownLatch latchReady, final CountDownLatch latchStart)
{
this.latchReady = latchReady;
this.latchStart = latchStart;
}
-
+
+ @Override
public void run()
{
try
@@ -122,20 +125,20 @@
{
latch.up();
}
- }
+ }
catch (Exception e)
{
log.error(e.getMessage(), e);
}
}
}
-
+
CountDownLatch latchReady = new CountDownLatch(numberOfThreads);
CountDownLatch latchStart = new CountDownLatch(1);
-
+
ThreadAdd[] threadAdds = new ThreadAdd[numberOfThreads];
ThreadWait waits[] = new ThreadWait[numberOfThreads];
-
+
for (int i = 0; i < numberOfThreads; i++)
{
threadAdds[i] = new ThreadAdd(latchReady, latchStart);
@@ -143,33 +146,35 @@
waits[i] = new ThreadWait();
waits[i].start();
}
-
+
latchReady.await();
latchStart.countDown();
-
+
for (int i = 0; i < numberOfThreads; i++)
{
threadAdds[i].join();
}
-
+
for (int i = 0; i < numberOfThreads; i++)
{
assertTrue(waits[i].waiting);
}
-
+
assertEquals(numberOfThreads * numberOfAdds + 1, latch.getCount());
-
+
class ThreadDown extends Thread
{
private final CountDownLatch latchReady;
+
private final CountDownLatch latchStart;
-
+
ThreadDown(final CountDownLatch latchReady, final CountDownLatch latchStart)
{
this.latchReady = latchReady;
this.latchStart = latchStart;
}
-
+
+ @Override
public void run()
{
try
@@ -182,66 +187,69 @@
{
latch.down();
}
- }
+ }
catch (Exception e)
{
log.error(e.getMessage(), e);
}
}
}
-
+
latchReady = new CountDownLatch(numberOfThreads);
latchStart = new CountDownLatch(1);
-
+
ThreadDown down[] = new ThreadDown[numberOfThreads];
-
+
for (int i = 0; i < numberOfThreads; i++)
{
down[i] = new ThreadDown(latchReady, latchStart);
down[i].start();
}
-
+
latchReady.await();
latchStart.countDown();
-
+
for (int i = 0; i < numberOfThreads; i++)
{
down[i].join();
}
-
+
assertEquals(1, latch.getCount());
-
+
for (int i = 0; i < numberOfThreads; i++)
{
assertTrue(waits[i].waiting);
}
-
+
latch.down();
-
+
for (int i = 0; i < numberOfThreads; i++)
{
waits[i].join();
}
-
+
assertEquals(0, latch.getCount());
-
+
for (int i = 0; i < numberOfThreads; i++)
{
assertFalse(waits[i].waiting);
}
}
-
+
public void testReuseLatch() throws Exception
{
final VariableLatch latch = new VariableLatch();
latch.up();
-
+
class ThreadWait extends Thread
{
private volatile boolean waiting = false;
+
private volatile Exception e;
+
private final CountDownLatch readyLatch = new CountDownLatch(1);
-
+
+ @Override
public void run()
{
waiting = true;
@@ -250,9 +258,9 @@
{
if (!latch.waitCompletion(1000))
{
- log.error("Latch timed out!", new Exception ("trace"));
+ log.error("Latch timed out!", new Exception("trace"));
}
- }
+ }
catch (Exception e)
{
log.error(e);
@@ -261,47 +269,47 @@
waiting = false;
}
}
-
+
ThreadWait t = new ThreadWait();
t.start();
-
+
t.readyLatch.await();
-
+
assertEquals(true, t.waiting);
-
+
latch.down();
-
+
t.join();
-
+
assertEquals(false, t.waiting);
-
+
assertNull(t.e);
-
+
latch.up();
-
+
t = new ThreadWait();
t.start();
-
+
t.readyLatch.await();
-
+
assertEquals(true, t.waiting);
-
+
latch.down();
-
+
t.join();
-
+
assertEquals(false, t.waiting);
-
+
assertNull(t.e);
-
+
assertTrue(latch.waitCompletion(1000));
-
+
assertEquals(0, latch.getCount());
-
+
latch.down();
-
+
assertEquals(0, latch.getCount());
-
+
}
-
+
}
More information about the jboss-cvs-commits
mailing list