Skip to content

Commit

Permalink
# IGNITE-56 Use IgniteCache instead of GridCache in the tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sevdokimov committed Feb 8, 2015
1 parent 6bbaa99 commit cd72342
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 229 deletions.
Expand Up @@ -194,11 +194,11 @@ public void testSize() throws Exception {
// Will actually delete entry from map. // Will actually delete entry from map.
CU.invalidate(cache(i), "key0"); CU.invalidate(cache(i), "key0");


assertNull("Failed check for grid: " + i, cache(i).peek("key0")); assertNull("Failed check for grid: " + i, jcache(i).localPeek("key0", CachePeekMode.ONHEAP));


Collection<String> keysCol = mapped.get(grid(i).localNode()); Collection<String> keysCol = mapped.get(grid(i).localNode());


assert !cache(i).isEmpty() || F.isEmpty(keysCol); assert jcache(i).localSize() != 0 || F.isEmpty(keysCol);
} }


for (int i = 0; i < gridCount(); i++) { for (int i = 0; i < gridCount(); i++) {
Expand All @@ -210,8 +210,7 @@ public void testSize() throws Exception {
if (ctx.affinity().localNode(key, ctx.discovery().topologyVersion())) if (ctx.affinity().localNode(key, ctx.discovery().topologyVersion()))
sum++; sum++;


assertEquals("Incorrect key size on cache #" + i, sum, cache(i).keySet().size()); assertEquals("Incorrect key size on cache #" + i, sum, jcache(i).localSize());
assertEquals("Incorrect key size on cache #" + i, sum, cache(i).size());
} }


for (int i = 0; i < gridCount(); i++) { for (int i = 0; i < gridCount(); i++) {
Expand Down Expand Up @@ -626,7 +625,7 @@ private void checkTransform(IgniteTxConcurrency concurrency, IgniteTxIsolation i
assertNull(cache.get("key3")); assertNull(cache.get("key3"));


for (int i = 0; i < gridCount(); i++) for (int i = 0; i < gridCount(); i++)
assertNull("Failed for cache: " + i, cache(i).peek("key3")); assertNull("Failed for cache: " + i, jcache(i).localPeek("key3", CachePeekMode.ONHEAP));


cache.remove("key1"); cache.remove("key1");
cache.put("key2", 1); cache.put("key2", 1);
Expand All @@ -641,7 +640,7 @@ private void checkTransform(IgniteTxConcurrency concurrency, IgniteTxIsolation i
assertNull(cache.get("key3")); assertNull(cache.get("key3"));


for (int i = 0; i < gridCount(); i++) for (int i = 0; i < gridCount(); i++)
assertNull(cache(i).peek("key3")); assertNull(jcache(i).localPeek("key3", CachePeekMode.ONHEAP));
} }


/** /**
Expand Down Expand Up @@ -711,9 +710,9 @@ private void checkTransformAll(IgniteTxConcurrency concurrency, IgniteTxIsolatio
Map<String, EntryProcessorResult<String>> res = cache.invokeAll(F.asSet("key1", "key2", "key3"), RMV_PROCESSOR); Map<String, EntryProcessorResult<String>> res = cache.invokeAll(F.asSet("key1", "key2", "key3"), RMV_PROCESSOR);


for (int i = 0; i < gridCount(); i++) { for (int i = 0; i < gridCount(); i++) {
assertNull(cache(i).peek("key1")); assertNull(jcache(i).localPeek("key1", CachePeekMode.ONHEAP));
assertNull(cache(i).peek("key2")); assertNull(jcache(i).localPeek("key2", CachePeekMode.ONHEAP));
assertNull(cache(i).peek("key3")); assertNull(jcache(i).localPeek("key3", CachePeekMode.ONHEAP));
} }


assertEquals("null", res.get("key1").get()); assertEquals("null", res.get("key1").get());
Expand Down Expand Up @@ -861,8 +860,8 @@ private void checkTransformSequential0(boolean startVal, IgniteTxConcurrency con
assertEquals(exp, cache.get("key")); assertEquals(exp, cache.get("key"));


for (int i = 0; i < gridCount(); i++) { for (int i = 0; i < gridCount(); i++) {
if (cache(i).affinity().isPrimaryOrBackup(grid(i).localNode(), "key")) if (ignite(i).affinity(null).isPrimaryOrBackup(grid(i).localNode(), "key"))
assertEquals(exp, peek(cache(i), "key")); assertEquals(exp, peek(jcache(i), "key"));
} }
} }


Expand Down Expand Up @@ -1059,7 +1058,7 @@ public void testInvokeAsync() throws Exception {
assertNull(cache.get("key3")); assertNull(cache.get("key3"));


for (int i = 0; i < gridCount(); i++) for (int i = 0; i < gridCount(); i++)
assertNull(cache(i).peek("key3")); assertNull(jcache(i).localPeek("key3", CachePeekMode.ONHEAP));
} }


/** /**
Expand Down Expand Up @@ -1098,7 +1097,7 @@ public void testInvoke() throws Exception {
assertNull(cache.get("k1")); assertNull(cache.get("k1"));


for (int i = 0; i < gridCount(); i++) for (int i = 0; i < gridCount(); i++)
assertNull(cache(i).peek("k1")); assertNull(jcache(i).localPeek("k1", CachePeekMode.ONHEAP));


final EntryProcessor<String, Integer, Integer> errProcessor = new EntryProcessor<String, Integer, Integer>() { final EntryProcessor<String, Integer, Integer> errProcessor = new EntryProcessor<String, Integer, Integer>() {
@Override public Integer process(MutableEntry<String, Integer> e, Object... args) { @Override public Integer process(MutableEntry<String, Integer> e, Object... args) {
Expand Down Expand Up @@ -2103,9 +2102,9 @@ public void testRemoveLoad() throws Exception {
String key = String.valueOf(i); String key = String.valueOf(i);


if (grid(0).affinity(null).mapKeyToPrimaryAndBackups(key).contains(grid(g).localNode())) if (grid(0).affinity(null).mapKeyToPrimaryAndBackups(key).contains(grid(g).localNode()))
assertEquals((Integer)i, cache(g).peek(key)); assertEquals((Integer)i, jcache(g).localPeek(key, CachePeekMode.ONHEAP));
else else
assertNull(cache(g).peek(key)); assertNull(jcache(g).localPeek(key, CachePeekMode.ONHEAP));
} }
} }
} }
Expand Down Expand Up @@ -2203,7 +2202,7 @@ public void testRemoveAll() throws Exception {
cache.put("key2", 2); cache.put("key2", 2);
cache.put("key3", 3); cache.put("key3", 3);


cache(gridCount() > 1 ? 1 : 0).removeAll(); jcache(gridCount() > 1 ? 1 : 0).removeAll();


assert cache.localSize() == 0; assert cache.localSize() == 0;
long entryCount = hugeRemoveAllEntryCount(); long entryCount = hugeRemoveAllEntryCount();
Expand Down Expand Up @@ -2454,10 +2453,10 @@ public void testClear() throws Exception {
assertNull(peek(cache, key)); assertNull(peek(cache, key));


for (i = 0; i < gridCount(); i++) for (i = 0; i < gridCount(); i++)
cache(i).clear(); jcache(i).clear();


for (i = 0; i < gridCount(); i++) for (i = 0; i < gridCount(); i++)
assert cache(i).isEmpty(); assert jcache(i).localSize() == 0;


for (Map.Entry<String, Integer> entry : vals.entrySet()) for (Map.Entry<String, Integer> entry : vals.entrySet())
cache.put(entry.getKey(), entry.getValue()); cache.put(entry.getKey(), entry.getValue());
Expand Down Expand Up @@ -2885,7 +2884,7 @@ public void testEvictExpired() throws Exception {
// Force reload on primary node. // Force reload on primary node.
for (int i = 0; i < gridCount(); i++) { for (int i = 0; i < gridCount(); i++) {
if (cache(i).entry(key).primary()) if (cache(i).entry(key).primary())
cache(i).reload(key); load(jcache(i), key, true);
} }


// Will do near get request. // Will do near get request.
Expand Down Expand Up @@ -3513,102 +3512,6 @@ public void testPessimisticTxRepeatableReadOnUpdate() throws Exception {
} }
} }


/**
* @throws Exception If failed.
*/
public void testPrimaryData() throws Exception {
if (offheapTiered(cache(0)))
return;

final List<String> keys = new ArrayList<>(3);

for (int i = 0; i < 3; i++) {
while (true) {
String key = UUID.randomUUID().toString();

if (grid(0).mapKeyToNode(null, key).equals(grid(0).localNode())) {
assertTrue(cache(0).putx(key, i));

keys.add(key);

break;
}
}
}

if (cacheMode() == PARTITIONED && gridCount() > 1) {
for (int i = 0; i < 10; i++) {
while (true) {
String key = UUID.randomUUID().toString();

if (!grid(0).mapKeyToNode(null, key).equals(grid(0).localNode())) {
assertTrue(cache(1).putx(key, i));

break;
}
}
}
}

List<String> subList = keys.subList(1, keys.size());

// ---------------
// Key set checks.
// ---------------

info("Key set: " + cache(0).keySet());
info("Entry set: " + cache(0).entrySet());
info("Primary entry set: " + cache(0).primaryEntrySet());

Set<String> primKeys = cache(0).primaryKeySet();

assertEquals(3, primKeys.size());
assertTrue(primKeys.containsAll(keys));

primKeys = cache(0).projection(new P1<CacheEntry<String, Integer>>() {
@Override public boolean apply(CacheEntry<String, Integer> e) {
return !e.getKey().equals(keys.get(0));
}
}).primaryKeySet();

assertEquals(2, primKeys.size());
assertTrue(primKeys.containsAll(subList));

// --------------
// Values checks.
// --------------

Collection<Integer> primVals = cache(0).primaryValues();

assertEquals(3, primVals.size());
assertTrue(primVals.containsAll(F.asList(0, 1, 2)));

primVals = cache(0).projection(new P1<CacheEntry<String, Integer>>() {
@Override public boolean apply(CacheEntry<String, Integer> e) {
return !e.getKey().equals(keys.get(0));
}
}).primaryValues();

assertEquals(2, primVals.size());
assertTrue(primVals.containsAll(F.asList(1, 2)));

// -----------------
// Entry set checks.
// -----------------

Set<CacheEntry<String, Integer>> primEntries = cache(0).primaryEntrySet();

assertEquals(3, primEntries.size());

primEntries = cache(0).projection(new P1<CacheEntry<String, Integer>>() {
@Override public boolean apply(CacheEntry<String, Integer> e) {
return !e.getKey().equals(keys.get(0));
}
}).primaryEntrySet();

assertEquals(2, primEntries.size());
}

/** /**
* @throws Exception In case of error. * @throws Exception In case of error.
*/ */
Expand All @@ -3624,7 +3527,7 @@ public void testToMap() throws Exception {
Map<String, Integer> map = new HashMap<>(); Map<String, Integer> map = new HashMap<>();


for (int i = 0; i < gridCount(); i++) { for (int i = 0; i < gridCount(); i++) {
for (CacheEntry<String, Integer> entry : cache(i)) for (Cache.Entry<String, Integer> entry : jcache(i))
map.put(entry.getKey(), entry.getValue()); map.put(entry.getKey(), entry.getValue());
} }


Expand Down Expand Up @@ -3662,7 +3565,7 @@ protected void checkSize(Collection<String> keys) throws Exception {
} }
} }


assertEquals("Incorrect size on cache #" + i, size, cache(i).size()); assertEquals("Incorrect size on cache #" + i, size, jcache(i).localSize());
} }
} }
} }
Expand All @@ -3684,7 +3587,7 @@ protected void checkKeySize(Collection<String> keys) throws Exception {
if (ctx.affinity().localNode(key, ctx.discovery().topologyVersion())) if (ctx.affinity().localNode(key, ctx.discovery().topologyVersion()))
size++; size++;


assertEquals("Incorrect key size on cache #" + i, size, cache(i).size()); assertEquals("Incorrect key size on cache #" + i, size, jcache(i).localSize());
} }
} }
} }
Expand All @@ -3701,7 +3604,7 @@ private void checkContainsKey(boolean exp, String key) throws Exception {
boolean contains = false; boolean contains = false;


for (int i = 0; i < gridCount(); i++) for (int i = 0; i < gridCount(); i++)
if (containsKey(cache(i), key)) { if (containsKey(jcache(i), key)) {
contains = true; contains = true;


break; break;
Expand Down
Expand Up @@ -37,11 +37,8 @@
import org.jdk8.backport.*; import org.jdk8.backport.*;
import org.jetbrains.annotations.*; import org.jetbrains.annotations.*;


import javax.cache.*;
import javax.cache.configuration.*; import javax.cache.configuration.*;
import javax.cache.integration.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;


import static org.apache.ignite.cache.CacheAtomicityMode.*; import static org.apache.ignite.cache.CacheAtomicityMode.*;
Expand Down Expand Up @@ -388,7 +385,7 @@ protected final boolean partitionedMode() {
* @return Default cache. * @return Default cache.
*/ */
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
protected IgniteCache<String, Integer> jcache(int idx) { @Override protected IgniteCache<String, Integer> jcache(int idx) {
return ignite(idx).jcache(null); return ignite(idx).jcache(null);
} }


Expand Down Expand Up @@ -456,8 +453,8 @@ protected <K, V> boolean offheapTiered(IgniteCache<K, V> cache) {
* @throws Exception If failed. * @throws Exception If failed.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected boolean containsKey(GridCache cache, Object key) throws Exception { protected boolean containsKey(IgniteCache cache, Object key) throws Exception {
return offheapTiered(cache) ? containsOffheapKey(cache, key) : cache.containsKey(key); return offheapTiered(cache) ? cache.localPeek(key, CachePeekMode.OFFHEAP) != null : cache.containsKey(key);
} }


/** /**
Expand All @@ -471,24 +468,6 @@ protected boolean containsValue(GridCache cache, Object val) throws Exception {
return offheapTiered(cache) ? containsOffheapValue(cache, val) : cache.containsValue(val); return offheapTiered(cache) ? containsOffheapValue(cache, val) : cache.containsValue(val);
} }


/**
* @param cache Cache.
* @param key Key.
* @return {@code True} if offheap contains given key.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
protected boolean containsOffheapKey(GridCache cache, Object key) throws Exception {
for (Iterator<Map.Entry> it = cache.offHeapIterator(); it.hasNext();) {
Map.Entry e = it.next();

if (key.equals(e.getKey()))
return true;
}

return false;
}

/** /**
* @param cache Cache. * @param cache Cache.
* @param val Value. * @param val Value.
Expand Down
Expand Up @@ -18,6 +18,7 @@
package org.apache.ignite.internal.processors.cache; package org.apache.ignite.internal.processors.cache;


import org.apache.ignite.cache.*; import org.apache.ignite.cache.*;
import org.apache.ignite.cache.affinity.*;
import org.apache.ignite.cluster.*; import org.apache.ignite.cluster.*;
import org.apache.ignite.configuration.*; import org.apache.ignite.configuration.*;
import org.apache.ignite.internal.managers.communication.*; import org.apache.ignite.internal.managers.communication.*;
Expand Down Expand Up @@ -143,20 +144,22 @@ protected void checkMessages(CacheDistributionMode distMode,
for (int i = 0; i < putCnt; i++) { for (int i = 0; i < putCnt; i++) {
ClusterNode locNode = grid(0).localNode(); ClusterNode locNode = grid(0).localNode();


CacheAffinity<Object> affinity = ignite(0).affinity(null);

if (writeOrderMode == CLOCK) { if (writeOrderMode == CLOCK) {
if (cache(0).affinity().isPrimary(locNode, i) || cache(0).affinity().isBackup(locNode, i)) if (affinity.isPrimary(locNode, i) || affinity.isBackup(locNode, i))
expNearCnt++; expNearCnt++;
else else
expNearCnt += 2; expNearCnt += 2;
} }
else { else {
if (cache(0).affinity().isPrimary(locNode, i)) if (affinity.isPrimary(locNode, i))
expDhtCnt++; expDhtCnt++;
else else
expNearCnt ++; expNearCnt ++;
} }


cache(0).put(i, i); jcache(0).put(i, i);
} }


assertEquals(expNearCnt, commSpi.messageCount(GridNearAtomicUpdateRequest.class)); assertEquals(expNearCnt, commSpi.messageCount(GridNearAtomicUpdateRequest.class));
Expand Down
Expand Up @@ -186,7 +186,7 @@ public void testPartitionedNearEnabled() throws Exception {


if (grid(0).mapKeyToNode(null, key).equals(grid(0).localNode())) { if (grid(0).mapKeyToNode(null, key).equals(grid(0).localNode())) {
if (i > 0) if (i > 0)
assertTrue(cache(0).putx(key, new Value(new byte[i * 1024]))); jcache(0).put(key, new Value(new byte[i * 1024]));


keys[i] = key; keys[i] = key;


Expand All @@ -196,8 +196,8 @@ public void testPartitionedNearEnabled() throws Exception {
} }


// Create near entries. // Create near entries.
assertNotNull(cache(1).get(keys[1])); assertNotNull(jcache(1).get(keys[1]));
assertNotNull(cache(1).get(keys[2])); assertNotNull(jcache(1).get(keys[2]));


assertEquals(KEY_SIZE + NULL_REF_SIZE + ENTRY_OVERHEAD + DHT_ENTRY_OVERHEAD + assertEquals(KEY_SIZE + NULL_REF_SIZE + ENTRY_OVERHEAD + DHT_ENTRY_OVERHEAD +
extrasSize(cache(0).entry(keys[0])), cache(0).entry(keys[0]).memorySize()); extrasSize(cache(0).entry(keys[0])), cache(0).entry(keys[0]).memorySize());
Expand Down

0 comments on commit cd72342

Please sign in to comment.