IGNITE-59 Support lock, lockAll: Fix 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/2d69d237 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/2d69d237 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/2d69d237 Branch: refs/heads/ignite-1 Commit: 2d69d23746f834ea1a4442f01fe4f1fe4e0f90c7 Parents: 7737e75 Author: sevdokimov <sevdoki...@gridgain.com> Authored: Thu Jan 15 19:02:31 2015 +0300 Committer: sevdokimov <sevdoki...@gridgain.com> Committed: Fri Jan 16 15:45:41 2015 +0300 ---------------------------------------------------------------------- .../cache/GridCacheAbstractFullApiSelfTest.java | 115 +++++-------------- .../cache/GridCacheBasicApiAbstractTest.java | 8 +- .../GridCacheLocalMultithreadedSelfTest.java | 28 +++-- 3 files changed, 49 insertions(+), 102 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2d69d237/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java index 4447383..2fb802b 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java @@ -17,6 +17,7 @@ package org.gridgain.grid.kernal.processors.cache; +import com.google.common.collect.*; import org.apache.ignite.*; import org.apache.ignite.cluster.*; import org.apache.ignite.configuration.*; @@ -278,11 +279,13 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract */ public void testRemoveInExplicitLocks() throws Exception { if (lockingEnabled()) { - GridCache<String, Integer> cache = cache(); + IgniteCache<String, Integer> cache = jcache(); cache.put("a", 1); - cache.lockAll(F.asList("a", "b", "c", "d"), 0); + Lock lock = cache.lockAll(ImmutableSet.of("a", "b", "c", "d")); + + lock.lock(); try { cache.remove("a"); @@ -291,7 +294,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract cache.putAll(F.asMap("b", 2, "c", 3, "d", 4)); } finally { - cache.unlockAll(F.asList("a", "b", "c", "d")); + lock.unlock(); } } } @@ -3693,109 +3696,47 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract @SuppressWarnings("BusyWait") public void testLockUnlockAll() throws Exception { if (lockingEnabled()) { - cache().put("key1", 1); - cache().put("key2", 2); + IgniteCache<String, Integer> cache = jcache(); - assert !cache().isLocked("key1"); - assert !cache().isLocked("key2"); + cache.put("key1", 1); + cache.put("key2", 2); + + assert !cache.isLocked("key1"); + assert !cache.isLocked("key2"); - cache().lockAll(F.asList("key1", "key2"), 0); + cache.lockAll(ImmutableSet.of("key1", "key2")).lock(); - assert cache().isLocked("key1"); - assert cache().isLocked("key2"); + assert cache.isLocked("key1"); + assert cache.isLocked("key2"); - cache().unlockAll(F.asList("key1", "key2")); + cache.lockAll(ImmutableSet.of("key1", "key2")).unlock(); for (int i = 0; i < 100; i++) - if (cache().isLocked("key1") || cache().isLocked("key2")) + if (cache.isLocked("key1") || cache.isLocked("key2")) Thread.sleep(10); else break; - assert !cache().isLocked("key1"); - assert !cache().isLocked("key2"); + assert !cache.isLocked("key1"); + assert !cache.isLocked("key2"); - cache().lockAll(F.asList("key1", "key2"), 0); + Lock lock = cache.lockAll(ImmutableSet.of("key1", "key2")); - assert cache().isLocked("key1"); - assert cache().isLocked("key2"); + lock.lock(); - cache().unlockAll(F.asList("key1", "key2")); + assert cache.isLocked("key1"); + assert cache.isLocked("key2"); + + lock.unlock(); for (int i = 0; i < 100; i++) - if (cache().isLocked("key1") || cache().isLocked("key2")) + if (cache.isLocked("key1") || cache.isLocked("key2")) Thread.sleep(10); else break; - assert !cache().isLocked("key1"); - assert !cache().isLocked("key2"); - } - } - - /** - * @throws Exception In case of error. - */ - @SuppressWarnings("BusyWait") - public void testLockAllFiltered() throws Exception { - if (lockingEnabled()) { - cache().put("key1", 1); - cache().put("key2", 2); - cache().put("key3", 100); - cache().put("key4", 101); - - assert !cache().isLocked("key1"); - assert !cache().isLocked("key2"); - assert !cache().isLocked("key3"); - assert !cache().isLocked("key4"); - - assert !cache().isLockedByThread("key1"); - assert !cache().isLockedByThread("key2"); - assert !cache().isLockedByThread("key3"); - assert !cache().isLockedByThread("key4"); - - assert !cache().projection(gte100).lockAll(F.asList("key2", "key3"), 0); - - assert !cache().isLocked("key1"); - assert !cache().isLocked("key2"); - assert !cache().isLocked("key3"); - assert !cache().isLocked("key4"); - - assert !cache().isLockedByThread("key1"); - assert !cache().isLockedByThread("key2"); - assert !cache().isLockedByThread("key3"); - assert !cache().isLockedByThread("key4"); - - assert cache().projection(F.<GridCacheEntry<String, Integer>>alwaysTrue()).lockAll( - F.asList("key1", "key2", "key3", "key4"), 0); - - assert cache().isLocked("key1"); - assert cache().isLocked("key2"); - assert cache().isLocked("key3"); - assert cache().isLocked("key4"); - - assert cache().isLockedByThread("key1"); - assert cache().isLockedByThread("key2"); - assert cache().isLockedByThread("key3"); - assert cache().isLockedByThread("key4"); - - cache().unlockAll(F.asList("key1", "key2", "key3", "key4"), - F.<GridCacheEntry<String, Integer>>alwaysTrue()); - - for (String key : cache().primaryKeySet()) { - for (int i = 0; i < 100; i++) - if (cache().isLocked(key)) - Thread.sleep(10); - else - break; - - assert !cache().isLocked(key); - } - - assert !cache().isLockedByThread("key1"); - assert !cache().isLockedByThread("key2"); - assert !cache().isLockedByThread("key3"); - assert !cache().isLockedByThread("key4"); + assert !cache.isLocked("key1"); + assert !cache.isLocked("key2"); } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2d69d237/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheBasicApiAbstractTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheBasicApiAbstractTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheBasicApiAbstractTest.java index 54c141e..2e5d00e 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheBasicApiAbstractTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheBasicApiAbstractTest.java @@ -449,13 +449,13 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe * @throws Exception If test failed. */ public void testMultiLocks() throws Exception { - GridCache<Integer, String> cache = ignite.cache(null); + IgniteCache<Integer, String> cache = ignite.jcache(null); - Collection<Integer> keys = new ArrayList<>(3); + Set<Integer> keys = new HashSet<>(); Collections.addAll(keys, 1, 2, 3); - assert cache.lockAll(keys, 0); + cache.lockAll(keys).lock(); assert cache.isLocked(1); assert cache.isLocked(2); @@ -465,7 +465,7 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe assert cache.isLockedByThread(2); assert cache.isLockedByThread(3); - cache.unlockAll(keys); + cache.lockAll(keys).unlock(); assert !cache.isLocked(1); assert !cache.isLocked(2); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2d69d237/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java index 1a42c15..624c5be 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java @@ -109,14 +109,16 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest * @throws Exception If test fails. */ public void testMultiLocks() throws Throwable { + final IgniteCache<Integer, String> cache = grid().jcache(null); + GridTestUtils.runMultiThreaded(new Callable<Object>() { /** {@inheritDoc} */ @Override public Object call() throws Exception { - Collection<Integer> keys = new ArrayList<>(); + Set<Integer> keys = new HashSet<Integer>(); Collections.addAll(keys, 1, 2, 3); - assert cache.lockAll(keys, 0); + cache.lockAll(keys).lock(); info("Locked keys from thread [keys=" + keys + ", thread=" + thread() + ']'); @@ -124,7 +126,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest info("Unlocking key from thread: " + thread()); - cache.unlockAll(keys); + cache.lockAll(keys).unlock(); info("Unlocked keys from thread: " + thread()); @@ -137,6 +139,8 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest * @throws Exception If test fails. */ public void testSlidingKeysLocks() throws Throwable { + final IgniteCache<Integer, String> cache = grid().jcache(null); + final AtomicInteger cnt = new AtomicInteger(); GridTestUtils.runMultiThreaded(new Callable<Object>() { @@ -144,11 +148,11 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest @Override public Object call() throws Exception { int idx = cnt.incrementAndGet(); - Collection<Integer> keys = new ArrayList<>(); + Set<Integer> keys = new HashSet<>(); Collections.addAll(keys, idx, idx + 1, idx + 2, idx + 3); - assert cache.lockAll(keys, 0); + cache.lockAll(keys).lock(); info("Locked keys from thread [keys=" + keys + ", thread=" + thread() + ']'); @@ -156,7 +160,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest info("Unlocking key from thread [keys=" + keys + ", thread=" + thread() + ']'); - cache.unlockAll(keys); + cache.lockAll(keys).unlock(); info("Unlocked keys from thread [keys=" + keys + ", thread=" + thread() + ']'); @@ -235,14 +239,16 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest * @throws Exception If test fails. */ public void testMultiLockTimeout() throws Exception { + final IgniteCache<Integer, String> cache = grid().jcache(null); + final CountDownLatch l1 = new CountDownLatch(1); final CountDownLatch l2 = new CountDownLatch(1); final CountDownLatch l3 = new CountDownLatch(1); final AtomicInteger cnt = new AtomicInteger(); - final Collection<Integer> keys1 = new ArrayList<>(); - final Collection<Integer> keys2 = new ArrayList<>(); + final Set<Integer> keys1 = new HashSet<>(); + final Set<Integer> keys2 = new HashSet<>(); GridTestThread t1 = new GridTestThread(new Callable<Object>() { /** {@inheritDoc} */ @@ -253,7 +259,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest Collections.addAll(keys1, idx, idx + 1, idx + 2, idx + 3); - assert cache.lockAll(keys1, 0); + cache.lockAll(keys1).lock(); for (Integer key : keys1) { assert cache.isLocked(key) : "Failed to acquire lock for key: " + key; @@ -264,7 +270,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest l2.await(); - cache.unlockAll(keys1); + cache.lockAll(keys1).unlock(); for (Integer key : keys1) { assert !cache.isLocked(key); @@ -292,7 +298,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest } // Lock won't be acquired due to timeout. - assert !cache.lockAll(keys2, 100); + assert !cache.lockAll(keys2).tryLock(100, TimeUnit.MILLISECONDS); for (Integer key : keys2) { boolean locked = cache.isLocked(key);