Search in sources :

Example 26 with CacheConfig

use of com.hazelcast.config.CacheConfig in project hazelcast by hazelcast.

the class CacheBackupTest method entrySuccessfullyRetrievedFromBackup.

private void entrySuccessfullyRetrievedFromBackup(int backupCount, boolean sync) {
    final String KEY = "key";
    final String VALUE = "value";
    final int nodeCount = backupCount + 1;
    final TestHazelcastInstanceFactory instanceFactory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance[] instances = new HazelcastInstance[nodeCount];
    for (int i = 0; i < instances.length; i++) {
        instances[i] = instanceFactory.newHazelcastInstance();
    }
    final HazelcastInstance hz = instances[0];
    final CachingProvider cachingProvider = HazelcastServerCachingProvider.createCachingProvider(hz);
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    final String cacheName = randomName();
    final CacheConfig cacheConfig = new CacheConfig().setName(cacheName);
    if (sync) {
        cacheConfig.setBackupCount(backupCount);
    } else {
        cacheConfig.setAsyncBackupCount(backupCount);
    }
    final Cache cache = cacheManager.createCache(cacheName, cacheConfig);
    warmUpPartitions(instances);
    waitAllForSafeState(instances);
    cache.put(KEY, VALUE);
    final Node node = getNode(hz);
    final InternalPartitionService partitionService = node.getPartitionService();
    final int keyPartitionId = partitionService.getPartitionId(KEY);
    for (int i = 1; i <= backupCount; i++) {
        final Node backupNode = getNode(instances[i]);
        final SerializationService serializationService = backupNode.getSerializationService();
        final ICacheService cacheService = backupNode.getNodeEngine().getService(ICacheService.SERVICE_NAME);
        if (sync) {
            checkSavedRecordOnBackup(KEY, VALUE, cacheName, keyPartitionId, serializationService, cacheService);
        } else {
            assertTrueEventually(new AssertTask() {

                @Override
                public void run() throws Exception {
                    checkSavedRecordOnBackup(KEY, VALUE, cacheName, keyPartitionId, serializationService, cacheService);
                }
            });
        }
    }
}
Also used : InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) Node(com.hazelcast.instance.Node) SerializationService(com.hazelcast.spi.serialization.SerializationService) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICacheService(com.hazelcast.cache.impl.ICacheService) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) Cache(javax.cache.Cache)

Example 27 with CacheConfig

use of com.hazelcast.config.CacheConfig in project hazelcast by hazelcast.

the class CacheClearTest method testInvalidationListenerCallCount.

@Test
public void testInvalidationListenerCallCount() {
    final ICache<String, String> cache = createCache();
    Map<String, String> entries = createAndFillEntries();
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        cache.put(entry.getKey(), entry.getValue());
    }
    // Verify that put works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        String actualValue = cache.get(key);
        assertEquals(expectedValue, actualValue);
    }
    final AtomicInteger counter = new AtomicInteger(0);
    final CacheConfig config = cache.getConfiguration(CacheConfig.class);
    registerInvalidationListener(new CacheEventListener() {

        @Override
        public void handleEvent(Object eventObject) {
            if (eventObject instanceof Invalidation) {
                Invalidation event = (Invalidation) eventObject;
                if (null == event.getKey() && config.getNameWithPrefix().equals(event.getName())) {
                    counter.incrementAndGet();
                }
            }
        }
    }, config.getNameWithPrefix());
    cache.clear();
    // Make sure that one event is received
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, counter.get());
        }
    }, 5);
    // Make sure that the callback is not called for a while
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(counter.get() <= 1);
        }
    }, 3);
}
Also used : CacheEventListener(com.hazelcast.cache.impl.CacheEventListener) Invalidation(com.hazelcast.internal.nearcache.impl.invalidation.Invalidation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertTask(com.hazelcast.test.AssertTask) HashMap(java.util.HashMap) Map(java.util.Map) CacheConfig(com.hazelcast.config.CacheConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 28 with CacheConfig

use of com.hazelcast.config.CacheConfig in project hazelcast by hazelcast.

the class CacheDestroyTest method testInvalidationListenerCallCount.

@Test
public void testInvalidationListenerCallCount() {
    final ICache<String, String> cache = createCache();
    final AtomicInteger counter = new AtomicInteger(0);
    final CacheConfig config = cache.getConfiguration(CacheConfig.class);
    registerInvalidationListener(new CacheEventListener() {

        @Override
        public void handleEvent(Object eventObject) {
            if (eventObject instanceof Invalidation) {
                Invalidation event = (Invalidation) eventObject;
                if (null == event.getKey() && config.getNameWithPrefix().equals(event.getName())) {
                    counter.incrementAndGet();
                }
            }
        }
    }, config.getNameWithPrefix());
    cache.destroy();
    // Make sure that at least 1 invalidation event has been received
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(counter.get() >= 1);
        }
    }, 2);
    // Make sure that no more than INSTNACE_COUNT events are received ever
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(counter.get() <= INSTANCE_COUNT);
        }
    }, 3);
}
Also used : CacheEventListener(com.hazelcast.cache.impl.CacheEventListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertTask(com.hazelcast.test.AssertTask) Invalidation(com.hazelcast.internal.nearcache.impl.invalidation.Invalidation) CacheConfig(com.hazelcast.config.CacheConfig) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 29 with CacheConfig

use of com.hazelcast.config.CacheConfig in project hazelcast by hazelcast.

the class CacheEvictionTest method setup.

@Before
public void setup() {
    instance = createHazelcastInstance();
    provider = HazelcastServerCachingProvider.createCachingProvider(instance);
    CacheManager cacheManager = provider.getCacheManager();
    String cacheName = randomString();
    cache = cacheManager.createCache(cacheName, new CacheConfig());
}
Also used : CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) Before(org.junit.Before)

Example 30 with CacheConfig

use of com.hazelcast.config.CacheConfig in project hazelcast by hazelcast.

the class CacheFromDifferentNodesTest method testJSRExample1.

@Test
public void testJSRExample1() throws InterruptedException {
    final String cacheName = randomString();
    CacheManager cacheManager = cachingProvider1.getCacheManager();
    assertNotNull(cacheManager);
    assertNull(cacheManager.getCache(cacheName));
    CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
    Cache<Integer, String> cache = cacheManager.createCache(cacheName, config);
    assertNotNull(cache);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            CacheManager cm2 = cachingProvider2.getCacheManager();
            assertNotNull(cm2.getCache(cacheName));
        }
    });
    Integer key = 1;
    String value1 = "value";
    cache.put(key, value1);
    String value2 = cache.get(key);
    assertEquals(value1, value2);
    cache.remove(key);
    assertNull(cache.get(key));
    Cache<Integer, String> cache2 = cacheManager.getCache(cacheName);
    assertNotNull(cache2);
    key = 1;
    value1 = "value";
    cache.put(key, value1);
    value2 = cache.get(key);
    assertEquals(value1, value2);
    cache.remove(key);
    assertNull(cache.get(key));
    cacheManager.destroyCache(cacheName);
    cacheManager.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) CacheConfig(com.hazelcast.config.CacheConfig) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

CacheConfig (com.hazelcast.config.CacheConfig)77 Test (org.junit.Test)30 CacheManager (javax.cache.CacheManager)28 QuickTest (com.hazelcast.test.annotation.QuickTest)27 ParallelTest (com.hazelcast.test.annotation.ParallelTest)25 CachingProvider (javax.cache.spi.CachingProvider)16 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)14 HazelcastInstance (com.hazelcast.core.HazelcastInstance)14 AssertTask (com.hazelcast.test.AssertTask)13 NearCacheConfig (com.hazelcast.config.NearCacheConfig)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ICacheService (com.hazelcast.cache.impl.ICacheService)8 HazelcastClientCachingProvider (com.hazelcast.client.cache.impl.HazelcastClientCachingProvider)8 ICache (com.hazelcast.cache.ICache)5 ClientConfig (com.hazelcast.client.config.ClientConfig)5 Config (com.hazelcast.config.Config)5 Data (com.hazelcast.nio.serialization.Data)5 CacheService (com.hazelcast.cache.impl.CacheService)4 HazelcastServerCachingProvider.createCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider.createCachingProvider)4 Map (java.util.Map)4