Search in sources :

Example 66 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheResourceTest method testCloseableCacheWriter.

@Test
public void testCloseableCacheWriter() throws InterruptedException {
    CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(factory.newHazelcastInstance());
    CacheManager cacheManager = provider.getCacheManager();
    CloseableCacheWriter writer = new CloseableCacheWriter();
    Factory<CloseableCacheWriter> writerFactory = FactoryBuilder.factoryOf(writer);
    CompleteConfiguration<Object, Object> configuration = new CacheConfig().setCacheWriterFactory(writerFactory).setWriteThrough(true);
    Cache<Object, Object> cache = cacheManager.createCache("test", configuration);
    // trigger partition assignment
    cache.get("key");
    factory.newHazelcastInstance();
    for (int i = 0; i < 1000; i++) {
        cache.put(i, i);
        LockSupport.parkNanos(1000);
    }
    assertFalse("CacheWriter should not be closed!", writer.closed);
}
Also used : CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 67 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheSerializationTest method test_CacheReplicationOperation_serialization.

@Test
public void test_CacheReplicationOperation_serialization() throws Exception {
    TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(1);
    HazelcastInstance hazelcastInstance = factory.newHazelcastInstance();
    try {
        CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(hazelcastInstance);
        CacheManager manager = provider.getCacheManager();
        CompleteConfiguration configuration = new MutableConfiguration();
        Cache cache1 = manager.createCache("cache1", configuration);
        Cache cache2 = manager.createCache("cache2", configuration);
        Cache cache3 = manager.createCache("cache3", configuration);
        for (int i = 0; i < 1000; i++) {
            cache1.put("key" + i, i);
            cache2.put("key" + i, i);
            cache3.put("key" + i, i);
        }
        HazelcastInstanceProxy proxy = (HazelcastInstanceProxy) hazelcastInstance;
        Field original = HazelcastInstanceProxy.class.getDeclaredField("original");
        original.setAccessible(true);
        HazelcastInstanceImpl impl = (HazelcastInstanceImpl) original.get(proxy);
        NodeEngineImpl nodeEngine = impl.node.nodeEngine;
        CacheService cacheService = nodeEngine.getService(CacheService.SERVICE_NAME);
        int partitionCount = nodeEngine.getPartitionService().getPartitionCount();
        for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
            CachePartitionSegment segment = cacheService.getSegment(partitionId);
            CacheReplicationOperation operation = new CacheReplicationOperation(segment, 1);
            Data serialized = service.toData(operation);
            try {
                service.toObject(serialized);
            } catch (Exception e) {
                throw new Exception("Partition: " + partitionId, e);
            }
        }
    } finally {
        factory.shutdownAll();
    }
}
Also used : HazelcastInstanceImpl(com.hazelcast.instance.HazelcastInstanceImpl) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) CachePartitionSegment(com.hazelcast.cache.impl.CachePartitionSegment) CacheReplicationOperation(com.hazelcast.cache.impl.operation.CacheReplicationOperation) Data(com.hazelcast.nio.serialization.Data) CachePartitionEventData(com.hazelcast.cache.impl.CachePartitionEventData) HazelcastInstanceProxy(com.hazelcast.instance.HazelcastInstanceProxy) MutableConfiguration(javax.cache.configuration.MutableConfiguration) UnknownHostException(java.net.UnknownHostException) Field(java.lang.reflect.Field) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CompleteConfiguration(javax.cache.configuration.CompleteConfiguration) CacheManager(javax.cache.CacheManager) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) Cache(javax.cache.Cache) CacheService(com.hazelcast.cache.impl.CacheService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 68 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheHazelcastInstanceAwareTest method test_inject_hazelcastInstance_to_expiryPolicy_if_itIs_hazelcastInstanceAware.

@Test
public void test_inject_hazelcastInstance_to_expiryPolicy_if_itIs_hazelcastInstanceAware() {
    long id1 = generateUniqueHazelcastInjectionId();
    long id2 = generateUniqueHazelcastInjectionId();
    CacheConfig<Integer, Integer> cacheConfig = createCacheConfig(CACHE_NAME);
    cacheConfig.setExpiryPolicyFactory(new HazelcastInstanceAwareExpiryPolicyFactory(id1, id2));
    HazelcastInstance hazelcastInstance = createInstance();
    CacheManager cacheManager = createCachingProvider(hazelcastInstance).getCacheManager();
    Cache<Integer, Integer> cache = cacheManager.createCache(CACHE_NAME, cacheConfig);
    cache.put(1, 1);
    assertEquals("Hazelcast instance has not been injected into expiry policy factory!", Boolean.TRUE, HAZELCAST_INSTANCE_INJECTION_RESULT_MAP.get(id1));
    assertEquals("Hazelcast instance has not been injected into expiry policy!", Boolean.TRUE, HAZELCAST_INSTANCE_INJECTION_RESULT_MAP.get(id2));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CacheManager(javax.cache.CacheManager) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 69 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheHazelcastInstanceAwareTest method test_inject_hazelcastInstance_to_partitionLostListener_if_itIs_hazelcastInstanceAware.

@Test
public void test_inject_hazelcastInstance_to_partitionLostListener_if_itIs_hazelcastInstanceAware() {
    long id = generateUniqueHazelcastInjectionId();
    CacheConfig<Integer, Integer> cacheConfig = createCacheConfig(CACHE_NAME);
    HazelcastInstance hazelcastInstance = createInstance();
    CacheManager cacheManager = createCachingProvider(hazelcastInstance).getCacheManager();
    ICache<Integer, Integer> cache = (ICache<Integer, Integer>) cacheManager.createCache(CACHE_NAME, cacheConfig);
    cache.addPartitionLostListener(new HazelcastInstanceAwareCachePartitionLostListener(id));
    assertEquals("Hazelcast instance has not been injected into partition lost listener!", Boolean.TRUE, HAZELCAST_INSTANCE_INJECTION_RESULT_MAP.get(id));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICache(com.hazelcast.cache.ICache) CacheManager(javax.cache.CacheManager) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 70 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CachePartitionLostListenerStressTest method testCachePartitionLostListener.

@Test
public void testCachePartitionLostListener() {
    List<HazelcastInstance> instances = getCreatedInstancesShuffledAfterWarmedUp();
    List<HazelcastInstance> survivingInstances = new ArrayList<HazelcastInstance>(instances);
    List<HazelcastInstance> terminatingInstances = survivingInstances.subList(0, numberOfNodesToCrash);
    survivingInstances = survivingInstances.subList(numberOfNodesToCrash, instances.size());
    HazelcastInstance instance = survivingInstances.get(0);
    HazelcastServerCachingProvider cachingProvider = createCachingProvider(instance);
    CacheManager cacheManager = cachingProvider.getCacheManager();
    List<EventCollectingCachePartitionLostListener> listeners = registerListeners(cacheManager);
    if (withData) {
        for (int i = 0; i < getNodeCount(); i++) {
            Cache<Integer, Integer> cache = cacheManager.getCache(getIthCacheName(i));
            for (int j = 0; j < getCacheEntryCount(); j++) {
                cache.put(j, j);
            }
        }
    }
    final String log = "Surviving: " + survivingInstances + " Terminating: " + terminatingInstances;
    Map<Integer, Integer> survivingPartitions = getMinReplicaIndicesByPartitionId(survivingInstances);
    stopInstances(terminatingInstances, nodeLeaveType);
    waitAllForSafeState(survivingInstances);
    if (shouldExpectPartitionLostEvents) {
        for (int i = 0; i < getNodeCount(); i++) {
            assertListenerInvocationsEventually(log, i, numberOfNodesToCrash, listeners.get(i), survivingPartitions);
        }
    } else {
        for (final EventCollectingCachePartitionLostListener listener : listeners) {
            assertTrueAllTheTime(new AssertTask() {

                @Override
                public void run() throws Exception {
                    assertTrue(listener.getEvents().isEmpty());
                }
            }, 1);
        }
    }
    for (int i = 0; i < getNodeCount(); i++) {
        cacheManager.destroyCache(getIthCacheName(i));
    }
    cacheManager.close();
    cachingProvider.close();
}
Also used : ArrayList(java.util.ArrayList) EventCollectingCachePartitionLostListener(com.hazelcast.cache.CachePartitionLostListenerTest.EventCollectingCachePartitionLostListener) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) AbstractPartitionLostListenerTest(com.hazelcast.partition.AbstractPartitionLostListenerTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Aggregations

CacheManager (javax.cache.CacheManager)105 Test (org.junit.Test)63 QuickTest (com.hazelcast.test.annotation.QuickTest)56 CachingProvider (javax.cache.spi.CachingProvider)48 ParallelTest (com.hazelcast.test.annotation.ParallelTest)41 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)40 HazelcastInstance (com.hazelcast.core.HazelcastInstance)34 CacheConfig (com.hazelcast.config.CacheConfig)32 AssertTask (com.hazelcast.test.AssertTask)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 MutableConfiguration (javax.cache.configuration.MutableConfiguration)17 URI (java.net.URI)16 Cache (javax.cache.Cache)15 HazelcastCacheManager (com.hazelcast.cache.HazelcastCacheManager)13 HazelcastClientCachingProvider (com.hazelcast.client.cache.impl.HazelcastClientCachingProvider)12 ICache (com.hazelcast.cache.ICache)10 Properties (java.util.Properties)10 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)9 ClientConfig (com.hazelcast.client.config.ClientConfig)8 HazelcastCachingProvider (com.hazelcast.cache.HazelcastCachingProvider)7