#ignite-1087: 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/17b0fb7b Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/17b0fb7b Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/17b0fb7b Branch: refs/heads/ignite-1087 Commit: 17b0fb7be896b365225cdbad181ab9f46c362c18 Parents: fc793cd Author: ivasilinets <ivasilin...@gridgain.com> Authored: Mon Jul 13 15:19:55 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Mon Jul 13 15:19:55 2015 +0300 ---------------------------------------------------------------------- .../cache/CacheAffinityCallSelfTest.java | 113 +++++++++++++++++++ .../cache/GridCacheAffinityRoutingSelfTest.java | 77 +++++++++++-- .../ignite/testsuites/IgniteCacheTestSuite.java | 1 + 3 files changed, 179 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/17b0fb7b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheAffinityCallSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheAffinityCallSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheAffinityCallSelfTest.java new file mode 100644 index 0000000..a8a838e --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheAffinityCallSelfTest.java @@ -0,0 +1,113 @@ +package org.apache.ignite.internal.processors.cache; + +import org.apache.ignite.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.resources.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; +import org.apache.ignite.spi.failover.always.*; +import org.apache.ignite.testframework.*; +import org.apache.ignite.testframework.junits.common.*; + +import java.util.concurrent.*; + +import static org.apache.ignite.cache.CacheMode.*; + +/** + * Test for {@link IgniteCompute#affinityCall(String, Object, IgniteCallable)} and + * {@link IgniteCompute#affinityRun(String, Object, IgniteRunnable)}. + */ +public class CacheAffinityCallSelfTest extends GridCommonAbstractTest { + /** */ + private static final String CACHE_NAME = "myCache"; + + /** */ + private static final int MAX_FAILOVER_ATTEMPTS = 5; + + /** */ + private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + TcpDiscoverySpi spi = new TcpDiscoverySpi(); + + spi.setIpFinder(IP_FINDER); + + cfg.setDiscoverySpi(spi); + + AlwaysFailoverSpi failSpi = new AlwaysFailoverSpi(); + failSpi.setMaximumFailoverAttempts(MAX_FAILOVER_ATTEMPTS); + cfg.setFailoverSpi(failSpi); + + CacheConfiguration ccfg = defaultCacheConfiguration(); + ccfg.setName(CACHE_NAME); + ccfg.setCacheMode(PARTITIONED); + ccfg.setBackups(1); + + cfg.setCacheConfiguration(ccfg); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + } + + /** + * @throws Exception If failed. + */ + public void testAffinityCallRestartNode() throws Exception { + startGrids(4); + + Integer key = primaryKey(grid(0).cache(CACHE_NAME)); + + IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() { + @Override public Object call() throws Exception { + U.sleep(500); + stopGrid(0); + + return null; + } + }); + + while (!fut.isDone()) + grid(1).compute().affinityCall(CACHE_NAME, key, new CheckCallable(key)); + + stopAllGrids(); + } + + /** + * Test callable. + */ + public static class CheckCallable implements IgniteCallable<Object> { + /** Key. */ + private final Object key; + + /** */ + @IgniteInstanceResource + private Ignite ignite; + + /** + * @param key Key. + */ + public CheckCallable(Object key) { + this.key = key; + } + + /** {@inheritDoc} */ + @Override public Object call() throws IgniteCheckedException { + assert ignite.cluster().localNode().id().equals(ignite.cluster().mapKeyToNode(null, key).id()); + + return null; + } + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/17b0fb7b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityRoutingSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityRoutingSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityRoutingSelfTest.java index b5cdc08..a56ab9f 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityRoutingSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityRoutingSelfTest.java @@ -22,9 +22,7 @@ import org.apache.ignite.cache.affinity.*; import org.apache.ignite.cluster.*; import org.apache.ignite.compute.*; import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.*; import org.apache.ignite.internal.util.typedef.*; -import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.lang.*; import org.apache.ignite.resources.*; import org.apache.ignite.spi.discovery.tcp.*; @@ -166,20 +164,21 @@ public class GridCacheAffinityRoutingSelfTest extends GridCommonAbstractTest { /** * @throws Exception If failed. */ - public void testAffinityCallRestartNode() throws Exception { - Integer key = primaryKey(grid(0).cache(NON_DFLT_CACHE_NAME)); - - IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() { + public void testAffinityRunRestartFails() throws Exception { + GridTestUtils.assertThrows(log, new Callable<Object>() { @Override public Object call() throws Exception { - U.sleep(500); - stopGrid(0); - + grid(0).compute().affinityRun(NON_DFLT_CACHE_NAME, "key", + new FailedRunnable("key", MAX_FAILOVER_ATTEMPTS + 1)); return null; } - }); + }, ClusterTopologyException.class, "Failed to failover a job to another node"); + } - while (!fut.isDone()) - grid(1).compute().affinityCall(NON_DFLT_CACHE_NAME, key, new CheckCallable(key, key)); + /** + * @throws Exception If failed. + */ + public void testAffinityRunRestart() throws Exception { + grid(0).compute().affinityRun(NON_DFLT_CACHE_NAME, "key", new FailedRunnable("key", MAX_FAILOVER_ATTEMPTS)); } /** @@ -282,6 +281,9 @@ public class GridCacheAffinityRoutingSelfTest extends GridCommonAbstractTest { */ private static class FailedCallable implements IgniteCallable<Object> { /** */ + private static final long serialVersionUID = 0L; + + /** */ private static final String ATTR_ATTEMPT = "Attempt"; /** */ @@ -326,6 +328,57 @@ public class GridCacheAffinityRoutingSelfTest extends GridCommonAbstractTest { } /** + * Test runnable. + */ + private static class FailedRunnable implements IgniteRunnable { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private static final String ATTR_ATTEMPT = "Attempt"; + + /** */ + @IgniteInstanceResource + private Ignite ignite; + + /** */ + @JobContextResource + private ComputeJobContext jobCtx; + + /** Key. */ + private final Object key; + + /** Call attempts. */ + private final Integer callAttempt; + + /** + * @param key Key. + * @param callAttempt Call attempts. + */ + public FailedRunnable(Object key, Integer callAttempt) { + this.key = key; + this.callAttempt = callAttempt; + } + + /** {@inheritDoc} */ + @Override public void run() { + Integer attempt = jobCtx.getAttribute(ATTR_ATTEMPT); + + if (attempt == null) + attempt = 1; + + assertEquals(ignite.affinity(NON_DFLT_CACHE_NAME).mapKeyToNode(key), ignite.cluster().localNode()); + + jobCtx.setAttribute(ATTR_ATTEMPT, attempt + 1); + + if (attempt < callAttempt) + throw new ComputeJobFailoverException("Failover exception."); + else + assertEquals(callAttempt, attempt); + } + } + + /** * Test callable. */ private static class CheckCallable implements IgniteCallable<Object> { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/17b0fb7b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java index 39702a3..bafdfef 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java @@ -109,6 +109,7 @@ public class IgniteCacheTestSuite extends TestSuite { // Common tests. suite.addTestSuite(GridCacheConcurrentMapSelfTest.class); suite.addTestSuite(GridCacheAffinityMapperSelfTest.class); + suite.addTestSuite(CacheAffinityCallSelfTest.class); GridTestUtils.addTestIfNeeded(suite, GridCacheAffinityRoutingSelfTest.class, ignoredTests); GridTestUtils.addTestIfNeeded(suite, GridCacheMvccSelfTest.class, ignoredTests); suite.addTestSuite(GridCacheMvccPartitionedSelfTest.class);