Search in sources :

Example 91 with CacheManager

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

the class CacheFromDifferentNodesTest method testCompletionTestByPuttingAndRemovingFromDifferentNodes.

// Issue https://github.com/hazelcast/hazelcast/issues/5865
@Test
public void testCompletionTestByPuttingAndRemovingFromDifferentNodes() throws InterruptedException {
    String cacheName = "simpleCache";
    CacheManager cacheManager1 = cachingProvider1.getCacheManager();
    CacheManager cacheManager2 = cachingProvider2.getCacheManager();
    CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
    final SimpleEntryListener<Integer, String> listener = new SimpleEntryListener<Integer, String>();
    MutableCacheEntryListenerConfiguration<Integer, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<Integer, String>(FactoryBuilder.factoryOf(listener), null, true, true);
    config.addCacheEntryListenerConfiguration(listenerConfiguration);
    Cache<Integer, String> cache1 = cacheManager1.createCache(cacheName, config);
    Cache<Integer, String> cache2 = cacheManager2.getCache(cacheName);
    assertNotNull(cache1);
    assertNotNull(cache2);
    Integer key1 = 1;
    String value1 = "value1";
    cache1.put(key1, value1);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, listener.created.get());
        }
    });
    Integer key2 = 2;
    String value2 = "value2";
    cache1.put(key2, value2);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, listener.created.get());
        }
    });
    Set<Integer> keys = new HashSet<Integer>();
    keys.add(key1);
    keys.add(key2);
    cache2.removeAll(keys);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, listener.removed.get());
        }
    });
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) CacheConfig(com.hazelcast.config.CacheConfig) HashSet(java.util.HashSet) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 92 with CacheManager

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

the class CacheIteratorAbstractTest method getCache.

private Cache<Integer, Integer> getCache() {
    String cacheName = randomString();
    CacheManager cacheManager = cachingProvider.getCacheManager();
    CacheConfig<Integer, Integer> config = new CacheConfig<Integer, Integer>();
    return cacheManager.createCache(cacheName, config);
}
Also used : CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig)

Example 93 with CacheManager

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

the class CacheListenerTest method testReplaceIfSameWithSyncListener_whenEntryNotExists.

@Test(timeout = 30000)
public void testReplaceIfSameWithSyncListener_whenEntryNotExists() {
    CachingProvider cachingProvider = getCachingProvider();
    CacheManager cacheManager = cachingProvider.getCacheManager();
    CompleteConfiguration<String, String> config = new MutableConfiguration<String, String>().setTypes(String.class, String.class).addCacheEntryListenerConfiguration(new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(new TestListener(new AtomicInteger())), null, true, true));
    Cache<String, String> cache = cacheManager.createCache(randomString(), config);
    // there should not be any hanging due to sync listener
    cache.replace(randomString(), randomString(), randomString());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(javax.cache.CacheManager) MutableConfiguration(javax.cache.configuration.MutableConfiguration) 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 94 with CacheManager

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

the class CacheListenerTest method testRemoveWithSyncListener_whenEntryNotExists.

@Test(timeout = 30000)
public void testRemoveWithSyncListener_whenEntryNotExists() {
    CachingProvider cachingProvider = getCachingProvider();
    CacheManager cacheManager = cachingProvider.getCacheManager();
    CompleteConfiguration<String, String> config = new MutableConfiguration<String, String>().setTypes(String.class, String.class).addCacheEntryListenerConfiguration(new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(new TestListener(new AtomicInteger())), null, true, true));
    Cache<String, String> cache = cacheManager.createCache(randomString(), config);
    // there should not be any hanging due to sync listener
    cache.remove(randomString());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(javax.cache.CacheManager) MutableConfiguration(javax.cache.configuration.MutableConfiguration) 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 95 with CacheManager

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

the class CacheListenerTest method testSyncListener_shouldNotHang_AfterAction.

private void testSyncListener_shouldNotHang_AfterAction(String cacheName, CachingProvider provider, Runnable action) {
    CacheManager cacheManager = provider.getCacheManager();
    CompleteConfiguration<String, String> config = new MutableConfiguration<String, String>().addCacheEntryListenerConfiguration(new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(new TestListener(new AtomicInteger())), null, true, true));
    final Cache<String, String> cache = cacheManager.createCache(cacheName, config);
    int threads = 4;
    final CountDownLatch latch = new CountDownLatch(threads);
    for (int i = 0; i < threads; i++) {
        new Thread() {

            public void run() {
                Random rand = new Random();
                while (true) {
                    try {
                        cache.put(String.valueOf(rand.nextInt(100)), "value");
                    } catch (Throwable e) {
                        break;
                    }
                }
                latch.countDown();
            }
        }.start();
    }
    // wait a little for putting threads to start
    sleepSeconds(3);
    action.run();
    assertOpenEventually("Cache operations should not hang when sync listener is present!", latch);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(javax.cache.CacheManager)

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