http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/349d51ed/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractDataStructuresFailoverSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractDataStructuresFailoverSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractDataStructuresFailoverSelfTest.java index 37c4d16..8e6cfc9 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractDataStructuresFailoverSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractDataStructuresFailoverSelfTest.java @@ -20,28 +20,22 @@ package org.apache.ignite.internal.processors.cache.datastructures; import org.apache.ignite.*; import org.apache.ignite.cache.*; import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.processors.cache.*; import org.apache.ignite.internal.util.*; import org.apache.ignite.lang.*; import org.apache.ignite.resources.*; import org.apache.ignite.internal.util.typedef.*; -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.testframework.*; -import org.apache.ignite.testframework.junits.common.*; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; -import static org.apache.ignite.cache.CacheMode.PARTITIONED; -import static org.apache.ignite.cache.CachePreloadMode.*; +import static org.apache.ignite.cache.CacheAtomicityMode.*; /** * Failover tests for cache data structures. */ -public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends GridCommonAbstractTest { +public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends IgniteAtomicsAbstractTest { /** */ private static final long TEST_TIMEOUT = 2 * 60 * 1000; @@ -52,13 +46,13 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr private static final String STRUCTURE_NAME = "structure"; /** */ - private static final int TOP_CHANGE_CNT = 5; + private static final String TRANSACTIONAL_CACHE_NAME = "tx_cache"; /** */ - private static final int TOP_CHANGE_THREAD_CNT = 3; + private static final int TOP_CHANGE_CNT = 5; /** */ - private static TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); + private static final int TOP_CHANGE_THREAD_CNT = 3; /** {@inheritDoc} */ @Override protected long getTestTimeout() { @@ -66,37 +60,13 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr } /** - * @return Cache mode. - */ - protected abstract CacheMode cacheMode(); - - /** * @return Grids count to start. */ - public int gridCount() { + @Override public int gridCount() { return 3; } /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration cfg = super.getConfiguration(gridName); - - TcpDiscoverySpi disco = new TcpDiscoverySpi(); - - disco.setIpFinder(ipFinder); - - cfg.setDiscoverySpi(disco); - - IgniteAtomicConfiguration atomicCfg = new IgniteAtomicConfiguration(); - - atomicCfg.setCacheMode(cacheMode()); - - cfg.setAtomicConfiguration(atomicCfg); - - return cfg; - } - - /** {@inheritDoc} */ @Override protected void beforeTestsStarted() throws Exception { // No-op } @@ -118,13 +88,25 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr stopAllGrids(); } + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + CacheConfiguration ccfg = new CacheConfiguration(); + + ccfg.setName(TRANSACTIONAL_CACHE_NAME); + ccfg.setAtomicityMode(TRANSACTIONAL); + + cfg.setCacheConfiguration(ccfg); + + return cfg; + } + /** * @throws Exception If failed. */ public void testAtomicLongTopologyChange() throws Exception { - try { - grid(0).atomicLong(STRUCTURE_NAME, 10, true); - + try (IgniteAtomicLong atomic = grid(0).atomicLong(STRUCTURE_NAME, 10, true)) { Ignite g = startGrid(NEW_GRID_NAME); assert g.atomicLong(STRUCTURE_NAME, 10, true).get() == 10; @@ -135,18 +117,13 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr assert grid(0).atomicLong(STRUCTURE_NAME, 10, true).get() == 20; } - finally { - cache().dataStructures().removeAtomicLong(STRUCTURE_NAME); - } } /** * @throws Exception If failed. */ public void testAtomicLongConstantTopologyChange() throws Exception { - try { - IgniteAtomicLong s = cache().dataStructures().atomicLong(STRUCTURE_NAME, 1, true); - + try (IgniteAtomicLong s = grid(0).atomicLong(STRUCTURE_NAME, 1, true)) { IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { @Override public void apply() { try { @@ -156,7 +133,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr try { Ignite g = startGrid(name); - assert g.cache(null).dataStructures().atomicLong(STRUCTURE_NAME, 1, true).get() > 0; + assert g.atomicLong(STRUCTURE_NAME, 1, true).get() > 0; } finally { if (i != TOP_CHANGE_CNT - 1) @@ -183,10 +160,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr fut.get(); for (Ignite g : G.allGrids()) - assert g.cache(null).dataStructures().atomicLong(STRUCTURE_NAME, 1, true).get() == val; - } - finally { - cache().dataStructures().removeAtomicLong(STRUCTURE_NAME); + assertEquals(val, g.atomicLong(STRUCTURE_NAME, 1, true).get()); } } @@ -194,9 +168,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr * @throws Exception If failed. */ public void testAtomicLongConstantMultipleTopologyChange() throws Exception { - try { - IgniteAtomicLong s = cache().dataStructures().atomicLong(STRUCTURE_NAME, 1, true); - + try (IgniteAtomicLong s = grid(0).atomicLong(STRUCTURE_NAME, 1, true)) { IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { @Override public void apply() { try { @@ -211,7 +183,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr Ignite g = startGrid(name); - assert g.cache(null).dataStructures().atomicLong(STRUCTURE_NAME, 1, true).get() > 0; + assert g.atomicLong(STRUCTURE_NAME, 1, true).get() > 0; } } finally { @@ -240,10 +212,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr fut.get(); for (Ignite g : G.allGrids()) - assert g.cache(null).dataStructures().atomicLong(STRUCTURE_NAME, 1, true).get() == val; - } - finally { - cache().dataStructures().removeAtomicLong(STRUCTURE_NAME); + assertEquals(val, g.atomicLong(STRUCTURE_NAME, 1, true).get()); } } @@ -251,21 +220,16 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr * @throws Exception If failed. */ public void testAtomicReferenceTopologyChange() throws Exception { - try { - cache().dataStructures().atomicReference(STRUCTURE_NAME, 10, true); - + try (IgniteAtomicReference atomic = grid(0).atomicReference(STRUCTURE_NAME, 10, true)) { Ignite g = startGrid(NEW_GRID_NAME); - assert g.cache(null).dataStructures().<Integer>atomicReference(STRUCTURE_NAME, 10, true).get() == 10; + assert g.<Integer>atomicReference(STRUCTURE_NAME, 10, true).get() == 10; - g.cache(null).dataStructures().<Integer>atomicReference(STRUCTURE_NAME, 10, true).set(20); + g.<Integer>atomicReference(STRUCTURE_NAME, 10, true).set(20); stopGrid(NEW_GRID_NAME); - assert cache().dataStructures().atomicReference(STRUCTURE_NAME, 10, true).get().equals(20); - } - finally { - cache().dataStructures().removeAtomicReference(STRUCTURE_NAME); + assertEquals(20, (int) grid(0).atomicReference(STRUCTURE_NAME, 10, true).get()); } } @@ -273,9 +237,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr * @throws Exception If failed. */ public void testAtomicReferenceConstantTopologyChange() throws Exception { - try { - IgniteAtomicReference<Integer> s = cache().dataStructures().atomicReference(STRUCTURE_NAME, 1, true); - + try (IgniteAtomicReference<Integer> s = grid(0).atomicReference(STRUCTURE_NAME, 1, true)) { IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { @Override public void apply() { try { @@ -285,8 +247,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr try { Ignite g = startGrid(name); - assert g.cache(null).dataStructures().<Integer>atomicReference(STRUCTURE_NAME, 1, true) - .get() > 0; + assert g.<Integer>atomicReference(STRUCTURE_NAME, 1, true).get() > 0; } finally { if (i != TOP_CHANGE_CNT - 1) @@ -311,10 +272,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr fut.get(); for (Ignite g : G.allGrids()) - assert g.cache(null).dataStructures().<Integer>atomicReference(STRUCTURE_NAME, 1, true).get() == val; - } - finally { - cache().dataStructures().removeAtomicReference(STRUCTURE_NAME); + assertEquals(val, (int)g.<Integer>atomicReference(STRUCTURE_NAME, 1, true).get()); } } @@ -322,9 +280,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr * @throws Exception If failed. */ public void testAtomicReferenceConstantMultipleTopologyChange() throws Exception { - try { - IgniteAtomicReference<Integer> s = cache().dataStructures().atomicReference(STRUCTURE_NAME, 1, true); - + try (IgniteAtomicReference<Integer> s = grid(0).atomicReference(STRUCTURE_NAME, 1, true)) { IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { @Override public void apply() { try { @@ -339,9 +295,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr Ignite g = startGrid(name); - assert - g.cache(null).dataStructures().<Integer>atomicReference(STRUCTURE_NAME, 1, true) - .get() > 0; + assert g.<Integer>atomicReference(STRUCTURE_NAME, 1, true).get() > 0; } } finally { @@ -368,10 +322,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr fut.get(); for (Ignite g : G.allGrids()) - assert g.cache(null).dataStructures().<Integer>atomicReference(STRUCTURE_NAME, 1, true).get() == val; - } - finally { - cache().dataStructures().removeAtomicReference(STRUCTURE_NAME); + assert g.<Integer>atomicReference(STRUCTURE_NAME, 1, true).get() == val; } } @@ -379,38 +330,30 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr * @throws Exception If failed. */ public void testAtomicStampedTopologyChange() throws Exception { - try { - cache().dataStructures().atomicStamped(STRUCTURE_NAME, 10, 10, true); - + try (IgniteAtomicStamped atomic = grid(0).atomicStamped(STRUCTURE_NAME, 10, 10, true)) { Ignite g = startGrid(NEW_GRID_NAME); - IgniteBiTuple<Integer, Integer> t = g.cache(null).dataStructures() - .<Integer, Integer>atomicStamped(STRUCTURE_NAME, 10, 10, true).get(); + IgniteBiTuple<Integer, Integer> t = g.<Integer, Integer>atomicStamped(STRUCTURE_NAME, 10, 10, true).get(); assert t.get1() == 10; assert t.get2() == 10; - g.cache(null).dataStructures().<Integer, Integer>atomicStamped(STRUCTURE_NAME, 10, 10, true).set(20, 20); + g.<Integer, Integer>atomicStamped(STRUCTURE_NAME, 10, 10, true).set(20, 20); stopGrid(NEW_GRID_NAME); - t = cache().dataStructures().<Integer, Integer>atomicStamped(STRUCTURE_NAME, 10, 10, true).get(); + t = grid(0).<Integer, Integer>atomicStamped(STRUCTURE_NAME, 10, 10, true).get(); assert t.get1() == 20; assert t.get2() == 20; } - finally { - cache().dataStructures().removeAtomicStamped(STRUCTURE_NAME); - } } /** * @throws Exception If failed. */ public void testAtomicStampedConstantTopologyChange() throws Exception { - try { - IgniteAtomicStamped<Integer, Integer> s = cache().dataStructures().atomicStamped(STRUCTURE_NAME, 1, 1, true); - + try (IgniteAtomicStamped<Integer, Integer> s = grid(0).atomicStamped(STRUCTURE_NAME, 1, 1, true)) { IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { @Override public void apply() { try { @@ -421,8 +364,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr Ignite g = startGrid(name); IgniteBiTuple<Integer, Integer> t = - g.cache(null).dataStructures() - .<Integer, Integer>atomicStamped(STRUCTURE_NAME, 1, 1, true).get(); + g.<Integer, Integer>atomicStamped(STRUCTURE_NAME, 1, 1, true).get(); assert t.get1() > 0; assert t.get2() > 0; @@ -455,25 +397,19 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr fut.get(); for (Ignite g : G.allGrids()) { - IgniteBiTuple<Integer, Integer> t = g.cache(null).dataStructures() - .<Integer, Integer>atomicStamped(STRUCTURE_NAME, 1, 1, true).get(); + IgniteBiTuple<Integer, Integer> t = g.<Integer, Integer>atomicStamped(STRUCTURE_NAME, 1, 1, true).get(); assert t.get1() == val; assert t.get2() == val; } } - finally { - cache().dataStructures().removeAtomicStamped(STRUCTURE_NAME); - } } /** * @throws Exception If failed. */ public void testAtomicStampedConstantMultipleTopologyChange() throws Exception { - try { - IgniteAtomicStamped<Integer, Integer> s = cache().dataStructures().atomicStamped(STRUCTURE_NAME, 1, 1, true); - + try (IgniteAtomicStamped<Integer, Integer> s = grid(0).atomicStamped(STRUCTURE_NAME, 1, 1, true)) { IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { @Override public void apply() { try { @@ -489,8 +425,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr Ignite g = startGrid(name); IgniteBiTuple<Integer, Integer> t = - g.cache(null).dataStructures() - .<Integer, Integer>atomicStamped(STRUCTURE_NAME, 1, 1, true).get(); + g.<Integer, Integer>atomicStamped(STRUCTURE_NAME, 1, 1, true).get(); assert t.get1() > 0; assert t.get2() > 0; @@ -525,39 +460,33 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr fut.get(); for (Ignite g : G.allGrids()) { - IgniteBiTuple<Integer, Integer> t = g.cache(null).dataStructures() - .<Integer, Integer>atomicStamped(STRUCTURE_NAME, 1, 1, true).get(); + IgniteBiTuple<Integer, Integer> t = g.<Integer, Integer>atomicStamped(STRUCTURE_NAME, 1, 1, true).get(); assert t.get1() == val; assert t.get2() == val; } } - finally { - cache().dataStructures().removeAtomicStamped(STRUCTURE_NAME); - } } /** * @throws Exception If failed. */ public void testCountDownLatchTopologyChange() throws Exception { - try { - cache().dataStructures().countDownLatch(STRUCTURE_NAME, 20, true, true); - - Ignite g = startGrid(NEW_GRID_NAME); - - assert g.cache(null).dataStructures().countDownLatch(STRUCTURE_NAME, 20, true, true).count() == 20; + try (IgniteCountDownLatch latch = grid(0).countDownLatch(STRUCTURE_NAME, 20, true, true)) { + try { + Ignite g = startGrid(NEW_GRID_NAME); - g.cache(null).dataStructures().countDownLatch(STRUCTURE_NAME, 20, true, true).countDown(10); + assert g.countDownLatch(STRUCTURE_NAME, 20, true, true).count() == 20; - stopGrid(NEW_GRID_NAME); + g.countDownLatch(STRUCTURE_NAME, 20, true, true).countDown(10); - assert cache().dataStructures().countDownLatch(STRUCTURE_NAME, 20, true, true).count() == 10; - } - finally { - cache().dataStructures().countDownLatch(STRUCTURE_NAME, 20, true, true).countDownAll(); + stopGrid(NEW_GRID_NAME); - cache().dataStructures().removeCountDownLatch(STRUCTURE_NAME); + assert grid(0).countDownLatch(STRUCTURE_NAME, 20, true, true).count() == 10; + } + finally { + grid(0).countDownLatch(STRUCTURE_NAME, 20, true, true).countDownAll(); + } } } @@ -565,54 +494,49 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr * @throws Exception If failed. */ public void testCountDownLatchConstantTopologyChange() throws Exception { - try { - IgniteCountDownLatch s = cache().dataStructures().countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, - false, true); - - IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { - @Override public void apply() { - try { - for (int i = 0; i < TOP_CHANGE_CNT; i++) { - String name = UUID.randomUUID().toString(); - - try { - Ignite g = startGrid(name); + try (IgniteCountDownLatch s = grid(0).countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, true)) { + try { + IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { + @Override public void apply() { + try { + for (int i = 0; i < TOP_CHANGE_CNT; i++) { + String name = UUID.randomUUID().toString(); + + try { + Ignite g = startGrid(name); - assert g.cache(null).dataStructures().countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, - false, false) != null; - } - finally { - if (i != TOP_CHANGE_CNT - 1) - stopGrid(name); + assert g.countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, false) != null; + } + finally { + if (i != TOP_CHANGE_CNT - 1) + stopGrid(name); + } } } + catch (Exception e) { + throw F.wrap(e); + } } - catch (Exception e) { - throw F.wrap(e); - } - } - }, TOP_CHANGE_THREAD_CNT, "topology-change-thread"); + }, TOP_CHANGE_THREAD_CNT, "topology-change-thread"); - int val = s.count(); - - while (!fut.isDone()) { - assert s.count() == val; + int val = s.count(); - assert s.countDown() == val - 1; + while (!fut.isDone()) { + assert s.count() == val; - val--; - } + assert s.countDown() == val - 1; - fut.get(); + val--; + } - for (Ignite g : G.allGrids()) - assert g.cache(null).dataStructures().countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, true) - .count() == val; - } - finally { - cache().dataStructures().countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, true).countDownAll(); + fut.get(); - cache().dataStructures().removeCountDownLatch(STRUCTURE_NAME); + for (Ignite g : G.allGrids()) + assert g.countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, true).count() == val; + } + finally { + grid(0).countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, true).countDownAll(); + } } } @@ -620,61 +544,56 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr * @throws Exception If failed. */ public void testCountDownLatchConstantMultipleTopologyChange() throws Exception { - try { - IgniteCountDownLatch s = cache().dataStructures() - .countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, true); + try (IgniteCountDownLatch s = grid(0).countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, true)) { + try { + IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { + @Override public void apply() { + try { + for (int i = 0; i < TOP_CHANGE_CNT; i++) { + Collection<String> names = new GridLeanSet<>(3); - IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() { - @Override public void apply() { - try { - for (int i = 0; i < TOP_CHANGE_CNT; i++) { - Collection<String> names = new GridLeanSet<>(3); - - try { - for (int j = 0; j < 3; j++) { - String name = UUID.randomUUID().toString(); + try { + for (int j = 0; j < 3; j++) { + String name = UUID.randomUUID().toString(); - names.add(name); + names.add(name); - Ignite g = startGrid(name); + Ignite g = startGrid(name); - assert g.cache(null).dataStructures() - .countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, false) != null; + assert g.countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, false) != null; + } + } + finally { + if (i != TOP_CHANGE_CNT - 1) + for (String name : names) + stopGrid(name); } - } - finally { - if (i != TOP_CHANGE_CNT - 1) - for (String name : names) - stopGrid(name); } } + catch (Exception e) { + throw F.wrap(e); + } } - catch (Exception e) { - throw F.wrap(e); - } - } - }, TOP_CHANGE_THREAD_CNT, "topology-change-thread"); - - int val = s.count(); + }, TOP_CHANGE_THREAD_CNT, "topology-change-thread"); - while (!fut.isDone()) { - assert s.count() == val; + int val = s.count(); - assert s.countDown() == val - 1; + while (!fut.isDone()) { + assert s.count() == val; - val--; - } + assert s.countDown() == val - 1; - fut.get(); + val--; + } - for (Ignite g : G.allGrids()) - assert g.cache(null).dataStructures() - .countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, false).count() == val; - } - finally { - cache().dataStructures().countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, false).countDownAll(); + fut.get(); - cache().dataStructures().removeCountDownLatch(STRUCTURE_NAME); + for (Ignite g : G.allGrids()) + assertEquals(val, g.countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, false).count()); + } + finally { + grid(0).countDownLatch(STRUCTURE_NAME, Integer.MAX_VALUE, false, false).countDownAll(); + } } } @@ -703,7 +622,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr /** * @throws Exception If failed. */ - public void testFifoQueueConstantTopologyChange() throws Exception { + public void testQueueConstantTopologyChange() throws Exception { try { IgniteQueue<Integer> s = cache().dataStructures().queue(STRUCTURE_NAME, 0, false, true); @@ -753,7 +672,7 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr /** * @throws Exception If failed. */ - public void testFifoQueueConstantMultipleTopologyChange() throws Exception { + public void testQueueConstantMultipleTopologyChange() throws Exception { try { IgniteQueue<Integer> s = cache().dataStructures().queue(STRUCTURE_NAME, 0, false, true); @@ -974,17 +893,19 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr public void testUncommitedTxLeave() throws Exception { final int val = 10; - cache().dataStructures().atomicLong(STRUCTURE_NAME, val, true); + grid(0).atomicLong(STRUCTURE_NAME, val, true); GridTestUtils.runAsync(new Callable<Object>() { @Override public Object call() throws Exception { Ignite g = startGrid(NEW_GRID_NAME); try { - g.cache(null).txStart(); + g.transactions().txStart(); + + + g.jcache(TRANSACTIONAL_CACHE_NAME).put(1, 1); - assert g.cache(null).dataStructures().atomicLong(STRUCTURE_NAME, val, false). - incrementAndGet() == val + 1; + assert g.atomicLong(STRUCTURE_NAME, val, false).incrementAndGet() == val + 1; } finally { stopGrid(NEW_GRID_NAME); @@ -996,6 +917,6 @@ public abstract class GridCacheAbstractDataStructuresFailoverSelfTest extends Gr waitForDiscovery(G.allGrids().toArray(new Ignite[gridCount()])); - assert cache().dataStructures().atomicLong(STRUCTURE_NAME, val, false).get() == val + 1; + assert grid(0).atomicLong(STRUCTURE_NAME, val, false).get() == val + 1; } }
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/349d51ed/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractQueueFailoverDataConsistencySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractQueueFailoverDataConsistencySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractQueueFailoverDataConsistencySelfTest.java index e3c3ddb..77c6050 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractQueueFailoverDataConsistencySelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAbstractQueueFailoverDataConsistencySelfTest.java @@ -19,8 +19,6 @@ package org.apache.ignite.internal.processors.cache.datastructures; import org.apache.ignite.*; import org.apache.ignite.cache.*; -import org.apache.ignite.cache.affinity.*; -import org.apache.ignite.cache.store.*; import org.apache.ignite.configuration.*; import org.apache.ignite.internal.*; import org.apache.ignite.internal.processors.cache.*; @@ -33,15 +31,13 @@ import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; -import static org.apache.ignite.cache.CacheAtomicWriteOrderMode.*; import static org.apache.ignite.cache.CacheAtomicityMode.*; -import static org.apache.ignite.cache.CacheDistributionMode.*; import static org.apache.ignite.cache.CacheMode.*; /** * Queue failover test. */ -public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest extends GridCacheAbstractSelfTest { +public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest extends IgniteCollectionAbstractTest { /** */ private static final String QUEUE_NAME = "FailoverQueueTest"; @@ -85,31 +81,17 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte } /** {@inheritDoc} */ - @Override protected CacheStore<?, ?> cacheStore() { - return null; + @Override protected CacheMode collectionCacheMode() { + return PARTITIONED; } /** {@inheritDoc} */ - @Override protected CacheConfiguration cacheConfiguration(String gridName) throws Exception { - CacheConfiguration ccfg = super.cacheConfiguration(gridName); - - ccfg.setBackups(1); - ccfg.setAtomicWriteOrderMode(PRIMARY); - ccfg.setSwapEnabled(false); - ccfg.setQueryIndexEnabled(false); - ccfg.setCacheStoreFactory(null); - - return ccfg; - } + @Override IgniteCollectionConfiguration collectionConfiguration() { + IgniteCollectionConfiguration colCfg = super.collectionConfiguration(); - /** {@inheritDoc} */ - @Override protected CacheMode cacheMode() { - return PARTITIONED; - } + colCfg.setBackups(1); - /** {@inheritDoc} */ - @Override protected CacheDistributionMode distributionMode() { - return PARTITIONED_ONLY; + return colCfg; } /** @@ -131,12 +113,16 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte * @throws Exception If failed. */ private void testAddFailover(boolean collocated) throws Exception { - IgniteQueue<Integer> queue = cache().dataStructures().queue(QUEUE_NAME, 0, collocated, true); + IgniteCollectionConfiguration colCfg = collectionConfiguration(); + + colCfg.setCollocated(collocated); + + IgniteQueue<Integer> queue = grid(0).queue(QUEUE_NAME, colCfg, 0,true); assertNotNull(queue); assertEquals(0, queue.size()); - int primaryNode = primaryQueueNode(); + int primaryNode = primaryQueueNode(queue); int testNodeIdx = -1; @@ -148,7 +134,7 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte log.info("Test node: " + testNodeIdx) ; log.info("Header primary node: " + primaryNode) ; - queue = grid(testNodeIdx).cache(null).dataStructures().queue(QUEUE_NAME, 0, collocated, false); + queue = grid(testNodeIdx).queue(QUEUE_NAME, null, 0, false); assertNotNull(queue); @@ -176,7 +162,7 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte IgniteFuture<?> fut = startNodeKiller(stop, new AtomicInteger(), killIdxs); - final int ITEMS = (atomicityMode() == ATOMIC) ? 10_000 : 3000; + final int ITEMS = (collectionCacheAtomicityMode() == ATOMIC) ? 10_000 : 3000; try { for (int i = 0; i < ITEMS; i++) { @@ -224,12 +210,16 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte * @throws Exception If failed. */ private void testPollFailover(boolean collocated) throws Exception { - IgniteQueue<Integer> queue = cache().dataStructures().queue(QUEUE_NAME, 0, collocated, true); + IgniteCollectionConfiguration colCfg = collectionConfiguration(); + + colCfg.setCollocated(collocated); + + IgniteQueue<Integer> queue = grid(0).queue(QUEUE_NAME, colCfg, 0, true); assertNotNull(queue); assertEquals(0, queue.size()); - int primaryNode = primaryQueueNode(); + int primaryNode = primaryQueueNode(queue); int testNodeIdx = -1; @@ -241,11 +231,11 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte log.info("Test node: " + testNodeIdx) ; log.info("Primary node: " + primaryNode) ; - queue = grid(testNodeIdx).cache(null).dataStructures().queue(QUEUE_NAME, 0, collocated, false); + queue = grid(testNodeIdx).queue(QUEUE_NAME, null, 0, false); assertNotNull(queue); - testPollFailover(queue, Arrays.asList(primaryQueueNode())); // Kill queue header's primary node . + testPollFailover(queue, Arrays.asList(primaryQueueNode(queue))); // Kill queue header's primary node . List<Integer> killIdxs = new ArrayList<>(); @@ -265,7 +255,7 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte private void testPollFailover(IgniteQueue<Integer> queue, final List<Integer> killIdxs) throws Exception { assert !killIdxs.isEmpty(); - final int ITEMS = atomicityMode() == ATOMIC && !queue.collocated() ? 10_000 : 3000; + final int ITEMS = collectionCacheAtomicityMode() == ATOMIC && !queue.collocated() ? 10_000 : 3000; for (int i = 0; i < ITEMS; i++) { assertTrue(queue.add(i)); @@ -292,7 +282,7 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte for (int i = 0; i < pollNum; i++) { Integer e = queue.poll(); - if (atomicityMode() == ATOMIC) { + if (collectionCacheAtomicityMode() == ATOMIC) { if (e == null || e != exp) { log.info("Unexpected data [expected=" + i + ", actual=" + e + ']'); @@ -318,7 +308,7 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte fut.get(); - if (atomicityMode() == ATOMIC) + if (collectionCacheAtomicityMode() == ATOMIC) assertTrue("Too many errors for atomic cache: " + err, err <= stopCnt.get()); assertNull(queue.poll()); @@ -333,7 +323,8 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte * @param killIdxs Indexes of nodes to kill. * @return Future completing when thread finishes. */ - private IgniteFuture<?> startNodeKiller(final AtomicBoolean stop, final AtomicInteger killCnt, + private IgniteFuture<?> startNodeKiller(final AtomicBoolean stop, + final AtomicInteger killCnt, final List<Integer> killIdxs) { return GridTestUtils.runAsync(new Callable<Void>() { @Override public Void call() throws Exception { @@ -361,14 +352,17 @@ public abstract class GridCacheAbstractQueueFailoverDataConsistencySelfTest exte } /** + * @param queue Queue. * @return Primary node for queue's header. */ - private int primaryQueueNode() { - CacheAffinity<Object> aff = grid(0).cache(null).affinity(); + private int primaryQueueNode(IgniteQueue queue) { + GridCacheContext cctx = GridTestUtils.getFieldValue(queue, "cctx"); + + GridCacheAffinityManager aff = cctx.affinity(); for (int i = 0; i < gridCount(); i++) { - for (GridCacheEntryEx e : ((GridKernal)grid(i)).context().cache().internalCache().map().allEntries0()) { - if (aff.isPrimary(grid(i).localNode(), e.key()) && e.key() instanceof GridCacheQueueHeaderKey) + for (GridCacheEntryEx e : ((GridKernal)grid(i)).context().cache().internalCache(cctx.name()).map().allEntries0()) { + if (aff.primary(grid(i).localNode(), e.key(), -1) && e.key() instanceof GridCacheQueueHeaderKey) return i; } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/349d51ed/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicLongApiAbstractSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicLongApiAbstractSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicLongApiAbstractSelfTest.java new file mode 100644 index 0000000..8eb34a7 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicLongApiAbstractSelfTest.java @@ -0,0 +1,266 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.cache.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.transactions.*; + +import java.util.*; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; + +/** + * Cache atomic long api test. + */ +public abstract class GridCacheAtomicLongApiAbstractSelfTest extends IgniteAtomicsAbstractTest { + /** Random number generator. */ + private static final Random RND = new Random(); + + /** */ + private static final String TRANSACTIONAL_CACHE_NAME = "tx_cache"; + + /** {@inheritDoc} */ + @Override protected int gridCount() { + return 1; + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + CacheConfiguration ccfg = new CacheConfiguration(); + + ccfg.setName(TRANSACTIONAL_CACHE_NAME); + ccfg.setAtomicityMode(TRANSACTIONAL); + + cfg.setCacheConfiguration(ccfg); + + return cfg; + } + + /** + * @throws Exception If failed. + */ + public void testCreateRemove() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + String atomicName1 = "FIRST"; + + String atomicName2 = "SECOND"; + + IgniteAtomicLong atomic1 = ignite.atomicLong(atomicName1, 0, true); + IgniteAtomicLong atomic2 = ignite.atomicLong(atomicName2, 0, true); + IgniteAtomicLong atomic3 = ignite.atomicLong(atomicName1, 0, true); + + assertNotNull(atomic1); + assertNotNull(atomic2); + assertNotNull(atomic3); + + assert atomic1.equals(atomic3); + assert atomic3.equals(atomic1); + assert !atomic3.equals(atomic2); + + atomic1.close(); + atomic2.close(); + atomic3.close(); + + assertNull(ignite.atomicLong(atomicName1, 0, false)); + assertNull(ignite.atomicLong(atomicName2, 0, false)); + + try { + atomic1.get(); + + fail(); + } + catch (IgniteCheckedException e) { + info("Caught expected exception: " + e.getMessage()); + } + } + + /** + * @throws Exception If failed. + */ + public void testIncrementAndGet() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true); + + long curAtomicVal = atomic.get(); + + assert curAtomicVal + 1 == atomic.incrementAndGet(); + assert curAtomicVal + 1 == atomic.get(); + } + + /** + * @throws Exception If failed. + */ + public void testGetAndIncrement() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true); + + long curAtomicVal = atomic.get(); + + assert curAtomicVal == atomic.getAndIncrement(); + assert curAtomicVal + 1 == atomic.get(); + } + + /** + * @throws Exception If failed. + */ + public void testDecrementAndGet() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true); + + long curAtomicVal = atomic.get(); + + assert curAtomicVal - 1 == atomic.decrementAndGet(); + assert curAtomicVal - 1 == atomic.get(); + } + + /** + * @throws Exception If failed. + */ + public void testGetAndDecrement() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true); + + long curAtomicVal = atomic.get(); + + assert curAtomicVal == atomic.getAndDecrement(); + assert curAtomicVal - 1 == atomic.get(); + } + + /** + * @throws Exception If failed. + */ + public void testGetAndAdd() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true); + + long delta = RND.nextLong(); + + long curAtomicVal = atomic.get(); + + assert curAtomicVal == atomic.getAndAdd(delta); + assert curAtomicVal + delta == atomic.get(); + } + + /** + * @throws Exception If failed. + */ + public void testAddAndGet() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true); + + long delta = RND.nextLong(); + + long curAtomicVal = atomic.get(); + + assert curAtomicVal + delta == atomic.addAndGet(delta); + assert curAtomicVal + delta == atomic.get(); + } + + /** + * @throws Exception If failed. + */ + public void testGetAndSet() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true); + + long newVal = RND.nextLong(); + + long curAtomicVal = atomic.get(); + + assert curAtomicVal == atomic.getAndSet(newVal); + assert newVal == atomic.get(); + } + + /** + * @throws Exception If failed. + */ + @SuppressWarnings({"NullableProblems", "ConstantConditions"}) + public void testCompareAndSet() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true); + + long newVal = RND.nextLong(); + + final long oldVal = atomic.get(); + + // Don't set new value. + assert !atomic.compareAndSet(oldVal - 1, newVal); + + assert oldVal == atomic.get(); + + // Set new value. + assert atomic.compareAndSet(oldVal, newVal); + + assert newVal == atomic.get(); + } + + /** + * @throws Exception If failed. + */ + public void testGetAndSetInTx() throws Exception { + info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']'); + + Ignite ignite = grid(0); + + IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true); + + IgniteCache<Object, Object> cache = ignite.jcache(TRANSACTIONAL_CACHE_NAME); + + try (IgniteTx tx = ignite.transactions().txStart()) { + cache.put(1, 1); + + long newVal = RND.nextLong(); + + long curAtomicVal = atomic.get(); + + assert curAtomicVal == atomic.getAndSet(newVal); + assert newVal == atomic.get(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/349d51ed/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicLongApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicLongApiSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicLongApiSelfTest.java deleted file mode 100644 index 4018068..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicLongApiSelfTest.java +++ /dev/null @@ -1,483 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.internal.processors.cache.datastructures; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.configuration.*; -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.transactions.*; -import org.apache.ignite.testframework.junits.common.*; - -import java.util.*; - -import static org.apache.ignite.cache.CacheAtomicityMode.*; -import static org.apache.ignite.cache.CacheDistributionMode.*; -import static org.apache.ignite.cache.CacheMode.*; -import static org.apache.ignite.cache.CachePreloadMode.*; -import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; - -/** - * Cache atomic long api test. - */ -public class GridCacheAtomicLongApiSelfTest extends GridCommonAbstractTest { - /** Random number generator. */ - private static final Random RND = new Random(); - - /** */ - private static TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration cfg = super.getConfiguration(gridName); - - cfg.setLocalHost("127.0.0.1"); - - TcpDiscoverySpi spi = new TcpDiscoverySpi(); - - spi.setIpFinder(ipFinder); - - cfg.setDiscoverySpi(spi); - - CacheConfiguration repCacheCfg = defaultCacheConfiguration(); - - repCacheCfg.setName("replicated"); - repCacheCfg.setCacheMode(REPLICATED); - repCacheCfg.setPreloadMode(SYNC); - repCacheCfg.setWriteSynchronizationMode(FULL_SYNC); - repCacheCfg.setEvictionPolicy(null); - repCacheCfg.setAtomicityMode(TRANSACTIONAL); - repCacheCfg.setDistributionMode(NEAR_PARTITIONED); - - CacheConfiguration partCacheCfg = defaultCacheConfiguration(); - - partCacheCfg.setName("partitioned"); - partCacheCfg.setCacheMode(PARTITIONED); - partCacheCfg.setBackups(1); - partCacheCfg.setPreloadMode(SYNC); - partCacheCfg.setWriteSynchronizationMode(FULL_SYNC); - partCacheCfg.setEvictionPolicy(null); - partCacheCfg.setNearEvictionPolicy(null); - partCacheCfg.setAtomicityMode(TRANSACTIONAL); - partCacheCfg.setDistributionMode(NEAR_PARTITIONED); - - CacheConfiguration locCacheCfg = defaultCacheConfiguration(); - - locCacheCfg.setName("local"); - locCacheCfg.setCacheMode(LOCAL); - locCacheCfg.setWriteSynchronizationMode(FULL_SYNC); - locCacheCfg.setEvictionPolicy(null); - locCacheCfg.setAtomicityMode(TRANSACTIONAL); - locCacheCfg.setDistributionMode(NEAR_PARTITIONED); - - cfg.setCacheConfiguration(repCacheCfg, partCacheCfg, locCacheCfg); - - return cfg; - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - super.beforeTestsStarted(); - - startGrid(); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - super.afterTestsStopped(); - - stopAllGrids(); - } - - /** - * @throws Exception If failed. - */ - public void testCreateRemove() throws Exception { - createRemove("local"); - - createRemove("replicated"); - - createRemove("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - private void createRemove(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - String atomicName1 = "FIRST"; - - String atomicName2 = "SECOND"; - - IgniteAtomicLong atomic1 = cache.dataStructures().atomicLong(atomicName1, 0, true); - IgniteAtomicLong atomic2 = cache.dataStructures().atomicLong(atomicName2, 0, true); - IgniteAtomicLong atomic3 = cache.dataStructures().atomicLong(atomicName1, 0, true); - - assertNotNull(atomic1); - assertNotNull(atomic2); - assertNotNull(atomic3); - - assert atomic1.equals(atomic3); - assert atomic3.equals(atomic1); - assert !atomic3.equals(atomic2); - - assertEquals(0, cache.primarySize()); - - assert cache.dataStructures().removeAtomicLong(atomicName1); - assert cache.dataStructures().removeAtomicLong(atomicName2); - assert !cache.dataStructures().removeAtomicLong(atomicName1); - assert !cache.dataStructures().removeAtomicLong(atomicName2); - - assertEquals(0, cache.primarySize()); - - try { - atomic1.get(); - - fail(); - } - catch (IgniteCheckedException e) { - info("Caught expected exception: " + e.getMessage()); - } - } - - /** - * @throws Exception If failed. - */ - public void testIncrementAndGet() throws Exception { - incrementAndGet("local"); - - incrementAndGet("replicated"); - - incrementAndGet("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - private void incrementAndGet(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - IgniteAtomicLong atomic = cache.dataStructures().atomicLong("atomic", 0, true); - - assertEquals(0, cache.primarySize()); - - long curAtomicVal = atomic.get(); - - assert curAtomicVal + 1 == atomic.incrementAndGet(); - assert curAtomicVal + 1 == atomic.get(); - } - - /** - * @throws Exception If failed. - */ - public void testGetAndIncrement() throws Exception { - getAndIncrement("local"); - - getAndIncrement("replicated"); - - getAndIncrement("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - private void getAndIncrement(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - IgniteAtomicLong atomic = cache.dataStructures().atomicLong("atomic", 0, true); - - assertEquals(0, cache.primarySize()); - - long curAtomicVal = atomic.get(); - - assert curAtomicVal == atomic.getAndIncrement(); - assert curAtomicVal + 1 == atomic.get(); - } - - /** - * @throws Exception If failed. - */ - public void testDecrementAndGet() throws Exception { - decrementAndGet("local"); - - decrementAndGet("replicated"); - - decrementAndGet("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - private void decrementAndGet(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - IgniteAtomicLong atomic = cache.dataStructures().atomicLong("atomic", 0, true); - - assertEquals(0, cache.primarySize()); - - long curAtomicVal = atomic.get(); - - assert curAtomicVal - 1 == atomic.decrementAndGet(); - assert curAtomicVal - 1 == atomic.get(); - } - - /** - * @throws Exception If failed. - */ - public void testGetAndDecrement() throws Exception { - getAndDecrement("local"); - - getAndDecrement("replicated"); - - getAndDecrement("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - private void getAndDecrement(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - IgniteAtomicLong atomic = cache.dataStructures().atomicLong("atomic", 0, true); - - assertEquals(0, cache.primarySize()); - - long curAtomicVal = atomic.get(); - - assert curAtomicVal == atomic.getAndDecrement(); - assert curAtomicVal - 1 == atomic.get(); - } - - /** - * @throws Exception If failed. - */ - public void testGetAndAdd() throws Exception { - getAndAdd("local"); - - getAndAdd("replicated"); - - getAndAdd("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - private void getAndAdd(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - IgniteAtomicLong atomic = cache.dataStructures().atomicLong("atomic", 0, true); - - assertEquals(0, cache.primarySize()); - - long delta = RND.nextLong(); - - long curAtomicVal = atomic.get(); - - assert curAtomicVal == atomic.getAndAdd(delta); - assert curAtomicVal + delta == atomic.get(); - } - - /** - * @throws Exception If failed. - */ - public void testAddAndGet() throws Exception { - addAndGet("local"); - - addAndGet("replicated"); - - addAndGet("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - private void addAndGet(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - IgniteAtomicLong atomic = cache.dataStructures().atomicLong("atomic", 0, true); - - assertEquals(0, cache.primarySize()); - - long delta = RND.nextLong(); - - long curAtomicVal = atomic.get(); - - assert curAtomicVal + delta == atomic.addAndGet(delta); - assert curAtomicVal + delta == atomic.get(); - } - - /** - * @throws Exception If failed. - */ - public void testGetAndSet() throws Exception { - getAndSet("local"); - - getAndSet("replicated"); - - getAndSet("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - private void getAndSet(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - IgniteAtomicLong atomic = cache.dataStructures().atomicLong("atomic", 0, true); - - assertEquals(0, cache.primarySize()); - - long newVal = RND.nextLong(); - - long curAtomicVal = atomic.get(); - - assert curAtomicVal == atomic.getAndSet(newVal); - assert newVal == atomic.get(); - } - - /** - * @throws Exception If failed. - */ - public void testCompareAndSet() throws Exception { - compareAndSet("local"); - - compareAndSet("replicated"); - - compareAndSet("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - @SuppressWarnings({"NullableProblems", "ConstantConditions"}) - private void compareAndSet(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - IgniteAtomicLong atomic = cache.dataStructures().atomicLong("atomic", 0, true); - - assertEquals(0, cache.primarySize()); - - long newVal = RND.nextLong(); - - final long oldVal = atomic.get(); - - // Don't set new value. - assert !atomic.compareAndSet(oldVal - 1, newVal); - - assert oldVal == atomic.get(); - - // Set new value. - assert atomic.compareAndSet(oldVal, newVal); - - assert newVal == atomic.get(); - } - - /** - * @throws Exception If failed. - */ - public void testGetAndSetInTx() throws Exception { - getAndSetInTx("local"); - - getAndSetInTx("replicated"); - - getAndSetInTx("partitioned"); - } - - /** - * @param cacheName Cache name. - * @throws Exception If failed. - */ - private void getAndSetInTx(String cacheName) throws Exception { - info("Running test [name=" + getName() + ", cache=" + cacheName + ']'); - - GridCache cache = grid().cache(cacheName); - - assertNotNull(cache); - assertEquals(0, cache.primarySize()); - - IgniteAtomicLong atomic = cache.dataStructures().atomicLong("atomic", 0, true); - - assertEquals(0, cache.primarySize()); - - try (IgniteTx tx = cache.txStart()) { - long newVal = RND.nextLong(); - - long curAtomicVal = atomic.get(); - - assert curAtomicVal == atomic.getAndSet(newVal); - assert newVal == atomic.get(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/349d51ed/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicReferenceApiSelfAbstractTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicReferenceApiSelfAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicReferenceApiSelfAbstractTest.java index 75c8f46..e46222f 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicReferenceApiSelfAbstractTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicReferenceApiSelfAbstractTest.java @@ -18,95 +18,16 @@ package org.apache.ignite.internal.processors.cache.datastructures; import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.GridCache; -import org.apache.ignite.cache.store.*; -import org.apache.ignite.configuration.*; -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.testframework.junits.common.*; -import org.mockito.*; - -import javax.cache.configuration.*; -import javax.cache.integration.*; -import java.util.*; -import java.util.concurrent.atomic.*; -import static org.apache.ignite.cache.CacheAtomicityMode.*; -import static org.apache.ignite.cache.CacheDistributionMode.*; +import java.util.*; /** * Basic tests for atomic reference. */ -public abstract class GridCacheAtomicReferenceApiSelfAbstractTest extends GridCommonAbstractTest { - /** */ - protected static TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); - - /** */ - private AtomicBoolean storeCalled = new AtomicBoolean(); - - /** - * Constructs a test. - */ - protected GridCacheAtomicReferenceApiSelfAbstractTest() { - super(true /* start grid. */); - } - - /** {@inheritDoc} */ - @Override protected void beforeTest() throws Exception { - MockitoAnnotations.initMocks(this); - } - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration() throws Exception { - IgniteConfiguration cfg = super.getConfiguration(); - - TcpDiscoverySpi spi = new TcpDiscoverySpi(); - - spi.setIpFinder(ipFinder); - - cfg.setDiscoverySpi(spi); - - return cfg; - } - - /** - * @return Cache configuration for the test. - */ - @SuppressWarnings("unchecked") - protected CacheConfiguration getCacheConfiguration() { - CacheConfiguration ccfg = defaultCacheConfiguration(); - - ccfg.setAtomicityMode(TRANSACTIONAL); - ccfg.setDistributionMode(NEAR_PARTITIONED); - - ccfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory(new CacheStoreAdapter() { - @Override public Object load(Object key) throws CacheLoaderException { - storeCalled.set(true); - - return null; - } - - @Override public void write(javax.cache.Cache.Entry entry) throws CacheWriterException { - storeCalled.set(true); - } - - @Override public void delete(Object key) throws CacheWriterException { - storeCalled.set(true); - } - })); - - ccfg.setReadThrough(true); - ccfg.setWriteThrough(true); - ccfg.setLoadPreviousValue(true); - - return ccfg; - } - +public abstract class GridCacheAtomicReferenceApiSelfAbstractTest extends IgniteAtomicsAbstractTest { /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); + @Override protected int gridCount() { + return 1; } /** @@ -122,21 +43,24 @@ public abstract class GridCacheAtomicReferenceApiSelfAbstractTest extends GridCo String atomicName2 = UUID.randomUUID().toString(); String initVal = "1"; - IgniteAtomicReference<String> atomic1 = grid().cache(null).dataStructures() - .atomicReference(atomicName1, initVal, true); - IgniteAtomicReference<String> atomic2 = grid().cache(null).dataStructures() - .atomicReference(atomicName2, null, true); + IgniteAtomicReference<String> atomic1 = grid(0).atomicReference(atomicName1, initVal, true); + IgniteAtomicReference<String> atomic2 = grid(0).atomicReference(atomicName2, null, true); assertNotNull(atomic1); assertNotNull(atomic2); - assert grid().cache(null).dataStructures().removeAtomicReference(atomicName1); - assert grid().cache(null).dataStructures().removeAtomicReference(atomicName2); - assert !grid().cache(null).dataStructures().removeAtomicReference(atomicName1); - assert !grid().cache(null).dataStructures().removeAtomicReference(atomicName2); + atomic1.close(); + atomic2.close(); + + atomic1.close(); + atomic2.close(); + + assertNull(grid(0).atomicReference(atomicName1, null, false)); + assertNull(grid(0).atomicReference(atomicName2, null, false)); try { atomic1.get(); + fail(); } catch (IgniteCheckedException e) { @@ -154,8 +78,7 @@ public abstract class GridCacheAtomicReferenceApiSelfAbstractTest extends GridCo String initVal = "qwerty"; - IgniteAtomicReference<String> atomic = grid().cache(null).dataStructures() - .atomicReference(atomicName, initVal, true); + IgniteAtomicReference<String> atomic = grid(0).atomicReference(atomicName, initVal, true); assertEquals(initVal, atomic.get()); @@ -174,8 +97,7 @@ public abstract class GridCacheAtomicReferenceApiSelfAbstractTest extends GridCo String initVal = "qwerty"; - IgniteAtomicReference<String> atomic = grid().cache(null).dataStructures() - .atomicReference(atomicName, initVal, true); + IgniteAtomicReference<String> atomic = grid(0).atomicReference(atomicName, initVal, true); assertEquals(initVal, atomic.get()); @@ -187,26 +109,4 @@ public abstract class GridCacheAtomicReferenceApiSelfAbstractTest extends GridCo assertEquals(null, atomic.get()); } - - /** - * Tests that non-persistent atomic reference doesn't ever - * hit the store. - * - * @throws IgniteCheckedException If failed. - */ - public void testNonPersistentMode() throws IgniteCheckedException { - String atomicName = UUID.randomUUID().toString(); - - GridCache<Object, Object> cache = grid().cache(null); - - assertNotNull(cache); - - IgniteAtomicReference<Boolean> atomic = cache.dataStructures().atomicReference(atomicName, false, true); - - atomic.set(true); - - cache.dataStructures().removeAtomicReference(atomicName); - - assertFalse(storeCalled.get()); - } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/349d51ed/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicStampedApiSelfAbstractTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicStampedApiSelfAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicStampedApiSelfAbstractTest.java index 984f434..31e4c0d 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicStampedApiSelfAbstractTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheAtomicStampedApiSelfAbstractTest.java @@ -18,44 +18,16 @@ package org.apache.ignite.internal.processors.cache.datastructures; import org.apache.ignite.*; -import org.apache.ignite.configuration.*; -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.testframework.junits.common.*; import java.util.*; /** * Basic tests for atomic stamped. */ -public abstract class GridCacheAtomicStampedApiSelfAbstractTest extends GridCommonAbstractTest { - /** */ - protected static TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); - - /** - * Constructs a test. - */ - protected GridCacheAtomicStampedApiSelfAbstractTest() { - super(true /* start grid. */); - } - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration() throws Exception { - IgniteConfiguration cfg = super.getConfiguration(); - - TcpDiscoverySpi spi = new TcpDiscoverySpi(); - - spi.setIpFinder(ipFinder); - - cfg.setDiscoverySpi(spi); - - return cfg; - } - +public abstract class GridCacheAtomicStampedApiSelfAbstractTest extends IgniteAtomicsAbstractTest { /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); + @Override protected int gridCount() { + return 1; } /** @@ -70,10 +42,8 @@ public abstract class GridCacheAtomicStampedApiSelfAbstractTest extends GridComm String initVal = "1"; String initStamp = "2"; - IgniteAtomicStamped<String, String> atomic1 = grid().cache(null).dataStructures() - .atomicStamped(atomicName1, initVal, initStamp, true); - IgniteAtomicStamped<String, String> atomic2 = grid().cache(null).dataStructures() - .atomicStamped(atomicName1, null, null, true); + IgniteAtomicStamped<String, String> atomic1 = grid(0).atomicStamped(atomicName1, initVal, initStamp, true); + IgniteAtomicStamped<String, String> atomic2 = grid(0).atomicStamped(atomicName1, null, null, true); assertNotNull(atomic1); assertNotNull(atomic2); @@ -83,11 +53,14 @@ public abstract class GridCacheAtomicStampedApiSelfAbstractTest extends GridComm assert initVal.equals(atomic2.value()); assert initStamp.equals(atomic2.stamp()); - assert grid().cache(null).dataStructures().removeAtomicStamped(atomicName1); - assert !grid().cache(null).dataStructures().removeAtomicStamped(atomicName1); + atomic1.close(); + atomic2.close(); + + assertNull(grid(0).atomicStamped(atomicName1, null, null, false)); try { atomic1.get(); + fail(); } catch (IgniteCheckedException e) { @@ -106,8 +79,7 @@ public abstract class GridCacheAtomicStampedApiSelfAbstractTest extends GridComm String initVal = "qwerty"; String initStamp = "asdfgh"; - IgniteAtomicStamped<String, String> atomic = grid().cache(null).dataStructures() - .atomicStamped(atomicName, initVal, initStamp, true); + IgniteAtomicStamped<String, String> atomic = grid(0).atomicStamped(atomicName, initVal, initStamp, true); assertEquals(initVal, atomic.value()); assertEquals(initStamp, atomic.stamp()); @@ -131,8 +103,7 @@ public abstract class GridCacheAtomicStampedApiSelfAbstractTest extends GridComm String initVal = "qwerty"; String initStamp = "asdfgh"; - IgniteAtomicStamped<String, String> atomic = grid().cache(null).dataStructures() - .atomicStamped(atomicName, initVal, initStamp, true); + IgniteAtomicStamped<String, String> atomic = grid(0).atomicStamped(atomicName, initVal, initStamp, true); assertEquals(initVal, atomic.value()); assertEquals(initStamp, atomic.stamp()); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/349d51ed/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheCountDownLatchAbstractSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheCountDownLatchAbstractSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheCountDownLatchAbstractSelfTest.java new file mode 100644 index 0000000..e799a98 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/datastructures/GridCacheCountDownLatchAbstractSelfTest.java @@ -0,0 +1,270 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.cache.datastructures; + +import org.apache.ignite.*; +import org.apache.ignite.internal.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.resources.*; +import org.apache.ignite.internal.util.typedef.*; +import org.apache.ignite.testframework.*; +import org.jetbrains.annotations.*; + +import java.io.*; +import java.util.*; +import java.util.concurrent.*; + +import static java.util.concurrent.TimeUnit.*; + +/** + * Cache count down latch self test. + */ +public abstract class GridCacheCountDownLatchAbstractSelfTest extends IgniteAtomicsAbstractTest + implements Externalizable { + /** */ + private static final int NODES_CNT = 4; + + /** */ + protected static final int THREADS_CNT = 5; + + /** */ + private static final Random RND = new Random(); + + /** {@inheritDoc} */ + @Override protected int gridCount() { + return NODES_CNT; + } + + /** + * @throws Exception If failed. + */ + public void testLatch() throws Exception { + checkLatch(); + } + + /** + * @throws Exception If failed. + */ + private void checkLatch() throws Exception { + // Test API. + checkAutoDelete(); + + checkAwait(); + + checkCountDown(); + + // Test main functionality. + IgniteCountDownLatch latch1 = grid(0).countDownLatch("latch", 2, false, true); + + assertEquals(2, latch1.count()); + + IgniteCompute comp = grid(0).compute().enableAsync(); + + comp.call(new IgniteCallable<Object>() { + @IgniteInstanceResource + private Ignite ignite; + + @IgniteLoggerResource + private IgniteLogger log; + + @Nullable @Override public Object call() throws Exception { + // Test latch in multiple threads on each node. + IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync( + new Callable<Object>() { + @Nullable @Override public Object call() throws Exception { + IgniteCountDownLatch latch = ignite.countDownLatch("latch", 2, false, true); + + assert latch != null && latch.count() == 2; + + log.info("Thread is going to wait on latch: " + Thread.currentThread().getName()); + + assert latch.await(1, MINUTES); + + log.info("Thread is again runnable: " + Thread.currentThread().getName()); + + return null; + } + }, + 5, + "test-thread" + ); + + fut.get(); + + return null; + } + }); + + IgniteFuture<Object> fut = comp.future(); + + Thread.sleep(3000); + + assert latch1.countDown() == 1; + + assert latch1.countDown() == 0; + + // Ensure there are no hangs. + fut.get(); + + // Test operations on removed latch. + latch1.close(); + + checkRemovedLatch(latch1); + } + + /** + * @param latch Latch. + * + * @throws Exception If failed. + */ + protected void checkRemovedLatch(IgniteCountDownLatch latch) throws Exception { + assert latch.removed(); + + assert latch.count() == 0; + + // Test await on removed future. + latch.await(); + assert latch.await(10); + assert latch.await(10, SECONDS); + + latch.await(); + + // Test countdown. + assert latch.countDown() == 0; + assert latch.countDown(5) == 0; + latch.countDownAll(); + } + + /** + * @throws Exception Exception. + */ + private void checkAutoDelete() throws Exception { + IgniteCountDownLatch latch = createLatch("rmv", 5, true); + + latch.countDownAll(); + + // Latch should be removed since autoDelete = true + checkRemovedLatch(latch); + + IgniteCountDownLatch latch1 = createLatch("rmv1", 5, false); + + latch1.countDownAll(); + + // Latch should NOT be removed since autoDelete = false + assert !latch1.removed(); + + removeLatch("rmv1"); + } + + /** + * @throws Exception Exception. + */ + private void checkAwait() throws Exception { + // Check only 'false' cases here. Successful await is tested over the grid. + IgniteCountDownLatch latch = createLatch("await", 5, true); + + assert !latch.await(10); + assert !latch.await(10, MILLISECONDS); + + removeLatch("await"); + } + + /** + * @throws Exception Exception. + */ + private void checkCountDown() throws Exception { + IgniteCountDownLatch latch = createLatch("cnt", 10, true); + + assert latch.countDown() == 9; + assert latch.countDown(2) == 7; + + latch.countDownAll(); + + assert latch.count() == 0; + + checkRemovedLatch(latch); + + IgniteCountDownLatch latch1 = createLatch("cnt1", 10, true); + + assert latch1.countDown() == 9; + assert latch1.countDown(2) == 7; + + latch1.countDownAll(); + + assert latch1.count() == 0; + + checkRemovedLatch(latch1); + } + + /** + * @param latchName Latch name. + * @param cnt Count. + * @param autoDel Auto delete flag. + * @throws Exception If failed. + * @return New latch. + */ + private IgniteCountDownLatch createLatch(String latchName, int cnt, boolean autoDel) + throws Exception { + IgniteCountDownLatch latch = grid(RND.nextInt(NODES_CNT)).countDownLatch(latchName, cnt, autoDel, true); + + // Test initialization. + assert latchName.equals(latch.name()); + assert latch.count() == cnt; + assert latch.initialCount() == cnt; + assert latch.autoDelete() == autoDel; + + return latch; + } + + /** + * @param latchName Latch name. + * @throws Exception If failed. + */ + private void removeLatch(String latchName) + throws Exception { + IgniteCountDownLatch latch = grid(RND.nextInt(NODES_CNT)).countDownLatch(latchName, 10, false, true); + + assert latch != null; + + if (latch.count() > 0) + latch.countDownAll(); + + // Remove latch on random node. + IgniteCountDownLatch latch0 = grid(RND.nextInt(NODES_CNT)).countDownLatch(latchName, 0, false, false); + + assertNotNull(latch0); + + latch0.close(); + + // Ensure latch is removed on all nodes. + for (Ignite g : G.allGrids()) + assertNull(((GridKernal)g).context().dataStructures().countDownLatch(latchName, 10, true, false)); + + checkRemovedLatch(latch); + } + + /** {@inheritDoc} */ + @Override public void writeExternal(ObjectOutput out) throws IOException { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + // No-op. + } +}