#IGNITE-99: Add 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/ef6bdbce Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/ef6bdbce Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/ef6bdbce Branch: refs/heads/sprint-1 Commit: ef6bdbce31cd734ff956e03f8258d18f39dea066 Parents: 5dbe651 Author: ivasilinets <ivasilin...@gridgain.com> Authored: Tue Jan 27 13:55:13 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Tue Jan 27 13:55:13 2015 +0300 ---------------------------------------------------------------------- .../ignite/IgniteCacheAffinityAbstractTest.java | 241 +++++++++++++++++++ .../apache/ignite/IgniteCacheAffinityTest.java | 201 ---------------- ...ePartitionedNearPartitionedAffinityTest.java | 18 ++ .../IgniteCachePartitionedOnlyAffinityTest.java | 18 ++ ...heReplicatedPartitionedOnlyAffinityTest.java | 18 ++ 5 files changed, 295 insertions(+), 201 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ef6bdbce/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityAbstractTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityAbstractTest.java new file mode 100644 index 0000000..aed7ea7 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityAbstractTest.java @@ -0,0 +1,241 @@ +package org.apache.ignite; + +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.affinity.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.processors.cache.*; + +import javax.cache.*; +import java.util.*; + +/** + * Tests for {@link org.apache.ignite.internal.processors.affinity.GridAffinityProcessor.CacheAffinityProxy}. + */ +public abstract class IgniteCacheAffinityAbstractTest extends IgniteCacheAbstractTest { + /** Initial grid count. */ + private int GRID_COUNT = 3; + + /** Cache name */ + private final String CACHE1 = "cache1"; + + /** {@inheritDoc} */ + @Override protected int gridCount() { + return GRID_COUNT; + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + CacheConfiguration cache0 = cacheConfiguration(null); + + CacheConfiguration cache1 = cacheConfiguration(null); + cache1.setName(CACHE1); + + if (gridName.contains("0")) { + cfg.setCacheConfiguration(); + } + else if (gridName.contains("1")) { + cfg.setCacheConfiguration(cache0); + } + else { + cfg.setCacheConfiguration(cache0, cache1); + } + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected CacheAtomicityMode atomicityMode() { + return CacheAtomicityMode.TRANSACTIONAL; + } + + /** + * Throws Exception if failed. + */ + public void testAffinity() throws Exception { + if (cacheMode().equals(CacheMode.LOCAL)) { + info("Test is not applied for local cache."); + + return; + } + + GridCache<String, Integer> cache0 = grid(1).cache(null); + GridCache<String, Integer> cache1 = grid(2).cache(CACHE1); + + for (int i = 0; i < 10; ++i) + cache0.put(Integer.toString(i), i); + + for (int i = 10; i < 20; ++i) + cache1.put(Integer.toString(i), i); + + checkAffinity(); + + startGrid(gridCount()); + startGrid(gridCount() + 1); + + GRID_COUNT += 2; + + checkAffinity(); + } + + /** + * Check CacheAffinityProxy methods. + */ + private void checkAffinity() { + for (int i = 0; i < gridCount(); ++i) { + if (grid(i).cachex(null) != null) + checkAffinity(grid(i).<String, Integer>jcache(null), grid(i).<String, Integer>cache(null)); + + if (grid(i).cachex(CACHE1) != null) + checkAffinity(grid(i).<String, Integer>jcache(CACHE1), grid(i).<String, Integer>cache(CACHE1)); + } + } + + /** + * @param jcache Jcache to iterate over. + * @param cache Cache to check. + */ + private void checkAffinity(IgniteCache<String, Integer> jcache, GridCache<String, Integer> cache) { + for (int i = 0; i < gridCount(); ++i) + checkGridAffinity(grid(i).<String>affinity(cache.name()), jcache, cache); + } + + /** + * @param testAff Cache affinity to test. + * @param jcache Ignite cache. + * @param cache Cache. + */ + private void checkGridAffinity(CacheAffinity<String> testAff, IgniteCache<String, Integer> jcache, + GridCache<String, Integer> cache) { + checkAffinityKey(testAff, jcache, cache.affinity()); + + checkPartitions(testAff, cache.affinity()); + + checkIsBackupOrPrimary(testAff, jcache, cache.affinity()); + + checkMapKeyToNode(testAff, jcache, cache.affinity()); + } + + /** + * Check mapKeyToNode, mapKeyToPrimaryAndBackups and mapPartitionToNode methods. + */ + private void checkMapKeyToNode(CacheAffinity<String> testAff, + IgniteCache<String, Integer> jcache, CacheAffinity<String> aff) { + Iterator<Cache.Entry<String, Integer>> iter = jcache.iterator(); + + while (iter.hasNext()) { + Cache.Entry<String, Integer> 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()); + + checkEqualCollection(nodes1, nodes2); + + int part = aff.partition(entry.getKey()); + + assertEquals(testAff.mapPartitionToNode(part).id(), aff.mapPartitionToNode(part).id()); + } + } + + /** + * Check affinityKey method. + */ + private void checkAffinityKey(CacheAffinity<String> testAff, + IgniteCache<String, Integer> jcache, CacheAffinity<String> aff) { + Iterator<Cache.Entry<String, Integer>> iter = jcache.iterator(); + + while (iter.hasNext()) { + Cache.Entry<String, Integer> entry = iter.next(); + + assertEquals(testAff.affinityKey(entry.getKey()), (aff.affinityKey(entry.getKey()))); + } + } + + /** + * Check isBackup, isPrimary and isPrimaryOrBackup methods. + */ + private void checkIsBackupOrPrimary(CacheAffinity<String> testAff, IgniteCache<String, Integer> jcache, + CacheAffinity<String> aff) { + + Iterator<Cache.Entry<String, Integer>> iter = jcache.iterator(); + + while (iter.hasNext()) { + Cache.Entry<String, Integer> entry = iter.next(); + + for (ClusterNode n : nodes()) { + assertEquals(testAff.isBackup(n, entry.getKey()), aff.isBackup(n, entry.getKey())); + + assertEquals(testAff.isPrimary(n, entry.getKey()), aff.isPrimary(n, entry.getKey())); + + assertEquals(testAff.isPrimaryOrBackup(n, entry.getKey()), aff.isPrimaryOrBackup(n, entry.getKey())); + } + } + } + + /** + * Check allPartitions, backupPartitions and primaryPartitions methods. + */ + private void checkPartitions(CacheAffinity<String> testAff, CacheAffinity<String> aff) { + for (ClusterNode n : nodes()) { + checkEqualIntArray(testAff.allPartitions(n), aff.allPartitions(n)); + + checkEqualIntArray(testAff.backupPartitions(n), aff.backupPartitions(n)); + + checkEqualIntArray(testAff.primaryPartitions(n), aff.primaryPartitions(n)); + } + } + + /** + * Check equal arrays. + */ + private void checkEqualIntArray(int[] arr1, int[] arr2) { + assertEquals(arr1.length, arr2.length); + + Collection<Integer> col1 = new HashSet<>(); + + for (int i = 0; i < arr1.length; ++i) + col1.add(arr1[i]); + + for (int i = 0; i < arr2.length; ++i) { + assertTrue(col1.contains(arr2[i])); + + col1.remove(arr2[i]); + } + + assertEquals(0, col1.size()); + + } + + /** + * Check equal collections. + */ + private void checkEqualCollection(Collection<ClusterNode> col1, Collection<ClusterNode> col2) { + Collection<ClusterNode> colCopy1 = new HashSet<>(col1); + + for (ClusterNode node : col2) { + assertTrue(colCopy1.contains(node)); + colCopy1.remove(node); + } + + assertEquals(0, colCopy1.size()); + } + + /** + * @return Cluster nodes. + */ + Collection<ClusterNode> nodes() { + Set<ClusterNode> nodes = new HashSet<>(); + + for (int i = 0; i < gridCount(); ++i) + nodes.addAll(grid(i).nodes()); + + return nodes; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ef6bdbce/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 deleted file mode 100644 index 3e83cdc..0000000 --- a/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityTest.java +++ /dev/null @@ -1,201 +0,0 @@ -package org.apache.ignite; - -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.affinity.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.processors.cache.*; - -import javax.cache.*; -import java.util.*; - -/** - * Tests affinity cache. - */ -public class IgniteCacheAffinityTest extends IgniteCacheAbstractTest { - private final int GRID_COUNT = 4; - private final String CACHE1 = "cache1"; - private final String CACHE2 = "cache2"; - - - @Override - protected int gridCount() { - return GRID_COUNT; - } - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration cfg = super.getConfiguration(gridName); - - CacheConfiguration cache0 = cacheConfiguration(null); - - CacheConfiguration cache1 = cacheConfiguration(null); - cache1.setName(CACHE1); - - CacheConfiguration cache2 = cacheConfiguration(null); - cache2.setName(CACHE2); - - - if (gridName.contains("0")) { - cfg.setCacheConfiguration(cache0); - } - else if (gridName.contains("1")) { - cfg.setCacheConfiguration(cache0, cache1); - } - else if (gridName.contains("2")) { - cfg.setCacheConfiguration(); - } - else { - cfg.setCacheConfiguration(cache0, cache1, cache2); - } - return cfg; - } - - @Override protected CacheMode cacheMode() { - return CacheMode.PARTITIONED; - } - - @Override protected CacheAtomicityMode atomicityMode() { - return CacheAtomicityMode.TRANSACTIONAL; - } - - @Override protected CacheDistributionMode distributionMode() { - return CacheDistributionMode.PARTITIONED_ONLY; - } - - public void testAffinity() throws Exception { - - GridCache<String, Integer> cache0 = grid(0).cache(null); - GridCache<String, Integer> cache1 = grid(1).cache(CACHE1); - GridCache<String, Integer> cache2 = grid(3).cache(CACHE2); - - cache0.affinity(); - - for (int i = 0; i < 10; ++i) - cache0.put(Integer.toString(i), i); - - for (int i = 10; i < 20; ++i) - cache1.put(Integer.toString(i), i); - - for (int i = 20; i < 30; ++i) - cache2.put(Integer.toString(i), i); - - checkAffinity(gridCount()); - - startGrid(4); - - startGrid(5); - - checkAffinity(6); - - } - - private void checkAffinity(int n) { - for (int i = 0; i < n; ++i) { - if (grid(i).cachex(null) != null) - checkAffinity(i, grid(i).<String, Integer>cache(null)); - - if (grid(i).cachex(CACHE1) != null) - checkAffinity(i, grid(i).<String, Integer>cache(null)); - - if (grid(i).cachex(CACHE2) != null) - checkAffinity(i, grid(i).<String, Integer>cache(null)); - } - } - - private void checkAffinity(int idx, GridCache<String, Integer> cache) { - for (int i = 0; i < gridCount(); ++i) - checkGridAffinity(grid(i), idx, cache); - } - - private void checkGridAffinity(Ignite ignite, int idx, GridCache<String, Integer> cache) { - IgniteCache<String, Integer> jcache = grid(idx).jcache(cache.name()); - checkAffinityKey(ignite, jcache, cache.affinity()); - checkPartitions(ignite, cache.name(), cache.affinity()); - checkIsBackupOrPrimary(ignite, jcache, cache.affinity()); - checkMapKeyToNode(ignite, jcache, cache.affinity()); - } - - private void checkMapKeyToNode(Ignite ignite, IgniteCache<String, Integer> jcache, CacheAffinity<String> aff) { - CacheAffinity<String> igniteAff = ignite.affinity(jcache.getName()); - - Iterator<Cache.Entry<String, Integer>> iter = jcache.iterator(); - - - - while (iter.hasNext()) { - Cache.Entry<String, Integer> entry = iter.next(); - UUID node1 = igniteAff.mapKeyToNode(entry.getKey()).id(); - UUID node2 = aff.mapKeyToNode(entry.getKey()).id(); - assertEquals(node1, node2); - - Collection<ClusterNode> nodes1 = igniteAff.mapKeyToPrimaryAndBackups(entry.getKey()); - Collection<ClusterNode> nodes2 = aff.mapKeyToPrimaryAndBackups(entry.getKey()); - checkEqualCollection(nodes1, nodes2); - } - } - - - - - private void checkAffinityKey(Ignite ignite, IgniteCache<String, Integer> jcache, CacheAffinity<String> aff) { - CacheAffinity<String> igniteAff = ignite.affinity(jcache.getName()); - - Iterator<Cache.Entry<String, Integer>> iter = jcache.iterator(); - while (iter.hasNext()) { - Cache.Entry<String, Integer> entry = iter.next(); - assertEquals(igniteAff.affinityKey(entry.getKey()), (aff.affinityKey(entry.getKey()))); - } - } - - private void checkIsBackupOrPrimary(Ignite ignite, IgniteCache<String, Integer> jcache, CacheAffinity<String> aff) { - CacheAffinity<String> igniteAff = ignite.affinity(jcache.getName()); - - Iterator<Cache.Entry<String, Integer>> iter = jcache.iterator(); - while (iter.hasNext()) { - Cache.Entry<String, Integer> entry = iter.next(); - for (ClusterNode n : ignite.cluster().nodes()) { - assertEquals(igniteAff.isBackup(n, entry.getKey()), aff.isBackup(n, entry.getKey())); - assertEquals(igniteAff.isPrimary(n, entry.getKey()), aff.isPrimary(n, entry.getKey())); - assertEquals(igniteAff.isPrimaryOrBackup(n, entry.getKey()), aff.isPrimaryOrBackup(n, entry.getKey())); - } - } - - } - - private void checkPartitions(Ignite ignite, String cacheName, CacheAffinity<String> aff) { - for (ClusterNode n : ignite.cluster().nodes()) { - checkEqualIntArray(ignite.affinity(cacheName).allPartitions(n), aff.allPartitions(n)); - checkEqualIntArray(ignite.affinity(cacheName).backupPartitions(n), aff.backupPartitions(n)); - checkEqualIntArray(ignite.affinity(cacheName).primaryPartitions(n), aff.primaryPartitions(n)); - } - } - - private void checkEqualIntArray(int[] arr1, int[] arr2) { - assertEquals(arr1.length, arr2.length); - - Collection<Integer> col1 = new HashSet<>(); - - for (int i = 0; i < arr1.length; ++i) - col1.add(arr1[i]); - - for (int i = 0; i < arr2.length; ++i) { - assertTrue(col1.contains(arr2[i])); - - col1.remove(arr2[i]); - } - - assertEquals(0, col1.size()); - - } - - private void checkEqualCollection(Collection<ClusterNode> col1, Collection<ClusterNode> col2) { - Collection<ClusterNode> colCopy1 = new HashSet<>(col1); - for (ClusterNode node : col2) { - assertTrue(colCopy1.contains(node)); - colCopy1.remove(node); - } - - assertEquals(0, colCopy1.size()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ef6bdbce/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedNearPartitionedAffinityTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedNearPartitionedAffinityTest.java b/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedNearPartitionedAffinityTest.java new file mode 100644 index 0000000..310eb04 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedNearPartitionedAffinityTest.java @@ -0,0 +1,18 @@ +package org.apache.ignite; + +import org.apache.ignite.cache.*; + +/** + * Tests for {@link org.apache.ignite.internal.processors.affinity.GridAffinityProcessor.CacheAffinityProxy}. + */ +public class IgniteCachePartitionedNearPartitionedAffinityTest extends IgniteCacheAffinityAbstractTest { + /** {@inheritDoc} */ + @Override protected CacheMode cacheMode() { + return CacheMode.PARTITIONED; + } + + /** {@inheritDoc} */ + @Override protected CacheDistributionMode distributionMode() { + return CacheDistributionMode.NEAR_PARTITIONED; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ef6bdbce/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedOnlyAffinityTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedOnlyAffinityTest.java b/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedOnlyAffinityTest.java new file mode 100644 index 0000000..b9fac92 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedOnlyAffinityTest.java @@ -0,0 +1,18 @@ +package org.apache.ignite; + +import org.apache.ignite.cache.*; + +/** + * Tests for {@link org.apache.ignite.internal.processors.affinity.GridAffinityProcessor.CacheAffinityProxy}. + */ +public class IgniteCachePartitionedOnlyAffinityTest extends IgniteCacheAffinityAbstractTest { + /** {@inheritDoc} */ + @Override protected CacheMode cacheMode() { + return CacheMode.PARTITIONED; + } + + /** {@inheritDoc} */ + @Override protected CacheDistributionMode distributionMode() { + return CacheDistributionMode.PARTITIONED_ONLY; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ef6bdbce/modules/core/src/test/java/org/apache/ignite/IgniteCacheReplicatedPartitionedOnlyAffinityTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/IgniteCacheReplicatedPartitionedOnlyAffinityTest.java b/modules/core/src/test/java/org/apache/ignite/IgniteCacheReplicatedPartitionedOnlyAffinityTest.java new file mode 100644 index 0000000..efdb8bc --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/IgniteCacheReplicatedPartitionedOnlyAffinityTest.java @@ -0,0 +1,18 @@ +package org.apache.ignite; + +import org.apache.ignite.cache.*; + +/** + * Tests for {@link org.apache.ignite.internal.processors.affinity.GridAffinityProcessor.CacheAffinityProxy}. + */ +public class IgniteCacheReplicatedPartitionedOnlyAffinityTest extends IgniteCacheAffinityAbstractTest{ + /** {@inheritDoc} */ + @Override protected CacheMode cacheMode() { + return CacheMode.REPLICATED; + } + + /** {@inheritDoc} */ + @Override protected CacheDistributionMode distributionMode() { + return CacheDistributionMode.PARTITIONED_ONLY; + } +}