Search in sources :

Example 6 with IFunction

use of com.hazelcast.core.IFunction in project hazelcast by hazelcast.

the class QueryCacheTest method testQueryCacheCleared_afterCalling_IMap_clear.

@Test
public void testQueryCacheCleared_afterCalling_IMap_clear() {
    String cacheName = randomString();
    final QueryCache<Integer, Employee> queryCache = map.getQueryCache(cacheName, TRUE_PREDICATE, false);
    populateMap(map, 1000);
    IFunction clear = new IFunction() {

        @Override
        public Object apply(Object ignored) {
            map.clear();
            return null;
        }
    };
    assertQueryCacheSizeEventually(0, clear, queryCache);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Employee(com.hazelcast.mapreduce.helpers.Employee) IFunction(com.hazelcast.core.IFunction) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with IFunction

use of com.hazelcast.core.IFunction in project hazelcast by hazelcast.

the class SampleableConcurrentHashMapTest method applyIfAbsentTest.

@Test
public void applyIfAbsentTest() throws Throwable {
    final SampleableConcurrentHashMap<String, String> map = new SampleableConcurrentHashMap<String, String>(10);
    assertEquals(map.applyIfAbsent("key", new IFunction<String, String>() {

        @Override
        public String apply(String input) {
            return "value";
        }
    }), "value");
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final int COUNT = 10;
    final CountDownLatch latch = new CountDownLatch(COUNT);
    for (int i = 0; i < COUNT; i++) new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                assertEquals(map.applyIfAbsent("key", new IFunction<String, String>() {

                    @Override
                    public String apply(String input) {
                        return "value1";
                    }
                }), "value");
            } catch (Throwable e) {
                error.set(e);
            } finally {
                latch.countDown();
            }
        }
    }).start();
    latch.await(20, TimeUnit.SECONDS);
    if (error.get() != null) {
        throw error.get();
    }
    map.clear();
    map.applyIfAbsent("key", new IFunction<String, String>() {

        @Override
        public String apply(String input) {
            return null;
        }
    });
    assertEquals(map.size(), 0);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IFunction(com.hazelcast.core.IFunction) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with IFunction

use of com.hazelcast.core.IFunction in project hazelcast by hazelcast.

the class AlterAndGetOperation method run.

@Override
public void run() throws Exception {
    NodeEngine nodeEngine = getNodeEngine();
    IFunction f = nodeEngine.toObject(function);
    AtomicReferenceContainer atomicReferenceContainer = getReferenceContainer();
    Data originalData = atomicReferenceContainer.get();
    Object input = nodeEngine.toObject(originalData);
    //noinspection unchecked
    Object output = f.apply(input);
    Data serializedOutput = nodeEngine.toData(output);
    shouldBackup = !isEquals(originalData, serializedOutput);
    if (shouldBackup) {
        backup = serializedOutput;
        atomicReferenceContainer.set(backup);
    }
    response = output;
}
Also used : NodeEngine(com.hazelcast.spi.NodeEngine) AtomicReferenceContainer(com.hazelcast.concurrent.atomicreference.AtomicReferenceContainer) Data(com.hazelcast.nio.serialization.Data) IFunction(com.hazelcast.core.IFunction)

Example 9 with IFunction

use of com.hazelcast.core.IFunction in project hazelcast by hazelcast.

the class QueryCacheTest method testQueryCacheCleared_afterCalling_IMap_evictAll.

@Test
public void testQueryCacheCleared_afterCalling_IMap_evictAll() {
    String cacheName = randomString();
    QueryCache<Integer, Employee> queryCache = map.getQueryCache(cacheName, TRUE_PREDICATE, false);
    populateMap(map, 1000);
    IFunction evictAll = new IFunction() {

        @Override
        public Object apply(Object ignored) {
            map.evictAll();
            return null;
        }
    };
    assertQueryCacheSizeEventually(0, evictAll, queryCache);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Employee(com.hazelcast.mapreduce.helpers.Employee) IFunction(com.hazelcast.core.IFunction) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with IFunction

use of com.hazelcast.core.IFunction in project hazelcast by hazelcast.

the class ClientQueryCacheTest method testQueryCacheCleared_afterCalling_IMap_evictAll.

@Test
public void testQueryCacheCleared_afterCalling_IMap_evictAll() {
    String cacheName = randomString();
    final IMap<Integer, Integer> map = getMap();
    QueryCache<Integer, Integer> queryCache = map.getQueryCache(cacheName, TRUE_PREDICATE, false);
    for (int i = 0; i < 1000; i++) {
        map.put(i, i);
    }
    IFunction evictAll = new IFunction() {

        @Override
        public Object apply(Object ignored) {
            map.evictAll();
            return null;
        }
    };
    assertQueryCacheSizeEventually(0, evictAll, queryCache);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IFunction(com.hazelcast.core.IFunction) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

IFunction (com.hazelcast.core.IFunction)10 QuickTest (com.hazelcast.test.annotation.QuickTest)6 Test (org.junit.Test)6 ParallelTest (com.hazelcast.test.annotation.ParallelTest)5 AtomicReferenceContainer (com.hazelcast.concurrent.atomicreference.AtomicReferenceContainer)4 NodeEngine (com.hazelcast.spi.NodeEngine)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Data (com.hazelcast.nio.serialization.Data)3 Employee (com.hazelcast.mapreduce.helpers.Employee)2 ReadResultSetImpl (com.hazelcast.ringbuffer.impl.ReadResultSetImpl)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1