Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds partition lost listener configuration to ICache.
- Loading branch information
ibrahim gürses
committed
Aug 20, 2015
1 parent
0d9f846
commit bff583c
Showing
46 changed files
with
2,368 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
...nt-new/src/test/java/com/hazelcast/client/cache/ClientCachePartitionLostListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
package com.hazelcast.client.cache; | ||
|
||
import com.hazelcast.cache.ICache; | ||
import com.hazelcast.cache.impl.CacheService; | ||
import com.hazelcast.cache.impl.HazelcastServerCachingProvider; | ||
import com.hazelcast.cache.impl.event.CachePartitionLostEvent; | ||
import com.hazelcast.cache.impl.event.CachePartitionLostListener; | ||
import com.hazelcast.client.cache.impl.HazelcastClientCachingProvider; | ||
import com.hazelcast.client.test.TestHazelcastFactory; | ||
import com.hazelcast.config.CacheConfig; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.partition.InternalPartitionLostEvent; | ||
import com.hazelcast.spi.EventRegistration; | ||
import com.hazelcast.spi.impl.eventservice.InternalEventService; | ||
import com.hazelcast.test.AssertTask; | ||
import com.hazelcast.test.HazelcastParallelClassRunner; | ||
import com.hazelcast.test.HazelcastTestSupport; | ||
import com.hazelcast.test.annotation.ParallelTest; | ||
import com.hazelcast.test.annotation.QuickTest; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.runner.RunWith; | ||
|
||
import javax.cache.Cache; | ||
import javax.cache.CacheManager; | ||
import javax.cache.spi.CachingProvider; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import static com.hazelcast.cache.impl.HazelcastServerCachingProvider.createCachingProvider; | ||
import static com.hazelcast.cache.impl.ICacheService.SERVICE_NAME; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
|
||
@RunWith(HazelcastParallelClassRunner.class) | ||
@Category({QuickTest.class, ParallelTest.class}) | ||
public class ClientCachePartitionLostListenerTest extends HazelcastTestSupport { | ||
|
||
private final TestHazelcastFactory hazelcastFactory = new TestHazelcastFactory(); | ||
|
||
@After | ||
public void tearDown() { | ||
hazelcastFactory.terminateAll(); | ||
} | ||
|
||
@Test | ||
public void test_cachePartitionLostListener_registered() { | ||
|
||
final String cacheName = randomName(); | ||
|
||
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance(); | ||
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(); | ||
|
||
final CachingProvider cachingProvider = HazelcastClientCachingProvider.createCachingProvider(client); | ||
final CacheManager cacheManager = cachingProvider.getCacheManager(); | ||
final CacheConfig<Integer, String> cacheConfig = new CacheConfig<Integer, String>(); | ||
final Cache<Integer, String> cache = cacheManager.createCache(cacheName, cacheConfig); | ||
final ICache iCache = cache.unwrap(ICache.class); | ||
|
||
iCache.addPartitionLostListener(new CachePartitionLostListener() { | ||
@Override | ||
public void partitionLost(CachePartitionLostEvent event) { | ||
} | ||
}); | ||
|
||
assertRegistrationsSizeEventually(instance, cacheName, 1); | ||
} | ||
|
||
@Test | ||
public void test_cachePartitionLostListener_invoked() { | ||
final String cacheName = randomName(); | ||
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance(); | ||
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(); | ||
|
||
final HazelcastServerCachingProvider cachingProvider = createCachingProvider(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 = HazelcastClientCachingProvider.createCachingProvider(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); | ||
|
||
final CacheService cacheService = getNode(instance).getNodeEngine().getService(CacheService.SERVICE_NAME); | ||
final int partitionId = 5; | ||
cacheService.onPartitionLost(new InternalPartitionLostEvent(partitionId, 0, null)); | ||
|
||
assertCachePartitionLostEventEventually(listener, partitionId); | ||
} | ||
|
||
@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 = createCachingProvider(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 = HazelcastClientCachingProvider.createCachingProvider(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 InternalPartitionLostEvent(partitionId, 0, null)); | ||
cacheService2.onPartitionLost(new InternalPartitionLostEvent(partitionId, 0, null)); | ||
|
||
assertCachePartitionLostEventEventually(listener, partitionId); | ||
} | ||
|
||
@Test | ||
public void test_cachePartitionLostListener_removed() { | ||
final String cacheName = randomName(); | ||
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance(); | ||
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(); | ||
|
||
final HazelcastServerCachingProvider cachingProvider = createCachingProvider(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 = HazelcastClientCachingProvider.createCachingProvider(client); | ||
final CacheManager clientCacheManager = clientCachingProvider.getCacheManager(); | ||
final Cache<Integer, String> cache = clientCacheManager.getCache(cacheName); | ||
final ICache iCache = cache.unwrap(ICache.class); | ||
|
||
final String registrationId = iCache.addPartitionLostListener(mock(CachePartitionLostListener.class)); | ||
|
||
assertRegistrationsSizeEventually(instance, cacheName, 1); | ||
|
||
assertTrue(iCache.removePartitionLostListener(registrationId)); | ||
assertRegistrationsSizeEventually(instance, cacheName, 0); | ||
} | ||
|
||
private void assertCachePartitionLostEventEventually(final EventCollectingCachePartitionLostListener listener, | ||
final int partitionId) { | ||
assertTrueEventually(new AssertTask() { | ||
@Override | ||
public void run() | ||
throws Exception { | ||
|
||
final List<CachePartitionLostEvent> events = listener.getEvents(); | ||
assertFalse(events.isEmpty()); | ||
assertEquals(partitionId, events.get(0).getPartitionId()); | ||
|
||
} | ||
}); | ||
} | ||
|
||
private void assertRegistrationsSizeEventually(final HazelcastInstance instance, final String cacheName, final int size) { | ||
assertTrueEventually(new AssertTask() { | ||
@Override | ||
public void run() | ||
throws Exception { | ||
|
||
final InternalEventService eventService = getNode(instance).getNodeEngine().getEventService(); | ||
final Collection<EventRegistration> registrations = eventService.getRegistrations(SERVICE_NAME, cacheName); | ||
assertEquals(size, registrations.size()); | ||
|
||
} | ||
}); | ||
} | ||
|
||
private class EventCollectingCachePartitionLostListener | ||
implements CachePartitionLostListener { | ||
|
||
private final List<CachePartitionLostEvent> events = Collections.synchronizedList(new LinkedList<CachePartitionLostEvent>()); | ||
|
||
public EventCollectingCachePartitionLostListener() { | ||
} | ||
|
||
@Override | ||
public void partitionLost(CachePartitionLostEvent event) { | ||
this.events.add(event); | ||
} | ||
|
||
public List<CachePartitionLostEvent> getEvents() { | ||
synchronized (events) { | ||
return new ArrayList<CachePartitionLostEvent>(events); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.