[infinispan-commits] Infinispan SVN: r1772 - in trunk/core/src/test/java/org/infinispan: test and 1 other directory.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Tue May 11 06:48:53 EDT 2010


Author: manik.surtani at jboss.com
Date: 2010-05-11 06:48:53 -0400 (Tue, 11 May 2010)
New Revision: 1772

Modified:
   trunk/core/src/test/java/org/infinispan/distribution/ConcurrentStartWithReplTest.java
   trunk/core/src/test/java/org/infinispan/test/AbstractCacheTest.java
Log:
Better test

Modified: trunk/core/src/test/java/org/infinispan/distribution/ConcurrentStartWithReplTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/distribution/ConcurrentStartWithReplTest.java	2010-05-11 10:47:56 UTC (rev 1771)
+++ trunk/core/src/test/java/org/infinispan/distribution/ConcurrentStartWithReplTest.java	2010-05-11 10:48:53 UTC (rev 1772)
@@ -2,93 +2,172 @@
 
 import org.infinispan.Cache;
 import org.infinispan.config.Configuration;
+import org.infinispan.config.GlobalConfiguration;
 import org.infinispan.lifecycle.ComponentStatus;
 import org.infinispan.manager.CacheManager;
+import org.infinispan.test.AbstractInfinispanTest;
 import org.infinispan.test.MultipleCacheManagersTest;
 import org.infinispan.test.TestingUtil;
 import org.infinispan.test.fwk.TestCacheManagerFactory;
+import org.infinispan.util.concurrent.AbstractInProcessFuture;
 import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
 
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
 /**
- * To reproduce https://jira.jboss.org/jira/browse/ISPN-428
+ * Tests concurrent startup of replicated and distributed caches
  *
  * @author Mircea.Markus at jboss.com
  * @since 4.1
  */
- at Test (testName = "distribution.ConcurrentStartWithReplTest", groups = "functional")
-public class ConcurrentStartWithReplTest extends MultipleCacheManagersTest {
+ at Test(testName = "distribution.ConcurrentStartWithReplTest", groups = "functional")
+public class ConcurrentStartWithReplTest extends AbstractInfinispanTest {
 
-   private Configuration config;
-   private static final String TOPOLOGY_CACHE_NAME = "TopologyCacheName";
+   Configuration replCfg, distCfg;
 
-   @Override
-   protected void assertSupportedConfig() {
-   }
+   @BeforeTest
+   public void setUp() {
+      replCfg = MultipleCacheManagersTest.getDefaultClusteredConfig(Configuration.CacheMode.REPL_SYNC);
+      replCfg.setFetchInMemoryState(true);
 
-   @AfterMethod
-   @Override
-   protected void clearContent() throws Throwable {
+      distCfg = MultipleCacheManagersTest.getDefaultClusteredConfig(Configuration.CacheMode.DIST_SYNC);
+      distCfg.setRehashEnabled(true);
    }
 
-   @Override
-   protected void createCacheManagers() throws Throwable {
-      config = getDefaultClusteredConfig(getCacheMode());
-      CacheManager cm1 = TestCacheManagerFactory.createClusteredCacheManager(config);
-      CacheManager cm2 = TestCacheManagerFactory.createClusteredCacheManager(config);
-      registerCacheManager(cm1);
-      registerCacheManager(cm2);
+   @Test(timeOut = 30000)
+   public void testSequence1() throws ExecutionException, InterruptedException {
+      /*
 
-      initStateTransfer(manager(0));
-      initStateTransfer(manager(1));
+      Sequence 1:
 
-      manager(0).getCache();
-      manager(1).getCache();
+         C1 (repl) (becomes coord)
+         C2 (repl)
+         C1 (dist)
+         C2 (dist)
 
-      TestingUtil.blockUntilViewReceived(manager(0).getCache(), 2, 10000);
-      TestingUtil.blockUntilCacheStatusAchieved(manager(0).getCache(), ComponentStatus.RUNNING, 10000);
-      TestingUtil.blockUntilCacheStatusAchieved(manager(1).getCache(), ComponentStatus.RUNNING, 10000);
+         in the same thread.
 
+       */
 
-      manager(0).getCache().put("k","v");
-      manager(0).getCache().get("k").equals("v");
-      manager(1).getCache().get("k").equals("v");
+      doTest(true, false);
 
-      log.info("Local replication test passed!");
    }
 
-   private void initStateTransfer(CacheManager cacheManager) {
-      defineTopologyCacheConfig(cacheManager);
-      Cache<Object, Object> cache = cacheManager.getCache(TOPOLOGY_CACHE_NAME);
-      Object o = cache.get("view");
-      if (o != null) {
-         cache.replace("view", "aaa");
-      } else {
-         cache.put("view", "bbb");
-      }
+   @Test(timeOut = 30000)
+   public void testSequence2() throws ExecutionException, InterruptedException {
+      /*
+
+      Sequence 2:
+
+         C1 (repl) (becomes coord)
+         C2 (repl)
+         C2 (dist)
+         C1 (dist)
+
+         in the same thread.
+
+       */
+
+      doTest(false, false);
    }
 
-   protected void defineTopologyCacheConfig(CacheManager cacheManager) {
-      Configuration topologyCacheConfig = new Configuration();
-      topologyCacheConfig.setCacheMode(Configuration.CacheMode.REPL_SYNC);
-      topologyCacheConfig.setSyncReplTimeout(10000);
-      topologyCacheConfig.setFetchInMemoryState(true);
-      cacheManager.defineConfiguration(TOPOLOGY_CACHE_NAME, topologyCacheConfig);
+   @Test(timeOut = 30000)
+   public void testSequence3() throws ExecutionException, InterruptedException {
+      /*
+
+      Sequence 3:
+
+         C1 (repl) (becomes coord)
+         C2 (repl)
+         C1 (dist) (async thread)
+         C2 (dist) (async thread)
+
+         in the same thread, except the last two which are in separate threads
+
+       */
+      doTest(true, true);
    }
 
+   @Test(timeOut = 30000)
+   public void testSequence4() throws ExecutionException, InterruptedException {
+      /*
 
-   protected Configuration.CacheMode getCacheMode() {
-      return Configuration.CacheMode.DIST_SYNC;
+      Sequence 4:
+
+         C1 (repl) (becomes coord)
+         C2 (repl)
+         C2 (dist) (async thread)
+         C1 (dist) (async thread)
+
+         in the same thread, except the last two which are in separate threads
+
+       */
+      doTest(false, true);
    }
 
-   public void testAllFine() {
-      for (int i = 0; i < 10; i++) {
-            manager(0).getCache().put("k" + i, "v" + i);
+   private void doTest(boolean inOrder, boolean nonBlockingStartupForDist) throws ExecutionException, InterruptedException {
+      CacheManager cm1 = TestCacheManagerFactory.createCacheManager(GlobalConfiguration.getClusteredDefault());
+      CacheManager cm2 = TestCacheManagerFactory.createCacheManager(GlobalConfiguration.getClusteredDefault());
+      try {
+         cm1.defineConfiguration("r", replCfg);
+         cm1.defineConfiguration("d", distCfg);
+         cm2.defineConfiguration("r", replCfg);
+         cm2.defineConfiguration("d", distCfg);
+
+         // first start the repl caches
+         Cache<String, String> c1r = startCache(cm1, "r", false).get();
+         c1r.put("key", "value");
+         Cache<String, String> c2r = startCache(cm2, "r", false).get();
+         assert "value".equals(c2r.get("key"));
+
+         // now the dist ones
+         Future<Cache<String, String>> c1df = startCache(inOrder ? cm1 : cm2, "d", nonBlockingStartupForDist);
+         Future<Cache<String, String>> c2df = startCache(inOrder ? cm2 : cm1, "d", nonBlockingStartupForDist);
+         Cache<String, String> c1d = c1df.get();
+         Cache<String, String> c2d = c2df.get();
+
+         c1d.put("key", "value");
+         assert "value".equals(c2d.get("key"));
+      } finally {
+         TestingUtil.killCacheManagers(cm1, cm2);
       }
-      for (int i = 0; i < 10; i++) {
-            manager(1).getCache().put("k" + i, "v" + i);
-      }      
    }
+
+   private Future<Cache<String, String>> startCache(final CacheManager cm, final String cacheName, boolean nonBlockingStartup) {
+      final Callable<Cache<String, String>> cacheCreator = new Callable<Cache<String, String>>() {
+
+         @Override
+         public Cache<String, String> call() throws Exception {
+            Cache<String, String> c = cm.getCache(cacheName);
+            return c;
+         }
+      };
+      if (nonBlockingStartup) {
+         final ExecutorService e = Executors.newFixedThreadPool(1);
+         return e.submit(cacheCreator);
+      } else {
+         return new AbstractInProcessFuture<Cache<String, String>>() {
+            @Override
+            public Cache<String, String> get() throws InterruptedException, ExecutionException {
+               try {
+                  return cacheCreator.call();
+               } catch (Exception e) {
+                  throw new ExecutionException(e);
+               }
+            }
+         };
+      }
+   }
+
 }
 
 

Modified: trunk/core/src/test/java/org/infinispan/test/AbstractCacheTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/test/AbstractCacheTest.java	2010-05-11 10:47:56 UTC (rev 1771)
+++ trunk/core/src/test/java/org/infinispan/test/AbstractCacheTest.java	2010-05-11 10:48:53 UTC (rev 1772)
@@ -46,11 +46,11 @@
     * make sure that an commit message will be dispatched in the same test method it was triggered and it will not
     * interfere with further log messages.  This is a non-transactional configuration.
     */
-   protected Configuration getDefaultClusteredConfig(Configuration.CacheMode mode) {
+   public static Configuration getDefaultClusteredConfig(Configuration.CacheMode mode) {
       return getDefaultClusteredConfig(mode, false);
    }
 
-   protected Configuration getDefaultClusteredConfig(Configuration.CacheMode mode, boolean transactional) {
+   public static Configuration getDefaultClusteredConfig(Configuration.CacheMode mode, boolean transactional) {
       Configuration configuration = TestCacheManagerFactory.getDefaultConfiguration(transactional);
       configuration.setCacheMode(mode);
       configuration.setSyncCommitPhase(true);



More information about the infinispan-commits mailing list