From 024beca1473474a20cd6cf9fab43b6c99824f1cf Mon Sep 17 00:00:00 2001
From: kosch <tf@tfromm.com>
Date: Tue, 13 Nov 2012 08:47:14 +0100
Subject: [PATCH] ISPN-2495 Testcase for issue and migrated deprecated config
 api usage

---
 .../StateTransferCacheLoaderFunctionalTest.java    |  100 ++++++++++++++++++--
 ...StateTransferFileCacheLoaderFunctionalTest.java |   51 +++++-----
 .../statetransfer/StateTransferFunctionalTest.java |   20 ++--
 3 files changed, 126 insertions(+), 45 deletions(-)

diff --git a/core/src/test/java/org/infinispan/statetransfer/StateTransferCacheLoaderFunctionalTest.java b/core/src/test/java/org/infinispan/statetransfer/StateTransferCacheLoaderFunctionalTest.java
index 9e89a48..d6fbaf7 100644
--- a/core/src/test/java/org/infinispan/statetransfer/StateTransferCacheLoaderFunctionalTest.java
+++ b/core/src/test/java/org/infinispan/statetransfer/StateTransferCacheLoaderFunctionalTest.java
@@ -23,15 +23,24 @@
 package org.infinispan.statetransfer;
 
 import org.infinispan.Cache;
-import org.infinispan.config.CacheLoaderManagerConfig;
+import org.infinispan.configuration.cache.CacheMode;
+import org.infinispan.configuration.cache.ConfigurationBuilder;
 import org.infinispan.loaders.CacheLoader;
 import org.infinispan.loaders.CacheLoaderManager;
-import org.infinispan.loaders.CacheStoreConfig;
-import org.infinispan.loaders.dummy.DummyInMemoryCacheStore;
+import org.infinispan.loaders.dummy.DummyInMemoryCacheStoreConfigurationBuilder;
 import org.infinispan.manager.EmbeddedCacheManager;
 import org.infinispan.test.TestingUtil;
 import org.testng.annotations.Test;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+
+import static org.infinispan.statetransfer.StateTransferTestingUtil.verifyNoData;
+import static org.infinispan.statetransfer.StateTransferTestingUtil.verifyNoDataOnLoader;
+import static org.testng.Assert.assertEquals;
+
 @Test(groups = "functional", testName = "statetransfer.StateTransferCacheLoaderFunctionalTest", enabled = true)
 public class StateTransferCacheLoaderFunctionalTest extends StateTransferFunctionalTest {
    int id;
@@ -47,13 +56,14 @@ public StateTransferCacheLoaderFunctionalTest() {
 
    @Override
    protected EmbeddedCacheManager createCacheManager() {
+      configurationBuilder.loaders().clearCacheLoaders();
       // increment the DIMCS store id
-      CacheLoaderManagerConfig clmc = new CacheLoaderManagerConfig();
-      CacheStoreConfig clc = new DummyInMemoryCacheStore.Cfg("store number " + id++);
-      clmc.addCacheLoaderConfig(clc);
-      clc.setFetchPersistentState(true);
-      clmc.setShared(sharedCacheLoader.get());
-      config.setCacheLoaderManagerConfig(clmc);
+      DummyInMemoryCacheStoreConfigurationBuilder dimcs = new DummyInMemoryCacheStoreConfigurationBuilder(configurationBuilder.loaders());
+      dimcs.storeName("store number " + id++);
+      dimcs.fetchPersistentState(true);
+      configurationBuilder.loaders().addLoader(dimcs);
+      configurationBuilder.loaders().shared(sharedCacheLoader.get()).preload(true);
+
       return super.createCacheManager();
    }
 
@@ -114,4 +124,76 @@ public void testSharedLoader() throws Exception {
          sharedCacheLoader.set(false);
       }
    }
+
+   public void testInitialSlowPreload() throws Exception {
+      // Test for ISPN-2494
+      // Preload on cache on node 1 is slow and unfinished at the point, where cache on node 2 starts.
+      // Node 2 requests state, got answer that no entries available. Since node 2 is not coordinator,
+      // preload is ignored. At the end, node 1 contains REPL cache with all entries, node 2 has same cache without entries.
+      try {
+         sharedCacheLoader.set(true);
+         EmbeddedCacheManager cm1 = createCacheManager();
+         Cache<Object, Object> cache1 = cm1.getCache(cacheName);
+         verifyNoDataOnLoader(cache1);
+         verifyNoData(cache1);
+
+         // write initial data
+         cache1.put("A", new DelayedUnmarshal());
+         cache1.put("B", new DelayedUnmarshal());
+         cache1.put("C", new DelayedUnmarshal());
+         assertEquals(cache1.size(), 3);
+         cm1.stop();
+
+         // this cache is only used to start networking
+         final ConfigurationBuilder defaultConfigurationBuilder = getDefaultClusteredCacheConfig(CacheMode.REPL_SYNC, true);
+
+         // now lets start cm and shortly after another cache manager
+         final EmbeddedCacheManager cm2 = super.createCacheManager();
+         cm2.defineConfiguration("initialCache", defaultConfigurationBuilder.build());
+         cm2.startCaches("initialCache");
+
+         EmbeddedCacheManager cm3 = super.createCacheManager();
+         cm3.defineConfiguration("initialCache", defaultConfigurationBuilder.build());
+         cm3.startCaches("initialCache");
+
+         // networking is started and cluster has 2 members
+         TestingUtil.blockUntilViewsReceived(60000, cm2.getCache("initialCache"), cm3.getCache("initialCache"));
+
+         // now fork start of "slow" cache
+         Thread worker = new Thread(){
+            @Override
+            public void run() {
+               cm2.startCaches(cacheName);
+            }
+         };
+         worker.start();
+         // lets wait a bit, cache is started pon cm2, but preload is not finished
+         TestingUtil.sleepThread(1000);
+
+         // uncomment this to see failing test
+         worker.join();
+
+         // at this point node is not alone, so preload is not used
+         // the start of the cache must be blocked until state transfer is finished
+         cm3.startCaches(cacheName);
+         assertEquals(cm3.getCache(cacheName).size(), 3);
+      } finally {
+         sharedCacheLoader.set(false);
+      }
+   }
+
+   public static class DelayedUnmarshal implements Serializable {
+
+      private static final long serialVersionUID = 1L;
+
+      private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
+         TestingUtil.sleepThread(2000);
+         in.defaultReadObject();
+      }
+
+      private void writeObject(ObjectOutputStream out) throws IOException {
+         out.defaultWriteObject();
+      }
+   }
+
 }
diff --git a/core/src/test/java/org/infinispan/statetransfer/StateTransferFileCacheLoaderFunctionalTest.java b/core/src/test/java/org/infinispan/statetransfer/StateTransferFileCacheLoaderFunctionalTest.java
index dd970fe..32d9738 100644
--- a/core/src/test/java/org/infinispan/statetransfer/StateTransferFileCacheLoaderFunctionalTest.java
+++ b/core/src/test/java/org/infinispan/statetransfer/StateTransferFileCacheLoaderFunctionalTest.java
@@ -22,31 +22,30 @@
  */
 package org.infinispan.statetransfer;
 
-import java.io.File;
+import java.io.*;
 import java.lang.reflect.Method;
 import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
-import java.util.concurrent.ThreadFactory;
 
 import org.infinispan.Cache;
-import org.infinispan.config.CacheLoaderManagerConfig;
-import org.infinispan.config.Configuration;
-import org.infinispan.loaders.file.FileCacheStoreConfig;
+import org.infinispan.configuration.cache.CacheMode;
+import org.infinispan.configuration.cache.ConfigurationBuilder;
+import org.infinispan.configuration.cache.FileCacheStoreConfigurationBuilder;
+import org.infinispan.loaders.CacheLoader;
 import org.infinispan.manager.CacheContainer;
 import org.infinispan.manager.EmbeddedCacheManager;
+import org.infinispan.marshall.AdvancedExternalizer;
 import org.infinispan.test.MultipleCacheManagersTest;
 import org.infinispan.test.TestingUtil;
+import org.infinispan.transaction.LockingMode;
 import org.infinispan.util.logging.Log;
 import org.infinispan.util.logging.LogFactory;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeTest;
-import org.testng.annotations.Optional;
-import org.testng.annotations.Parameters;
 import org.testng.annotations.Test;
 
 import static org.infinispan.statetransfer.StateTransferTestingUtil.*;
+import static org.testng.Assert.assertEquals;
 
 /**
  * StateTransferFileCacheStoreFunctionalTest.
@@ -69,7 +68,7 @@ protected Boolean initialValue() {
    String tmpDirectory3;
    String tmpDirectory4;
 
-   Configuration config;
+   ConfigurationBuilder configurationBuilder;
 
    @BeforeTest
    protected void setUpTempDir() {
@@ -95,26 +94,26 @@ protected void clearTempDir() {
    @Override
    protected void createCacheManagers() throws Throwable {
       // This impl only really sets up a configuration for use later.
-      config = getDefaultClusteredConfig(Configuration.CacheMode.REPL_SYNC, true);
-      config.setSyncReplTimeout(30000);
-      config.setFetchInMemoryState(true);
-      config.setUseLockStriping(false); // reduces the odd chance of a key collision and deadlock
+      configurationBuilder = getDefaultClusteredCacheConfig(CacheMode.REPL_SYNC, true);
+      configurationBuilder.transaction().lockingMode(LockingMode.PESSIMISTIC);
+      configurationBuilder.clustering().sync().replTimeout(30000);
+      configurationBuilder.clustering().stateTransfer().fetchInMemoryState(true);
+      configurationBuilder.locking().useLockStriping(false); // reduces the odd chance of a key collision and deadlock
    }
 
-   protected CacheContainer createCacheManager(String tmpDirectory) {
-      // increment the DIMCS store id
-      FileCacheStoreConfig cfg = new FileCacheStoreConfig();
-      cfg.setLocation(tmpDirectory);
-      cfg.setPurgeSynchronously(true); // for more accurate unit testing
-      cfg.setFetchPersistentState(true);
+   protected EmbeddedCacheManager createCacheManager(String tmpDirectory) {
+      configurationBuilder.loaders().clearCacheLoaders();
+      configurationBuilder.loaders().shared(sharedCacheLoader.get());
 
-      CacheLoaderManagerConfig clmc = new CacheLoaderManagerConfig();
-      clmc.addCacheLoaderConfig(cfg);
-      clmc.setShared(sharedCacheLoader.get());
-      config.setCacheLoaderManagerConfig(clmc);
+      FileCacheStoreConfigurationBuilder fcsBuilder = configurationBuilder.loaders().addFileCacheStore();
+      fcsBuilder
+            .purgeSynchronously(true) // for more accurate unit testing
+            .fetchPersistentState(true)
+            .purgeOnStartup(false)
+            .location(tmpDirectory);
 
       EmbeddedCacheManager cm = addClusterEnabledCacheManager();
-      cm.defineConfiguration(cacheName, config.clone());
+      cm.defineConfiguration(cacheName, configurationBuilder.build());
       return cm;
    }
 
@@ -270,6 +269,4 @@ public Void call() throws Exception {
          if (cm40 != null) cm40.stop();
       }
    }
-
-
 }
diff --git a/core/src/test/java/org/infinispan/statetransfer/StateTransferFunctionalTest.java b/core/src/test/java/org/infinispan/statetransfer/StateTransferFunctionalTest.java
index 14e3128..7cf0b4e 100644
--- a/core/src/test/java/org/infinispan/statetransfer/StateTransferFunctionalTest.java
+++ b/core/src/test/java/org/infinispan/statetransfer/StateTransferFunctionalTest.java
@@ -23,13 +23,15 @@
 package org.infinispan.statetransfer;
 
 import org.infinispan.Cache;
-import org.infinispan.config.Configuration;
+import org.infinispan.configuration.cache.CacheMode;
+import org.infinispan.configuration.cache.ConfigurationBuilder;
 import org.infinispan.manager.EmbeddedCacheManager;
 import org.infinispan.remoting.transport.Address;
 import org.infinispan.test.MultipleCacheManagersTest;
 import org.infinispan.test.TestingUtil;
 import org.infinispan.test.fwk.TestCacheManagerFactory;
 import org.infinispan.test.fwk.TransportFlags;
+import org.infinispan.transaction.LockingMode;
 import org.infinispan.util.logging.Log;
 import org.infinispan.util.logging.LogFactory;
 import org.testng.annotations.Test;
@@ -58,7 +60,7 @@
    public static final Integer TWENTY = 20;
    public static final Integer FORTY = 40;
 
-   Configuration config;
+   ConfigurationBuilder configurationBuilder;
    protected final String cacheName;
 
    private volatile int testCount = 0;
@@ -75,16 +77,16 @@ public StateTransferFunctionalTest(String testCacheName) {
    }
 
    protected void createCacheManagers() throws Throwable {
-      // This impl only really sets up a configuration for use later.
-      config = getDefaultClusteredConfig(Configuration.CacheMode.REPL_SYNC, true);
-      config.setSyncReplTimeout(30000);
-      config.setFetchInMemoryState(true);
-      config.setUseLockStriping(false); // reduces the odd chance of a key collision and deadlock
+      configurationBuilder = getDefaultClusteredCacheConfig(CacheMode.REPL_SYNC, true);
+      configurationBuilder.transaction().lockingMode(LockingMode.PESSIMISTIC);
+      configurationBuilder.clustering().sync().replTimeout(30000);
+      configurationBuilder.clustering().stateTransfer().fetchInMemoryState(true);
+      configurationBuilder.locking().useLockStriping(false); // reduces the odd chance of a key collision and deadlock
    }
 
    protected EmbeddedCacheManager createCacheManager() {
       EmbeddedCacheManager cm = addClusterEnabledCacheManager(new TransportFlags().withMerge(true));
-      cm.defineConfiguration(cacheName, config.clone());
+      cm.defineConfiguration(cacheName, configurationBuilder.build());
       return cm;
    }
 
@@ -198,7 +200,7 @@ public void testInitialStateTransferCacheNotPresent(Method m) throws Exception {
       node.waitForJoin(60000, cache1, cache2);
       node.verifyStateTransfer(new CacheVerifier(cache2));
 
-      cacheManager1.defineConfiguration("otherCache", config.clone());
+      cacheManager1.defineConfiguration("otherCache", configurationBuilder.build());
       cacheManager1.getCache("otherCache");
       logTestEnd(m);
    }
-- 
1.7.10

