[jbosscache-commits] JBoss Cache SVN: r7841 - in core/branches/flat/src/test/java/org/horizon/loader: bdbje and 1 other directory.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Wed Mar 4 06:32:49 EST 2009


Author: adriancole
Date: 2009-03-04 06:32:48 -0500 (Wed, 04 Mar 2009)
New Revision: 7841

Modified:
   core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java
   core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeLearningTest.java
Log:
cannot use testNG threads until http://jira.opensymphony.com/browse/TESTNG-286 is fixed

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 11:14:01 UTC (rev 7840)
+++ core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java	2009-03-04 11:32:48 UTC (rev 7841)
@@ -23,6 +23,7 @@
 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;
@@ -35,16 +36,11 @@
    protected abstract CacheStore createCacheStore() throws Exception;
 
    protected CacheStore cs;
-   final Random r = new Random();
-   protected StoredEntry[] entries;
 
-   @BeforeMethod(firstTimeOnly = true)
+   @BeforeMethod
    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();
@@ -52,7 +48,7 @@
       }
    }
 
-   @AfterMethod(lastTimeOnly = true)
+   @AfterMethod
    public void tearDown() throws CacheLoaderException {
       if (cs != null) {
          cs.clear();
@@ -65,7 +61,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;
       }
    }
 
@@ -437,15 +433,69 @@
       assert clc.getClassName().equals(cs.getClass().getName()) : "Cache loader doesn't provide a proper configuration type that is capable of creating the loader!";
    }
 
-   @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();
+   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();
+               }
+            }
+         };
       }
+
+      for (Thread t : threads) t.start();
+      for (Thread t : threads) t.join();
+
+      if (!exceptions.isEmpty()) throw exceptions.get(0);
    }
 }

Modified: core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeLearningTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeLearningTest.java	2009-03-04 11:14:01 UTC (rev 7840)
+++ core/branches/flat/src/test/java/org/horizon/loader/bdbje/BdbjeLearningTest.java	2009-03-04 11:32:48 UTC (rev 7841)
@@ -42,6 +42,7 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
@@ -55,7 +56,7 @@
  * @version $Id: $
  * @since 1.0
  */
- at Test(groups = "learning", enabled = false, testName = "loader.bdbje.BdbjeLearningTest")
+ at Test(groups = "unit", enabled = true, testName = "loader.bdbje.BdbjeLearningTest")
 public class BdbjeLearningTest {
    String dbHome = TestingUtil.TEST_FILES + "/Horizon-BdbjeLearningTest";
    Environment env;
@@ -66,11 +67,9 @@
    private static final String STORED_ENTRIES = "storedEntriesDb";
    private Database storedEntriesDb;
    private StoredMap<Object, StoredEntry> cacheMap;
-   final Random r = new Random();
-   protected StoredEntry[] entries;
 
 
-   @BeforeMethod(firstTimeOnly = true)
+   @BeforeMethod
    public void setUp() throws Exception {
       new File(dbHome).mkdirs();
       System.out.println("Opening environment in: " + dbHome);
@@ -98,10 +97,8 @@
       cacheMap =
             new StoredMap<Object, StoredEntry>(storedEntriesDb,
                                                storedEntryKeyBinding, storedEntryValueBinding, true);
-      entries = new StoredEntry[10];
-      for (int i = 0; i < entries.length; i++)
-         entries[i] = new StoredEntry("k" + i, "v" + i);
 
+
    }
 
    public void testTransactionWorker() throws Exception {
@@ -125,7 +122,7 @@
    }
 
 
-   @AfterMethod(lastTimeOnly = true)
+   @AfterMethod
    public void tearDown() throws Exception {
       storedEntriesDb.close();
       javaCatalog.close();
@@ -665,16 +662,72 @@
       assert expected.isEmpty();
    }
 
-   @Test(threadPoolSize = 3, invocationCount = 3, timeOut = 10000)
-   public void testConcurrency() throws CacheLoaderException {
-      for (int i = 0; i < 500; i++) {
-         store(entries[r.nextInt(entries.length)]);
-         cacheMap.remove(entries[r.nextInt(entries.length)]);
-         int index = r.nextInt(entries.length);
-         StoredEntry entry = load(entries[index]);
-         assert entry == null || entry.getValue().equals(entries[index].getValue());
-         loadAll();
+   public void testConcurrency() throws Throwable {
+      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<Throwable> throwables = new LinkedList<Throwable>();
+
+      final Runnable store = new Runnable() {
+         public void run() {
+            try {
+               int randomInt = r.nextInt(10);
+               store(new StoredEntry(keys[randomInt], values[randomInt]));
+            } catch (Throwable e) {
+               throwables.add(e);
+            }
+         }
+      };
+
+      final Runnable remove = new Runnable() {
+         public void run() {
+            try {
+               cacheMap.remove(keys[r.nextInt(10)]);
+            } catch (Throwable e) {
+               throwables.add(e);
+            }
+         }
+      };
+
+      final Runnable get = new Runnable() {
+         public void run() {
+            try {
+               int randomInt = r.nextInt(10);
+               StoredEntry se = load(keys[randomInt]);
+               assert se == null || se.getValue().equals(values[randomInt]);
+               loadAll();
+            } catch (Throwable e) {
+               throwables.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();
+               }
+            }
+         };
       }
+
+      for (Thread t : threads) t.start();
+      for (Thread t : threads) t.join();
+
+      if (!throwables.isEmpty()) throw throwables.get(0);
    }
+
+
 }
 




More information about the jbosscache-commits mailing list