#IGNITE-99: New tests

Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/8c4a1e2a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/8c4a1e2a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/8c4a1e2a

Branch: refs/heads/ignite-113
Commit: 8c4a1e2a7c4ec9c96d5d12200b565bbce65270de
Parents: 1ec0c2d
Author: ivasilinets <ivasilin...@gridgain.com>
Authored: Tue Jan 27 18:50:54 2015 +0300
Committer: ivasilinets <ivasilin...@gridgain.com>
Committed: Tue Jan 27 18:50:54 2015 +0300

----------------------------------------------------------------------
 .../affinity/GridAffinityProcessor.java         |  15 +-
 .../apache/ignite/IgniteCacheAffinityTest.java  | 168 ++++++++++---------
 2 files changed, 100 insertions(+), 83 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/8c4a1e2a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java
index bad1f59..ecc94df 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java
@@ -498,7 +498,7 @@ public class GridAffinityProcessor extends 
GridProcessorAdapter {
         /**
          * @return Cache affinity function.
          */
-        private CacheAffinityFunction affFunc() {
+        private CacheAffinityFunction affinityFunction() {
             return affFunc;
         }
 
@@ -509,6 +509,10 @@ public class GridAffinityProcessor extends 
GridProcessorAdapter {
             return assignment;
         }
 
+        private CacheAffinityKeyMapper keyMapper() {
+            return mapper;
+        }
+
         /** {@inheritDoc} */
         @Override public String toString() {
             return S.toString(AffinityInfo.class, this);
@@ -581,7 +585,7 @@ public class GridAffinityProcessor extends 
GridProcessorAdapter {
             ctx.gateway().readLock();
 
             try {
-                return cache().affFunc().partitions();
+                return cache().affinityFunction().partitions();
             }
             catch (IgniteCheckedException e) {
                 throw new IgniteException(e);
@@ -596,7 +600,7 @@ public class GridAffinityProcessor extends 
GridProcessorAdapter {
             ctx.gateway().readLock();
 
             try {
-                return cache().affFunc().partition(key);
+                return cache().affinityFunction().partition(key);
             }
             catch (IgniteCheckedException e) {
                 throw new IgniteException(e);
@@ -707,7 +711,7 @@ public class GridAffinityProcessor extends 
GridProcessorAdapter {
             ctx.gateway().readLock();
 
             try {
-                return cache().mapper.affinityKey(key);
+                return cache().keyMapper().affinityKey(key);
             }
             catch (IgniteCheckedException e) {
                 throw new IgniteException(e);
@@ -737,7 +741,6 @@ public class GridAffinityProcessor extends 
GridProcessorAdapter {
             ctx.gateway().readLock();
 
             try {
-                //return affinityCache(cacheName, 
topologyVersion()).assignment.get()
                 return GridAffinityProcessor.this.mapKeyToNode(cacheName, key);
             }
             catch (IgniteCheckedException e) {
@@ -753,7 +756,7 @@ public class GridAffinityProcessor extends 
GridProcessorAdapter {
             ctx.gateway().readLock();
 
             try {
-                return cache().assignment.get(partition(key));
+                return cache().assignment().get(partition(key));
             }
             catch (IgniteCheckedException e) {
                 throw new IgniteException(e);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/8c4a1e2a/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityTest.java 
b/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityTest.java
index 5989245..19ddc75 100644
--- a/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityTest.java
@@ -9,7 +9,6 @@ import org.apache.ignite.cluster.*;
 import org.apache.ignite.configuration.*;
 import org.apache.ignite.internal.processors.cache.*;
 
-import javax.cache.*;
 import java.util.*;
 
 import static org.apache.ignite.cache.CacheAtomicityMode.*;
@@ -98,109 +97,107 @@ public class IgniteCacheAffinityTest extends 
IgniteCacheAbstractTest {
      * Check CacheAffinityProxy methods.
      */
     private void checkAffinity() {
-        checkGridAffinity(grid(0).affinity(null), grid(1).jcache(null), 
grid(1).cache(null));
-
-        checkGridAffinity(grid(0).affinity(CACHE1), grid(1).jcache(CACHE1), 
grid(1).cache(CACHE1));
-
-        checkGridAffinity(grid(0).affinity(CACHE2), grid(1).jcache(CACHE2), 
grid(1).cache(CACHE2));
-
-        checkGridAffinity(grid(0).affinity(CACHE3), grid(1).jcache(CACHE3), 
grid(1).cache(CACHE3));
+        checkAffinity(grid(0).affinity(null), grid(1).cache(null).affinity());
+        checkAffinity(grid(0).affinity(CACHE1), 
grid(1).cache(CACHE1).affinity());
+        checkAffinity(grid(0).affinity(CACHE2), 
grid(1).cache(CACHE2).affinity());
+        checkAffinity(grid(0).affinity(CACHE3), 
grid(1).cache(CACHE3).affinity());
     }
 
     /**
      * @param testAff Cache affinity to test.
-     * @param jcache Ignite cache.
-     * @param cache Cache.
+     * @param aff Cache affinity.
      */
-    private void checkGridAffinity(CacheAffinity testAff, IgniteCache jcache, 
GridCache cache) {
-        checkAffinityKey(testAff, jcache, cache.affinity());
-
-        checkPartitions(testAff, cache.affinity());
-
-        checkIsBackupOrPrimary(testAff, jcache, cache.affinity());
-
-        checkMapKeyToNode(testAff, jcache, cache.affinity());
+    private void checkAffinity(CacheAffinity testAff, CacheAffinity aff) {
+        checkAffinityKey(testAff, aff);
+        checkPartitions(testAff, aff);
+        checkIsBackupOrPrimary(testAff, aff);
+        checkMapKeyToNode(testAff, aff);
+        checkMapKeysToNodes(testAff, aff);
+        checkMapPartitionsToNodes(testAff, aff);
     }
 
     /**
-     * Check mapKeyToNode, mapKeyToPrimaryAndBackups and mapPartitionToNode 
methods.
+     * Check affinityKey method.
      */
-    private void checkMapKeyToNode(CacheAffinity testAff, IgniteCache jcache, 
CacheAffinity aff) {
-        for (int i = 0; i < 10000; i++) {
-
-        }
-        
-        Iterator<Cache.Entry> iter = jcache.iterator();
-
-        while (iter.hasNext()) {
-            Cache.Entry entry = iter.next();
-
-            UUID node1 = testAff.mapKeyToNode(entry.getKey()).id();
-            UUID node2 = aff.mapKeyToNode(entry.getKey()).id();
-
-            assertEquals(node1, node2);
-
-            Collection<ClusterNode> nodes1 = 
testAff.mapKeyToPrimaryAndBackups(entry.getKey());
-            Collection<ClusterNode> nodes2 = 
aff.mapKeyToPrimaryAndBackups(entry.getKey());
+    private void checkAffinityKey(CacheAffinity testAff,CacheAffinity aff) {
+        for (int i = 0; i < 10000; i++)
+            assertEquals(testAff.affinityKey(i), aff.affinityKey(i));
+    }
 
-            checkEqualCollection(nodes1, nodes2);
+    /**
+     * Check allPartitions, backupPartitions and primaryPartitions methods.
+     */
+    private void checkPartitions(CacheAffinity testAff, CacheAffinity aff) {
+        for (ClusterNode n : nodes()) {
+            checkEqualIntArray(testAff.allPartitions(n), aff.allPartitions(n));
 
-            int part = aff.partition(entry.getKey());
+            checkEqualIntArray(testAff.backupPartitions(n), 
aff.backupPartitions(n));
 
-            assertEquals(testAff.mapPartitionToNode(part).id(), 
aff.mapPartitionToNode(part).id());
+            checkEqualIntArray(testAff.primaryPartitions(n), 
aff.primaryPartitions(n));
         }
     }
 
     /**
-     * Check affinityKey method.
+     * Check isBackup, isPrimary and isPrimaryOrBackup methods.
      */
-    private void checkAffinityKey(CacheAffinity testAff, IgniteCache jcache, 
CacheAffinity aff) {
-        Iterator<Cache.Entry> iter = jcache.iterator();
+    private void checkIsBackupOrPrimary(CacheAffinity testAff, CacheAffinity 
aff) {
+        for (int i = 0; i < 10000; i++)
+            for (ClusterNode n : nodes()) {
+                assertEquals(testAff.isBackup(n, i), aff.isBackup(n, i));
 
-        while (iter.hasNext()) {
-            Cache.Entry entry = iter.next();
+                assertEquals(testAff.isPrimary(n, i), aff.isPrimary(n, i));
 
-            assertEquals(testAff.affinityKey(entry.getKey()), 
(aff.affinityKey(entry.getKey())));
-        }
+                assertEquals(testAff.isPrimaryOrBackup(n, i), 
aff.isPrimaryOrBackup(n, i));
+            }
     }
 
     /**
-     * Check isBackup, isPrimary and isPrimaryOrBackup methods.
+     * Check mapKeyToNode, mapKeyToPrimaryAndBackups, 
mapPartitionToPrimaryAndBackups and mapPartitionToNode methods.
      */
-    private void checkIsBackupOrPrimary(CacheAffinity testAff, IgniteCache 
jcache, CacheAffinity aff) {
-
-        Iterator<Cache.Entry> iter = jcache.iterator();
+    private void checkMapKeyToNode(CacheAffinity testAff, CacheAffinity aff) {
+        for (int i = 0; i < 10000; i++) {
+            assertEquals(testAff.mapKeyToNode(i).id(), 
aff.mapKeyToNode(i).id());
 
-        while (iter.hasNext()) {
-            Cache.Entry entry = iter.next();
+            checkEqualCollection(testAff.mapKeyToPrimaryAndBackups(i), 
aff.mapKeyToPrimaryAndBackups(i));
+        }
 
-            for (ClusterNode n : nodes()) {
-                assertEquals(testAff.isBackup(n, entry.getKey()), 
aff.isBackup(n, entry.getKey()));
+        assertEquals(aff.partitions(), testAff.partitions());
 
-                assertEquals(testAff.isPrimary(n, entry.getKey()), 
aff.isPrimary(n, entry.getKey()));
+        for (int part = 0; part < aff.partitions(); ++part) {
+            assertEquals(testAff.mapPartitionToNode(part).id(), 
aff.mapPartitionToNode(part).id());
 
-                assertEquals(testAff.isPrimaryOrBackup(n, entry.getKey()), 
aff.isPrimaryOrBackup(n, entry.getKey()));
-            }
+            
checkEqualCollection(testAff.mapPartitionToPrimaryAndBackups(part), 
aff.mapKeyToPrimaryAndBackups(part));
         }
     }
 
     /**
-     * Check allPartitions, backupPartitions and primaryPartitions methods.
+     * Check mapKeysToNodes methods.
      */
-    private void checkPartitions(CacheAffinity testAff, CacheAffinity aff) {
-        for (ClusterNode n : nodes()) {
-            checkEqualIntArray(testAff.allPartitions(n), aff.allPartitions(n));
+    private void checkMapKeysToNodes(CacheAffinity testAff, CacheAffinity aff) 
{
+        List<Integer> keys = new ArrayList<>(10000);
 
-            checkEqualIntArray(testAff.backupPartitions(n), 
aff.backupPartitions(n));
+        for (int i = 0; i < 10000; ++i)
+            keys.add(i);
 
-            checkEqualIntArray(testAff.primaryPartitions(n), 
aff.primaryPartitions(n));
-        }
+        checkEqualMaps(testAff.mapKeysToNodes(keys), aff.mapKeysToNodes(keys));
+    }
+
+    /**
+     * Check mapPartitionsToNodes methods.
+     */
+    private void checkMapPartitionsToNodes(CacheAffinity testAff, 
CacheAffinity aff) {
+        List<Integer> parts = new ArrayList<>(aff.partitions());
+
+        for (int i = 0; i < aff.partitions(); ++i)
+            parts.add(i);
+
+        checkEqualPartitionMaps(testAff.mapPartitionsToNodes(parts), 
aff.mapPartitionsToNodes(parts));
     }
 
     /**
      * Check equal arrays.
      */
-    private void checkEqualIntArray(int[] arr1, int[] arr2) {
+    private static void checkEqualIntArray(int[] arr1, int[] arr2) {
         assertEquals(arr1.length, arr2.length);
 
         Collection<Integer> col1 = new HashSet<>();
@@ -220,26 +217,43 @@ public class IgniteCacheAffinityTest extends 
IgniteCacheAbstractTest {
     /**
      * Check equal collections.
      */
-    private void checkEqualCollection(Collection<ClusterNode> col1, 
Collection<ClusterNode> col2) {
-        Collection<ClusterNode> colCopy1 = new HashSet<>(col1);
+    private static void checkEqualCollection(Collection<ClusterNode> col1, 
Collection<ClusterNode> col2) {
+        assertEquals(col1.size(), col2.size());
+
+        for (ClusterNode node : col1)
+            assertTrue(col2.contains(node));
+    }
+
+    /**
+     * Check equal maps.
+     */
+    private static void checkEqualMaps(Map<ClusterNode, Collection> map1, 
Map<ClusterNode, Collection> map2) {
+        assertEquals(map1.size(), map2.size());
 
-        for (ClusterNode node : col2) {
-            assertTrue(colCopy1.contains(node));
-            colCopy1.remove(node);
+        for (ClusterNode node : map1.keySet()) {
+            assertTrue(map2.containsKey(node));
+
+            assertEquals(map1.get(node).size(), map2.get(node).size());
         }
+    }
 
-        assertEquals(0, colCopy1.size());
+    /**
+     * Check equal maps.
+     */
+    private static void checkEqualPartitionMaps(Map<Integer, ClusterNode> 
map1, Map<Integer, ClusterNode> map2) {
+        assertEquals(map1.size(), map2.size());
+
+        for (Integer i : map1.keySet()) {
+            assertTrue(map2.containsKey(i));
+
+            assertEquals(map1.get(i), map2.get(i));
+        }
     }
 
     /**
      * @return Cluster nodes.
      */
     private Collection<ClusterNode> nodes() {
-        Set<ClusterNode> nodes = new HashSet<>();
-
-        for (int i = 0; i < gridCount(); ++i)
-            nodes.addAll(grid(i).nodes());
-
-        return nodes;
+        return grid(0).nodes();
     }
 }

Reply via email to