[infinispan-commits] Infinispan SVN: r2623 - in branches/4.2.x/core/src/test/java/org/infinispan: distribution/rehash and 1 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Thu Oct 28 08:04:25 EDT 2010


Author: manik.surtani at jboss.com
Date: 2010-10-28 08:04:21 -0400 (Thu, 28 Oct 2010)
New Revision: 2623

Added:
   branches/4.2.x/core/src/test/java/org/infinispan/distribution/MagicKey.java
Modified:
   branches/4.2.x/core/src/test/java/org/infinispan/distribution/BaseDistFunctionalTest.java
   branches/4.2.x/core/src/test/java/org/infinispan/distribution/MagicKeyTest.java
   branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/L1OnRehashTest.java
   branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/RehashCompletedOnJoinTest.java
   branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/RehashTestBase.java
   branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/WorkDuringJoinTest.java
   branches/4.2.x/core/src/test/java/org/infinispan/lock/APIDistTest.java
Log:
Improved tests to use MagicKey

Modified: branches/4.2.x/core/src/test/java/org/infinispan/distribution/BaseDistFunctionalTest.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/distribution/BaseDistFunctionalTest.java	2010-10-27 21:00:52 UTC (rev 2622)
+++ branches/4.2.x/core/src/test/java/org/infinispan/distribution/BaseDistFunctionalTest.java	2010-10-28 12:04:21 UTC (rev 2623)
@@ -24,12 +24,10 @@
 import org.testng.annotations.Test;
 
 import javax.transaction.TransactionManager;
-import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Random;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -376,69 +374,4 @@
    protected TransactionManager getTransactionManager(Cache<?, ?> cache) {
       return TestingUtil.getTransactionManager(cache);
    }
-
-   /**
-    * A special type of key that if passed a cache in its constructor, will ensure it will always be assigned to that
-    * cache (plus however many additional caches in the hash space)
-    */
-   public static class MagicKey implements Serializable {
-      /** The serialVersionUID */
-      private static final long serialVersionUID = -835275755945753954L;
-      String name = null;
-      int hashcode;
-      String address;
-
-      public MagicKey(Cache<?, ?> toMapTo) {
-         address = addressOf(toMapTo).toString();
-         Random r = new Random();
-         for (; ;) {
-            // create a dummy object with this hashcode
-            final int hc = r.nextInt();
-            Object dummy = new Object() {
-               @Override
-               public int hashCode() {
-                  return hc;
-               }
-            };
-
-            if (BaseDistFunctionalTest.isFirstOwner(toMapTo, dummy)) {
-               // we have found a hashcode that works!
-               hashcode = hc;
-               break;
-            }
-         }
-      }
-
-      public MagicKey(Cache<?, ?> toMapTo, String name) {
-         this(toMapTo);
-         this.name = name;
-      }
-
-      @Override
-      public int hashCode() {
-         return hashcode;
-      }
-
-      @Override
-      public boolean equals(Object o) {
-         if (this == o) return true;
-         if (o == null || getClass() != o.getClass()) return false;
-
-         MagicKey magicKey = (MagicKey) o;
-
-         if (hashcode != magicKey.hashcode) return false;
-         if (address != null ? !address.equals(magicKey.address) : magicKey.address != null) return false;
-
-         return true;
-      }
-
-      @Override
-      public String toString() {
-         return "MagicKey{" +
-               (name == null ? "" : "name=" + name + ", ") +
-               "hashcode=" + hashcode +
-               ", address='" + address + '\'' +
-               '}';
-      }
-   }
 }

Added: branches/4.2.x/core/src/test/java/org/infinispan/distribution/MagicKey.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/distribution/MagicKey.java	                        (rev 0)
+++ branches/4.2.x/core/src/test/java/org/infinispan/distribution/MagicKey.java	2010-10-28 12:04:21 UTC (rev 2623)
@@ -0,0 +1,75 @@
+package org.infinispan.distribution;
+
+import org.infinispan.Cache;
+
+import java.io.Serializable;
+import java.util.Random;
+
+import static org.infinispan.distribution.BaseDistFunctionalTest.addressOf;
+import static org.infinispan.distribution.BaseDistFunctionalTest.isFirstOwner;
+
+/**
+ * A special type of key that if passed a cache in its constructor, will ensure it will always be assigned to that cache
+ * (plus however many additional caches in the hash space)
+ */
+public class MagicKey implements Serializable {
+   /**
+    * The serialVersionUID
+    */
+   private static final long serialVersionUID = -835275755945753954L;
+   String name = null;
+   int hashcode;
+   String address;
+
+   public MagicKey(Cache<?, ?> toMapTo) {
+      address = addressOf(toMapTo).toString();
+      Random r = new Random();
+      Object dummy;
+      do {
+         // create a dummy object with this hashcode
+         final int hc = r.nextInt();
+         dummy = new Object() {
+            @Override
+            public int hashCode() {
+               return hc;
+            }
+         };
+
+      } while (!isFirstOwner(toMapTo, dummy));
+
+      // we have found a hashcode that works!
+      hashcode = dummy.hashCode();
+   }
+
+   public MagicKey(Cache<?, ?> toMapTo, String name) {
+      this(toMapTo);
+      this.name = name;
+   }
+
+   @Override
+   public int hashCode () {
+      return hashcode;
+   }
+
+   @Override
+   public boolean equals (Object o) {
+      if (this == o) return true;
+      if (o == null || getClass() != o.getClass()) return false;
+
+      MagicKey magicKey = (MagicKey) o;
+
+      if (hashcode != magicKey.hashcode) return false;
+      if (address != null ? !address.equals(magicKey.address) : magicKey.address != null) return false;
+
+      return true;
+   }
+
+   @Override
+   public String toString() {
+      return "MagicKey{" +
+              (name == null ? "" : "name=" + name + ", ") +
+              "hashcode=" + hashcode +
+              ", address='" + address + '\'' +
+              '}';
+   }
+}
\ No newline at end of file

Modified: branches/4.2.x/core/src/test/java/org/infinispan/distribution/MagicKeyTest.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/distribution/MagicKeyTest.java	2010-10-27 21:00:52 UTC (rev 2622)
+++ branches/4.2.x/core/src/test/java/org/infinispan/distribution/MagicKeyTest.java	2010-10-28 12:04:21 UTC (rev 2623)
@@ -5,25 +5,25 @@
 @Test(groups = "unit", testName = "distribution.MagicKeyTest")
 public class MagicKeyTest extends BaseDistFunctionalTest {
    public void testMagicKeys() {
-      BaseDistFunctionalTest.MagicKey k1 = new BaseDistFunctionalTest.MagicKey(c1);
+      MagicKey k1 = new MagicKey(c1);
       assert getDistributionManager(c1).isLocal(k1);
       assert getDistributionManager(c2).isLocal(k1);
       assert !getDistributionManager(c3).isLocal(k1);
       assert !getDistributionManager(c4).isLocal(k1);
 
-      BaseDistFunctionalTest.MagicKey k2 = new BaseDistFunctionalTest.MagicKey(c2);
+      MagicKey k2 = new MagicKey(c2);
       assert !getDistributionManager(c1).isLocal(k2);
       assert getDistributionManager(c2).isLocal(k2);
       assert getDistributionManager(c3).isLocal(k2);
       assert !getDistributionManager(c4).isLocal(k2);
 
-      BaseDistFunctionalTest.MagicKey k3 = new BaseDistFunctionalTest.MagicKey(c3);
+      MagicKey k3 = new MagicKey(c3);
       assert !getDistributionManager(c1).isLocal(k3);
       assert !getDistributionManager(c2).isLocal(k3);
       assert getDistributionManager(c3).isLocal(k3);
       assert getDistributionManager(c4).isLocal(k3);
 
-      BaseDistFunctionalTest.MagicKey k4 = new BaseDistFunctionalTest.MagicKey(c4);
+      MagicKey k4 = new MagicKey(c4);
       assert getDistributionManager(c1).isLocal(k4);
       assert !getDistributionManager(c2).isLocal(k4);
       assert !getDistributionManager(c3).isLocal(k4);

Modified: branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/L1OnRehashTest.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/L1OnRehashTest.java	2010-10-27 21:00:52 UTC (rev 2622)
+++ branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/L1OnRehashTest.java	2010-10-28 12:04:21 UTC (rev 2623)
@@ -1,6 +1,7 @@
 package org.infinispan.distribution.rehash;
 
 import org.infinispan.Cache;
+import org.infinispan.distribution.MagicKey;
 import org.infinispan.manager.EmbeddedCacheManager;
 import org.infinispan.test.TestingUtil;
 import org.infinispan.distribution.BaseDistFunctionalTest;

Modified: branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/RehashCompletedOnJoinTest.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/RehashCompletedOnJoinTest.java	2010-10-27 21:00:52 UTC (rev 2622)
+++ branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/RehashCompletedOnJoinTest.java	2010-10-28 12:04:21 UTC (rev 2623)
@@ -3,6 +3,7 @@
 import org.infinispan.Cache;
 import org.infinispan.distribution.BaseDistFunctionalTest;
 import org.infinispan.distribution.DistributionManager;
+import org.infinispan.distribution.MagicKey;
 import org.infinispan.manager.EmbeddedCacheManager;
 import org.testng.annotations.Test;
 

Modified: branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/RehashTestBase.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/RehashTestBase.java	2010-10-27 21:00:52 UTC (rev 2622)
+++ branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/RehashTestBase.java	2010-10-28 12:04:21 UTC (rev 2623)
@@ -2,6 +2,7 @@
 
 import org.infinispan.Cache;
 import org.infinispan.distribution.BaseDistFunctionalTest;
+import org.infinispan.distribution.MagicKey;
 import org.infinispan.test.TestingUtil;
 import org.testng.annotations.Test;
 
@@ -181,13 +182,13 @@
 class Updater extends Thread {
    static final Random r = new Random();
    volatile int currentValue = 0;
-   BaseDistFunctionalTest.MagicKey key;
+   MagicKey key;
    Cache cache;
    CountDownLatch latch;
    volatile boolean running = true;
    TransactionManager tm;
 
-   Updater(Cache cache, BaseDistFunctionalTest.MagicKey key, CountDownLatch latch, boolean tx) {
+   Updater(Cache cache, MagicKey key, CountDownLatch latch, boolean tx) {
       super("Updater-" + key);
       this.key = key;
       this.cache = cache;

Modified: branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/WorkDuringJoinTest.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/WorkDuringJoinTest.java	2010-10-27 21:00:52 UTC (rev 2622)
+++ branches/4.2.x/core/src/test/java/org/infinispan/distribution/rehash/WorkDuringJoinTest.java	2010-10-28 12:04:21 UTC (rev 2623)
@@ -2,6 +2,7 @@
 
 import org.infinispan.Cache;
 import org.infinispan.distribution.BaseDistFunctionalTest;
+import org.infinispan.distribution.MagicKey;
 import org.infinispan.distribution.ch.ConsistentHash;
 import org.infinispan.distribution.ch.ConsistentHashHelper;
 import org.infinispan.manager.EmbeddedCacheManager;

Modified: branches/4.2.x/core/src/test/java/org/infinispan/lock/APIDistTest.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/lock/APIDistTest.java	2010-10-27 21:00:52 UTC (rev 2622)
+++ branches/4.2.x/core/src/test/java/org/infinispan/lock/APIDistTest.java	2010-10-28 12:04:21 UTC (rev 2623)
@@ -1,17 +1,12 @@
 package org.infinispan.lock;
 
 import org.infinispan.Cache;
-import org.infinispan.affinity.KeyAffinityService;
-import org.infinispan.affinity.KeyAffinityServiceFactory;
-import org.infinispan.affinity.KeyAffinityServiceImpl;
-import org.infinispan.affinity.KeyGenerator;
 import org.infinispan.config.Configuration;
+import org.infinispan.distribution.MagicKey;
 import org.infinispan.manager.EmbeddedCacheManager;
 import org.infinispan.test.MultipleCacheManagersTest;
 import org.infinispan.test.fwk.CleanupAfterMethod;
 import org.infinispan.test.fwk.TestCacheManagerFactory;
-import org.infinispan.util.concurrent.TimeoutException;
-import org.infinispan.util.concurrent.WithinThreadExecutor;
 import org.testng.annotations.Test;
 
 import javax.transaction.HeuristicMixedException;
@@ -19,19 +14,13 @@
 import javax.transaction.NotSupportedException;
 import javax.transaction.RollbackException;
 import javax.transaction.SystemException;
-import javax.transaction.Transaction;
-import java.util.Arrays;
-import java.util.Random;
-import java.util.concurrent.Executors;
 
-import static org.infinispan.context.Flag.FAIL_SILENTLY;
 
-
 @Test(testName = "lock.APITest", groups = "functional")
 @CleanupAfterMethod
 public class APIDistTest extends MultipleCacheManagersTest {
    EmbeddedCacheManager cm1, cm2;
-   String key; // guaranteed to be mapped to cache2
+   MagicKey key; // guaranteed to be mapped to cache2
 
    @Override
    protected void createCacheManagers() throws Throwable {
@@ -45,24 +34,11 @@
       cm2 = TestCacheManagerFactory.createClusteredCacheManager(cfg);
       registerCacheManager(cm1, cm2);
       cm1.getCache();
-      Cache<String, String> c = cm2.getCache();
-
-      // lets generate a key such that it is always mapped to cache2.
-      KeyAffinityService<String> service = KeyAffinityServiceFactory.newKeyAffinityService(c,
-              Executors.newCachedThreadPool(), new KeyGenerator<String>() {
-                 final Random r = new Random();
-
-                 @Override
-                 public String getKey() {
-                    return Integer.toHexString(r.nextInt(2000));
-                 }
-              }, 2);
-
-      key = service.getKeyForAddress(c.getAdvancedCache().getRpcManager().getAddress());
+      key = new MagicKey(cm2.getCache(), "Key mapped to Cache2");
    }
 
    public void testLockAndGet() throws SystemException, NotSupportedException {
-      Cache<String, String> cache1 = cache(0), cache2 = cache(1);
+      Cache<MagicKey, String> cache1 = cache(0), cache2 = cache(1);
 
       cache1.put(key, "v");
 
@@ -76,7 +52,7 @@
    }
 
    public void testLockAndGetAndPut() throws SystemException, NotSupportedException, RollbackException, HeuristicRollbackException, HeuristicMixedException, InterruptedException {
-      Cache<String, String> cache1 = cache(0), cache2 = cache(1);
+      Cache<MagicKey, String> cache1 = cache(0), cache2 = cache(1);
 
       cache1.put(key, "v");
 
@@ -96,7 +72,7 @@
    }
 
    public void testLockAndPutRetval() throws SystemException, NotSupportedException, RollbackException, HeuristicRollbackException, HeuristicMixedException, InterruptedException {
-      Cache<String, String> cache1 = cache(0), cache2 = cache(1);
+      Cache<MagicKey, String> cache1 = cache(0), cache2 = cache(1);
 
       cache1.put(key, "v");
 
@@ -115,7 +91,7 @@
    }
 
    public void testLockAndRemoveRetval() throws SystemException, NotSupportedException, RollbackException, HeuristicRollbackException, HeuristicMixedException, InterruptedException {
-      Cache<String, String> cache1 = cache(0), cache2 = cache(1);
+      Cache<MagicKey, String> cache1 = cache(0), cache2 = cache(1);
 
       cache1.put(key, "v");
 
@@ -132,5 +108,4 @@
       assert (null == (val = cache1.get(key))) : "Could not find key " + key + " on cache1: expected null, was " + val;
       assert (null == (val = cache2.get(key))) : "Could not find key " + key + " on cache2: expected null, was " + val;
    }
-
 }



More information about the infinispan-commits mailing list