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);
}
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);
}
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());
}
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());
}
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));
}
});
}
Aggregations