Changed to java.util.concurrent package for concurrency test

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2663 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2006-04-18 19:31:06 +00:00
parent e8d8361e8d
commit 6ddab4043e

View File

@@ -16,6 +16,9 @@
*/ */
package org.alfresco.repo.version.common.counter; package org.alfresco.repo.version.common.counter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.transaction.UserTransaction; import javax.transaction.UserTransaction;
import junit.framework.TestCase; import junit.framework.TestCase;
@@ -55,15 +58,6 @@ public class VersionCounterDaoServiceTest extends TestCase
storeRef2 = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "test2_" + System.currentTimeMillis()); storeRef2 = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "test2_" + System.currentTimeMillis());
} }
@Override
public void tearDown() throws Exception
{
synchronized(endWait)
{
endWait.notifyAll();
}
}
public void testSetUp() throws Exception public void testSetUp() throws Exception
{ {
assertNotNull(transactionService); assertNotNull(transactionService);
@@ -113,44 +107,16 @@ public class VersionCounterDaoServiceTest extends TestCase
public void testConcurrentVersionNumber() throws Throwable public void testConcurrentVersionNumber() throws Throwable
{ {
VersionCounterThread[] threads = new VersionCounterThread[5]; VersionCounterThread[] threads = new VersionCounterThread[threadCount];
for (int i = 0; i < threads.length; i++) for (int i = 0; i < threadCount; i++)
{ {
threads[i] = new VersionCounterThread("VersionCounterThread_" + i); threads[i] = new VersionCounterThread("VersionCounterThread_" + i);
// start the thread // start the thread
threads[i].start(); threads[i].start();
} }
// now wait until all the threads are waiting
int iteration = 0; // wait for the threads to all be done (or 10 seconds has passed)
while (waitCount < threads.length && iteration++ < 5000) endSignal.await(10, TimeUnit.SECONDS);
{
synchronized (this)
{
this.wait(20); // 20 ms wait
}
}
// reset wait count
this.waitCount = 0;
// kick them off
synchronized(beginWait)
{
beginWait.notifyAll();
}
// now wait until all the threads are waiting
while (waitCount < threads.length && iteration++ < 5000)
{
synchronized (this)
{
this.wait(20); // 20 ms wait
}
}
// let them finish
iteration = 0;
synchronized(endWait)
{
endWait.notifyAll();
}
// check for exceptions // check for exceptions
for (VersionCounterThread thread : threads) for (VersionCounterThread thread : threads)
@@ -162,9 +128,9 @@ public class VersionCounterDaoServiceTest extends TestCase
} }
} }
private Object beginWait = new String("BEGIN_WAIT"); private int threadCount = 5;
private Object endWait = new String("END_WAIT"); private CountDownLatch startSignal = new CountDownLatch(threadCount);
private int waitCount = 0; private CountDownLatch endSignal = new CountDownLatch(threadCount);
private class VersionCounterThread extends Thread private class VersionCounterThread extends Thread
{ {
@@ -182,24 +148,13 @@ public class VersionCounterDaoServiceTest extends TestCase
public Object doWork() throws Exception public Object doWork() throws Exception
{ {
// wait for all other threads to enter into their transactions // wait for all other threads to enter into their transactions
synchronized(beginWait) startSignal.countDown();
{
waitCount++;
beginWait.wait(10000L);
}
int startVersion = counter.currentVersionNumber(storeRef1); int startVersion = counter.currentVersionNumber(storeRef1);
// increment it // increment it
int incrementedVersion = counter.nextVersionNumber(storeRef1); int incrementedVersion = counter.nextVersionNumber(storeRef1);
assertTrue("Version number was not incremented", incrementedVersion > startVersion); assertTrue("Version number was not incremented", incrementedVersion > startVersion);
// wait for all other threads to have finished their increments
synchronized(endWait)
{
waitCount++;
endWait.wait(10000L);
}
return null; return null;
} }
}; };
@@ -213,6 +168,10 @@ public class VersionCounterDaoServiceTest extends TestCase
error = e; error = e;
e.printStackTrace(); e.printStackTrace();
} }
finally
{
endSignal.countDown();
}
} }
} }
} }