IGNITE-209 - Fixed 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/13475f3d Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/13475f3d Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/13475f3d Branch: refs/heads/ignite-128 Commit: 13475f3db38f5aee7bbc81c220334ad71b2ed4b8 Parents: 6a90e0f Author: Alexey Goncharuk <agoncha...@gridgain.com> Authored: Mon Feb 9 15:11:54 2015 -0800 Committer: Alexey Goncharuk <agoncha...@gridgain.com> Committed: Mon Feb 9 15:11:54 2015 -0800 ---------------------------------------------------------------------- .../cache/GridCacheAbstractFullApiSelfTest.java | 41 ++-- ...tomicClientOnlyMultiNodeFullApiSelfTest.java | 10 + ...idCacheNearOnlyMultiNodeFullApiSelfTest.java | 185 +++++++++++++++++++ 3 files changed, 222 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/13475f3d/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java index 7d26d87..c468b13 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java @@ -2957,8 +2957,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract /** * @throws Exception If failed. */ - // TODO: IGNITE-209: Enable when fixed. - public void _testTtlTx() throws Exception { + public void testTtlTx() throws Exception { if (txEnabled()) checkTtl(true, false); } @@ -2966,8 +2965,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract /** * @throws Exception If failed. */ - // TODO: IGNITE-209: Enable when fixed. - public void _testTtlNoTx() throws Exception { + public void testTtlNoTx() throws Exception { checkTtl(false, false); } @@ -3009,7 +3007,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract IgniteTx tx = transactions().txStart(); try { - grid(0).jcache(null).withExpiryPolicy(expiry).put(key, 1); + jcache().withExpiryPolicy(expiry).put(key, 1); } finally { tx.rollback(); @@ -3023,11 +3021,14 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract IgniteTx tx = inTx ? transactions().txStart() : null; try { - grid(0).jcache(null).withExpiryPolicy(expiry).put(key, 1); + jcache().withExpiryPolicy(expiry).put(key, 1); + + if (tx != null) + tx.commit(); } finally { if (tx != null) - tx.commit(); + tx.close(); } long[] expireTimes = new long[gridCount()]; @@ -3050,11 +3051,14 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract tx = inTx ? transactions().txStart() : null; try { - grid(0).jcache(null).withExpiryPolicy(expiry).put(key, 2); + jcache().withExpiryPolicy(expiry).put(key, 2); + + if (tx != null) + tx.commit(); } finally { if (tx != null) - tx.commit(); + tx.close(); } for (int i = 0; i < gridCount(); i++) { @@ -3075,11 +3079,14 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract tx = inTx ? transactions().txStart() : null; try { - grid(0).jcache(null).withExpiryPolicy(expiry).put(key, 3); + jcache().withExpiryPolicy(expiry).put(key, 3); + + if (tx != null) + tx.commit(); } finally { if (tx != null) - tx.commit(); + tx.close(); } for (int i = 0; i < gridCount(); i++) { @@ -3102,11 +3109,14 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract tx = inTx ? transactions().txStart() : null; try { - c.put(key, 4); + jcache().put(key, 4); + + if (tx != null) + tx.commit(); } finally { if (tx != null) - tx.commit(); + tx.close(); } log.info("Put 4 done"); @@ -3166,10 +3176,13 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract try { entry.set(10); + + if (tx != null) + tx.commit(); } finally { if (tx != null) - tx.commit(); + tx.close(); } U.sleep(2000); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/13475f3d/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheAtomicClientOnlyMultiNodeFullApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheAtomicClientOnlyMultiNodeFullApiSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheAtomicClientOnlyMultiNodeFullApiSelfTest.java index 17aa98c..8c18258 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheAtomicClientOnlyMultiNodeFullApiSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheAtomicClientOnlyMultiNodeFullApiSelfTest.java @@ -72,6 +72,16 @@ public class GridCacheAtomicClientOnlyMultiNodeFullApiSelfTest extends GridCache } /** {@inheritDoc} */ + @Override public void testReaderTtlNoTx() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testReaderTtlTx() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ @Override public void testSize() throws Exception { IgniteCache<String, Integer> cache = jcache(); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/13475f3d/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOnlyMultiNodeFullApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOnlyMultiNodeFullApiSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOnlyMultiNodeFullApiSelfTest.java index bc83d15..99d27d6 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOnlyMultiNodeFullApiSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOnlyMultiNodeFullApiSelfTest.java @@ -24,15 +24,20 @@ import org.apache.ignite.configuration.*; import org.apache.ignite.events.*; import org.apache.ignite.internal.*; import org.apache.ignite.internal.processors.cache.*; +import org.apache.ignite.internal.util.lang.*; import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.lang.*; +import org.apache.ignite.testframework.*; +import org.apache.ignite.transactions.*; +import javax.cache.expiry.*; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import static org.apache.ignite.cache.CacheAtomicWriteOrderMode.*; import static org.apache.ignite.cache.CacheDistributionMode.*; import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; @@ -183,6 +188,186 @@ public class GridCacheNearOnlyMultiNodeFullApiSelfTest extends GridCachePartitio // TODO fix this test for client mode. } + /** + * @throws Exception If failed. + */ + public void testReaderTtlTx() throws Exception { + checkReaderTtl(true); + } + + /** + * @throws Exception If failed. + */ + public void testReaderTtlNoTx() throws Exception { + checkReaderTtl(false); + } + + /** + * + * @throws Exception + */ + private void checkReaderTtl(boolean inTx) throws Exception { + int ttl = 1000; + + final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl)); + + final GridCache<String, Integer> c = cache(); + + final String key = primaryKeysForCache(jcache(), 1).get(0); + + c.put(key, 1); + + CacheEntry<String, Integer> entry = c.entry(key); + + assert entry != null; + + assertEquals(0, entry.timeToLive()); + assertEquals(0, entry.expirationTime()); + + long startTime = System.currentTimeMillis(); + + int fullIdx = nearIdx == 0 ? 1 : 0; + + // Now commit transaction and check that ttl and expire time have been saved. + IgniteTx tx = inTx ? grid(fullIdx).transactions().txStart() : null; + + try { + jcache(fullIdx).withExpiryPolicy(expiry).put(key, 1); + + if (tx != null) + tx.commit(); + } + finally { + if (tx != null) + tx.close(); + } + + long[] expireTimes = new long[gridCount()]; + + for (int i = 0; i < gridCount(); i++) { + CacheEntry<String, Integer> curEntry = cache(i).entry(key); + + if (curEntry.primary() || curEntry.backup() || i == nearIdx) { + assertEquals(ttl, curEntry.timeToLive()); + + assert curEntry.expirationTime() > startTime; + + expireTimes[i] = curEntry.expirationTime(); + } + } + + // One more update from the same cache entry to ensure that expire time is shifted forward. + U.sleep(100); + + tx = inTx ? grid(fullIdx).transactions().txStart() : null; + + try { + jcache(fullIdx).withExpiryPolicy(expiry).put(key, 2); + + if (tx != null) + tx.commit(); + } + finally { + if (tx != null) + tx.close(); + } + + for (int i = 0; i < gridCount(); i++) { + CacheEntry<String, Integer> curEntry = cache(i).entry(key); + + if (curEntry.primary() || curEntry.backup() || i == nearIdx) { + assertEquals(ttl, curEntry.timeToLive()); + + assert curEntry.expirationTime() > expireTimes[i]; + + expireTimes[i] = curEntry.expirationTime(); + } + } + + // And one more update to ensure that ttl is not changed and expire time is not shifted forward. + U.sleep(100); + + tx = inTx ? grid(fullIdx).transactions().txStart() : null; + + try { + jcache(fullIdx).put(key, 4); + } + finally { + if (tx != null) + tx.commit(); + } + + for (int i = 0; i < gridCount(); i++) { + CacheEntry<String, Integer> curEntry = cache(i).entry(key); + + if (curEntry.primary() || curEntry.backup() || i == nearIdx) { + assertEquals(ttl, curEntry.timeToLive()); + assertEquals(expireTimes[i], curEntry.expirationTime()); + } + } + + // Avoid reloading from store. + map.remove(key); + + assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() { + @SuppressWarnings("unchecked") + @Override public boolean applyx() throws IgniteCheckedException { + try { + Integer val = c.get(key); + + if (val != null) { + info("Value is in cache [key=" + key + ", val=" + val + ']'); + + return false; + } + + // Get "cache" field from GridCacheProxyImpl. + GridCacheAdapter c0 = GridTestUtils.getFieldValue(c, "cache"); + + if (!c0.context().deferredDelete()) { + GridCacheEntryEx e0 = c0.peekEx(key); + + return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null); + } + else + return true; + } + catch (GridCacheEntryRemovedException e) { + throw new RuntimeException(e); + } + } + }, Math.min(ttl * 10, getTestTimeout()))); + + // Ensure that old TTL and expire time are not longer "visible". + entry = c.entry(key); + + assert entry.get() == null; + + assertEquals(0, entry.timeToLive()); + assertEquals(0, entry.expirationTime()); + + // Ensure that next update will not pick old expire time. + + tx = inTx ? c.txStart() : null; + + try { + entry.set(10); + } + finally { + if (tx != null) + tx.commit(); + } + + U.sleep(2000); + + entry = c.entry(key); + + assertEquals((Integer)10, entry.get()); + + assertEquals(0, entry.timeToLive()); + assertEquals(0, entry.expirationTime()); + } + /** {@inheritDoc} */ @Override public void testClear() throws Exception { IgniteCache<String, Integer> nearCache = jcache();