Author: adriancole
Date: 2009-03-04 05:10:36 -0500 (Wed, 04 Mar 2009)
New Revision: 7838
Modified:
core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java
Log:
updated testConcurrency to use testNG thread pool
Modified: core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java 2009-03-04
09:32:20 UTC (rev 7837)
+++ core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java 2009-03-04
10:10:36 UTC (rev 7838)
@@ -23,7 +23,6 @@
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
@@ -36,11 +35,16 @@
protected abstract CacheStore createCacheStore() throws Exception;
protected CacheStore cs;
+ final Random r = new Random();
+ protected StoredEntry[] entries;
- @BeforeMethod
+ @BeforeMethod(firstTimeOnly = true)
public void setUp() throws Exception {
try {
cs = createCacheStore();
+ entries = new StoredEntry[10];
+ for (int i = 0; i < entries.length; i++)
+ entries[i] = new StoredEntry("k" + i, "v" + i);
} catch (Exception e) {
//in IDEs this won't be printed which makes debugging harder
e.printStackTrace();
@@ -48,7 +52,7 @@
}
}
- @AfterMethod
+ @AfterMethod(lastTimeOnly = true)
public void tearDown() throws CacheLoaderException {
if (cs != null) {
cs.clear();
@@ -61,7 +65,7 @@
public void assertNoLocksHeld() {
//doesn't really make sense to add a subclass for this check only
if (cs instanceof LockSupportCacheStore) {
- assert ((LockSupportCacheStore)cs).getTotalLockCount() == 0;
+ assert ((LockSupportCacheStore) cs).getTotalLockCount() == 0;
}
}
@@ -433,69 +437,15 @@
assert clc.getClassName().equals(cs.getClass().getName()) : "Cache loader
doesn't provide a proper configuration type that is capable of creating the
loader!";
}
- public void testConcurrency() throws Exception {
- int numThreads = 3;
- final int loops = 500;
- final String[] keys = new String[10];
- final String[] values = new String[10];
- for (int i = 0; i < 10; i++) keys[i] = "k" + i;
- for (int i = 0; i < 10; i++) values[i] = "v" + i;
-
-
- final Random r = new Random();
- final List<Exception> exceptions = new LinkedList<Exception>();
-
- final Runnable store = new Runnable() {
- public void run() {
- try {
- int randomInt = r.nextInt(10);
- cs.store(new StoredEntry(keys[randomInt], values[randomInt]));
- } catch (Exception e) {
- exceptions.add(e);
- }
- }
- };
-
- final Runnable remove = new Runnable() {
- public void run() {
- try {
- cs.remove(keys[r.nextInt(10)]);
- } catch (Exception e) {
- exceptions.add(e);
- }
- }
- };
-
- final Runnable get = new Runnable() {
- public void run() {
- try {
- int randomInt = r.nextInt(10);
- StoredEntry se = cs.load(keys[randomInt]);
- assert se == null || se.getValue().equals(values[randomInt]);
- cs.loadAll();
- } catch (Exception e) {
- exceptions.add(e);
- }
- }
- };
-
- Thread[] threads = new Thread[numThreads];
-
- for (int i = 0; i < numThreads; i++) {
- threads[i] = new Thread(getClass().getSimpleName() + "-" + i) {
- public void run() {
- for (int i = 0; i < loops; i++) {
- store.run();
- remove.run();
- get.run();
- }
- }
- };
+ @Test(threadPoolSize = 3, invocationCount = 3, timeOut = 10000)
+ public void testConcurrency() throws CacheLoaderException {
+ for (int i = 0; i < 500; i++) {
+ cs.store(entries[r.nextInt(entries.length)]);
+ cs.remove(entries[r.nextInt(entries.length)]);
+ int index = r.nextInt(entries.length);
+ StoredEntry entry = cs.load(entries[index]);
+ assert entry == null || entry.getValue().equals(entries[index].getValue());
+ cs.loadAll();
}
-
- for (Thread t : threads) t.start();
- for (Thread t : threads) t.join();
-
- if (!exceptions.isEmpty()) throw exceptions.get(0);
}
}