Search in sources :

Example 96 with CacheManager

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

the class CacheListenerTest method testGetAndRemoveWithSyncListener_whenEntryNotExists.

@Test(timeout = 30000)
public void testGetAndRemoveWithSyncListener_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.getAndRemove(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 97 with CacheManager

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

the class CacheListenerTest method testSyncListener.

@Test
public void testSyncListener() throws Exception {
    CachingProvider cachingProvider = getCachingProvider();
    CacheManager cacheManager = cachingProvider.getCacheManager();
    final AtomicInteger counter = new AtomicInteger();
    CompleteConfiguration<String, String> config = new MutableConfiguration<String, String>().setTypes(String.class, String.class).addCacheEntryListenerConfiguration(new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(new TestListener(counter)), null, true, true));
    final Cache<String, String> cache = cacheManager.createCache("test", config);
    final int threadCount = 10;
    final int shutdownWaitTimeInSeconds = threadCount;
    final int putCount = 1000;
    final AtomicInteger actualPutCount = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(threadCount);
    final AtomicBoolean shutdown = new AtomicBoolean(false);
    for (int i = 0; i < threadCount; i++) {
        new Thread() {

            public void run() {
                Random rand = new Random();
                for (int i = 0; i < putCount && !shutdown.get(); i++) {
                    String key = String.valueOf(rand.nextInt(putCount));
                    String value = UUID.randomUUID().toString();
                    cache.put(key, value);
                    actualPutCount.incrementAndGet();
                }
                latch.countDown();
            }
        }.start();
    }
    if (!latch.await(ASSERT_TRUE_EVENTUALLY_TIMEOUT, TimeUnit.SECONDS)) {
        shutdown.set(true);
        if (!latch.await(shutdownWaitTimeInSeconds, TimeUnit.SECONDS)) {
            fail("Cache operations have not finished in " + (ASSERT_TRUE_EVENTUALLY_TIMEOUT + shutdownWaitTimeInSeconds) + " seconds when sync listener is present!");
        }
    }
    assertEquals(actualPutCount.get(), counter.get());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) MutableConfiguration(javax.cache.configuration.MutableConfiguration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(javax.cache.CacheManager) 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 98 with CacheManager

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

the class CacheListenerTest method testRemoveIfSameWithSyncListener_whenEntryNotExists.

@Test(timeout = 30000)
public void testRemoveIfSameWithSyncListener_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(), 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 99 with CacheManager

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

the class BasicCacheLiteMemberTest method testCachesDestroy.

@Test
public void testCachesDestroy() {
    CacheManager cacheManager = instanceCachingProvider.getCacheManager();
    CacheManager cacheManager2 = liteCachingProvider.getCacheManager();
    MutableConfiguration configuration = new MutableConfiguration();
    final Cache c1 = cacheManager.createCache("c1", configuration);
    final Cache c2 = cacheManager2.getCache("c1");
    c1.put("key", "value");
    cacheManager.destroyCache("c1");
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            try {
                c2.get("key");
                fail("get should throw IllegalStateException");
            } catch (IllegalStateException e) {
            //ignored as expected
            }
        }
    });
}
Also used : CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) MutableConfiguration(javax.cache.configuration.MutableConfiguration) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) Cache(javax.cache.Cache) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 100 with CacheManager

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

the class BasicCacheLiteMemberTest method testCacheCreation.

private void testCacheCreation(final HazelcastServerCachingProvider providerToCreate, final HazelcastServerCachingProvider providerToValidate) {
    CacheManager cacheManager = providerToCreate.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 = providerToValidate.getCacheManager();
            assertNotNull(cm2.getCache(cacheName));
        }
    });
}
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)

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