Search in sources :

Example 1 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class ClientCachePartitionLostListenerTest method test_cachePartitionLostListener_invoked_fromOtherNode.

@Test
public void test_cachePartitionLostListener_invoked_fromOtherNode() {
    final String cacheName = randomName();
    HazelcastInstance instance1 = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final HazelcastServerCachingProvider cachingProvider = createServerCachingProvider(instance1);
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    final CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
    config.setBackupCount(0);
    cacheManager.createCache(cacheName, config);
    final CachingProvider clientCachingProvider = createClientCachingProvider(client);
    final CacheManager clientCacheManager = clientCachingProvider.getCacheManager();
    final Cache<Integer, String> cache = clientCacheManager.getCache(cacheName);
    final ICache iCache = cache.unwrap(ICache.class);
    final EventCollectingCachePartitionLostListener listener = new EventCollectingCachePartitionLostListener();
    iCache.addPartitionLostListener(listener);
    assertRegistrationsSizeEventually(instance1, cacheName, 1);
    assertRegistrationsSizeEventually(instance2, cacheName, 1);
    final CacheService cacheService1 = getNode(instance1).getNodeEngine().getService(CacheService.SERVICE_NAME);
    final CacheService cacheService2 = getNode(instance2).getNodeEngine().getService(CacheService.SERVICE_NAME);
    final int partitionId = 5;
    cacheService1.onPartitionLost(new PartitionLostEventImpl(partitionId, 0, null));
    cacheService2.onPartitionLost(new PartitionLostEventImpl(partitionId, 0, null));
    assertCachePartitionLostEventEventually(listener, partitionId);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICache(com.hazelcast.cache.ICache) CacheManager(javax.cache.CacheManager) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheTestSupport.createClientCachingProvider(com.hazelcast.cache.CacheTestSupport.createClientCachingProvider) CacheTestSupport.createServerCachingProvider(com.hazelcast.cache.CacheTestSupport.createServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) CacheService(com.hazelcast.cache.impl.CacheService) PartitionLostEventImpl(com.hazelcast.internal.partition.PartitionLostEventImpl) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class ClientCachePartitionLostListenerTest method test_cachePartitionLostListener_removed.

@Test
public void test_cachePartitionLostListener_removed() {
    final String cacheName = randomName();
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final HazelcastServerCachingProvider cachingProvider = createServerCachingProvider(instance);
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    final CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
    config.setBackupCount(0);
    cacheManager.createCache(cacheName, config);
    final CachingProvider clientCachingProvider = createClientCachingProvider(client);
    final CacheManager clientCacheManager = clientCachingProvider.getCacheManager();
    final Cache<Integer, String> cache = clientCacheManager.getCache(cacheName);
    final ICache iCache = cache.unwrap(ICache.class);
    final UUID registrationId = iCache.addPartitionLostListener(mock(CachePartitionLostListener.class));
    assertRegistrationsSizeEventually(instance, cacheName, 1);
    assertTrue(iCache.removePartitionLostListener(registrationId));
    assertRegistrationsSizeEventually(instance, cacheName, 0);
}
Also used : CachePartitionLostListener(com.hazelcast.cache.impl.event.CachePartitionLostListener) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICache(com.hazelcast.cache.ICache) CacheManager(javax.cache.CacheManager) UUID(java.util.UUID) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheTestSupport.createClientCachingProvider(com.hazelcast.cache.CacheTestSupport.createClientCachingProvider) CacheTestSupport.createServerCachingProvider(com.hazelcast.cache.CacheTestSupport.createServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class CacheExpirationTest method testSimpleExpiration_putIfAbsentAsync.

@Test
public void testSimpleExpiration_putIfAbsentAsync() {
    SimpleExpiryListener listener = new SimpleExpiryListener();
    CacheConfig<String, String> cacheConfig = createCacheConfig(new HazelcastExpiryPolicy(1, 1, 1), listener);
    Cache<String, String> cache = createCache(cacheConfig);
    ((ICache<String, String>) cache).putIfAbsentAsync("key", "value");
    assertEqualsEventually(1, listener.getExpirationCount());
}
Also used : ICache(com.hazelcast.cache.ICache) HazelcastExpiryPolicy(com.hazelcast.cache.HazelcastExpiryPolicy) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class CacheExpirationTest method testSimpleExpiration_getAndPutAsync.

@Test
public void testSimpleExpiration_getAndPutAsync() {
    SimpleExpiryListener listener = new SimpleExpiryListener();
    CacheConfig<String, String> cacheConfig = createCacheConfig(new HazelcastExpiryPolicy(1, 1, 1), listener);
    Cache<String, String> cache = createCache(cacheConfig);
    ((ICache<String, String>) cache).getAndPutAsync("key", "value");
    assertEqualsEventually(1, listener.getExpirationCount());
}
Also used : ICache(com.hazelcast.cache.ICache) HazelcastExpiryPolicy(com.hazelcast.cache.HazelcastExpiryPolicy) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with ICache

use of com.hazelcast.cache.ICache in project hazelcast by hazelcast.

the class ExtendedCacheExpirationTest method test_backupOperationAppliesDefaultExpiryPolicy.

@Test
public void test_backupOperationAppliesDefaultExpiryPolicy() {
    HazelcastExpiryPolicy defaultExpiryPolicy = new HazelcastExpiryPolicy(THREE_SECONDS, Duration.ZERO, Duration.ZERO);
    CacheConfig cacheConfig = createCacheConfig(defaultExpiryPolicy);
    ICache cache = createCache(cacheConfig);
    int keyCount = 100;
    for (int i = 0; i < keyCount; i++) {
        cache.put(i, i);
    }
    // Check if all backup entries have applied the default expiry policy
    for (int i = 1; i < CLUSTER_SIZE; i++) {
        BackupAccessor backupAccessor = TestBackupUtils.newCacheAccessor(instances, cache.getName(), i);
        for (int j = 0; j < keyCount; j++) {
            TestBackupUtils.assertExpirationTimeExistsEventually(j, backupAccessor);
        }
    }
    // terminate other nodes than number zero to cause backup promotion at the 0th member
    for (int i = 1; i < CLUSTER_SIZE; i++) {
        getNode(instances[i]).shutdown(true);
    }
    assertTrueEventually(() -> {
        for (int i = 0; i < keyCount; i++) {
            assertNull(cache.get(i));
        }
    });
}
Also used : BackupAccessor(com.hazelcast.test.backup.BackupAccessor) ICache(com.hazelcast.cache.ICache) HazelcastExpiryPolicy(com.hazelcast.cache.HazelcastExpiryPolicy) CacheConfig(com.hazelcast.config.CacheConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ICache (com.hazelcast.cache.ICache)20 Test (org.junit.Test)16 QuickTest (com.hazelcast.test.annotation.QuickTest)15 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)13 HazelcastInstance (com.hazelcast.core.HazelcastInstance)11 CacheManager (javax.cache.CacheManager)9 CacheConfig (com.hazelcast.config.CacheConfig)7 Config (com.hazelcast.config.Config)6 CachingProvider (javax.cache.spi.CachingProvider)6 CacheTestSupport.createClientCachingProvider (com.hazelcast.cache.CacheTestSupport.createClientCachingProvider)5 CacheTestSupport.createServerCachingProvider (com.hazelcast.cache.CacheTestSupport.createServerCachingProvider)5 HazelcastExpiryPolicy (com.hazelcast.cache.HazelcastExpiryPolicy)5 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)4 CacheSimpleConfig (com.hazelcast.config.CacheSimpleConfig)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 IList (com.hazelcast.collection.IList)3 Job (com.hazelcast.jet.Job)3 Util.entry (com.hazelcast.jet.Util.entry)3 JobConfig (com.hazelcast.jet.config.JobConfig)3 IMap (com.hazelcast.map.IMap)3