Search in sources :

Example 31 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project ignite by apache.

the class IgniteCacheNoReadThroughAbstractTest method testNoReadThrough.

/**
 * @throws Exception If failed.
 */
@Test
public void testNoReadThrough() throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(0);
    for (Integer key : keys()) {
        log.info("Test [key=" + key + ']');
        storeMap.put(key, key);
        assertNull(cache.get(key));
        assertEquals(key, storeMap.get(key));
        assertNull(cache.getAndPut(key, -1));
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertTrue(cache.putIfAbsent(key, -1));
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertNull(cache.getAndRemove(key));
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertNull(cache.getAndPutIfAbsent(key, -1));
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        Object ret = cache.invoke(key, new EntryProcessor<Integer, Integer, Object>() {

            @Override
            public Object process(MutableEntry<Integer, Integer> e, Object... args) {
                Integer val = e.getValue();
                assertFalse(e.exists());
                assertNull(val);
                e.setValue(-1);
                return String.valueOf(val);
            }
        });
        assertEquals("null", ret);
        assertEquals(-1, storeMap.get(key));
        cache.remove(key);
        assertNull(storeMap.get(key));
        storeMap.put(key, key);
        assertFalse(cache.replace(key, -1));
        assertEquals(key, storeMap.get(key));
        assertNull(cache.getAndReplace(key, -1));
        assertEquals(key, storeMap.get(key));
        assertFalse(cache.replace(key, key, -1));
        assertEquals(key, storeMap.get(key));
    }
    Set<Integer> keys = new HashSet<>();
    for (int i = 1000_0000; i < 1000_0000 + 1000; i++) {
        keys.add(i);
        storeMap.put(i, i);
    }
    assertTrue(cache.getAll(keys).isEmpty());
    if (atomicityMode() == TRANSACTIONAL) {
        for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                for (Integer key : keys()) {
                    log.info("Test tx [key=" + key + ", concurrency=" + concurrency + ", isolation=" + isolation + ']');
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertNull(cache.get(key));
                        tx.commit();
                    }
                    assertEquals(key, storeMap.get(key));
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertNull(cache.getAndPut(key, -1));
                        tx.commit();
                    }
                    assertEquals(-1, storeMap.get(key));
                    cache.remove(key);
                    assertNull(storeMap.get(key));
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertTrue(cache.putIfAbsent(key, -1));
                        tx.commit();
                    }
                    assertEquals(-1, storeMap.get(key));
                    cache.remove(key);
                    assertNull(storeMap.get(key));
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        Object ret = cache.invoke(key, new EntryProcessor<Integer, Integer, Object>() {

                            @Override
                            public Object process(MutableEntry<Integer, Integer> e, Object... args) {
                                Integer val = e.getValue();
                                assertFalse(e.exists());
                                assertNull(val);
                                e.setValue(-1);
                                return String.valueOf(val);
                            }
                        });
                        assertEquals("null", ret);
                        tx.commit();
                    }
                    assertEquals(-1, storeMap.get(key));
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertTrue(cache.getAll(keys).isEmpty());
                        tx.commit();
                    }
                }
            }
        }
    }
    // Check can load cache when read-through is disabled.
    allowLoad = true;
    Integer key = 1;
    cache.remove(key);
    storeMap.clear();
    storeMap.put(key, 10);
    cache.loadCache(null);
    assertEquals(10, (int) cache.get(key));
    cache.remove(key);
    storeMap.put(key, 11);
    CompletionListenerFuture fut = new CompletionListenerFuture();
    cache.loadAll(F.asSet(key), true, fut);
    fut.get();
    assertEquals(11, (int) cache.get(key));
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet) IgniteCacheAbstractTest(org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest) Test(org.junit.Test)

Example 32 with CompletionListenerFuture

use of javax.cache.integration.CompletionListenerFuture in project ignite by apache.

the class GridCachePartitionedReloadAllAbstractSelfTest method testReloadAll.

/**
 * @throws Exception If test failed.
 */
@Test
public void testReloadAll() throws Exception {
    // Fill caches with values.
    for (IgniteCache<Integer, String> cache : caches) {
        Iterable<Integer> keys = primaryKeys(cache, 100);
        info("Values [cache=" + caches.indexOf(cache) + ", size=" + F.size(keys.iterator()) + ", keys=" + keys + "]");
        for (Integer key : keys) map.put(key, "val" + key);
    }
    CompletionListenerFuture fut = new CompletionListenerFuture();
    caches.get(0).loadAll(map.keySet(), false, fut);
    fut.get();
    Affinity aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
    for (IgniteCache<Integer, String> cache : caches) {
        for (Integer key : map.keySet()) {
            if (aff.isPrimaryOrBackup(grid(caches.indexOf(cache)).localNode(), key))
                assertEquals(map.get(key), cache.localPeek(key));
            else
                assertNull(cache.localPeek(key));
        }
    }
}
Also used : Affinity(org.apache.ignite.cache.affinity.Affinity) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

CompletionListenerFuture (javax.cache.integration.CompletionListenerFuture)32 Test (org.junit.Test)30 HashSet (java.util.HashSet)26 IgniteCacheAbstractTest (org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest)3 ExecutionException (java.util.concurrent.ExecutionException)2 Duration (javax.cache.expiry.Duration)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 TimeStep (com.hazelcast.simulator.test.annotations.TimeStep)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MutableConfiguration (javax.cache.configuration.MutableConfiguration)1 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)1 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)1 Affinity (org.apache.ignite.cache.affinity.Affinity)1 SqlQuery (org.apache.ignite.cache.query.SqlQuery)1 GridCacheAbstractSelfTest (org.apache.ignite.internal.processors.cache.GridCacheAbstractSelfTest)1 Transaction (org.apache.ignite.transactions.Transaction)1 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)1 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)1