Search in sources :

Example 1 with Invalidation

use of com.hazelcast.internal.nearcache.impl.invalidation.Invalidation in project hazelcast by hazelcast.

the class CacheDestroyTest method testInvalidationListenerCallCount.

@Test
public void testInvalidationListenerCallCount() {
    final ICache<String, String> cache = createCache();
    final AtomicInteger counter = new AtomicInteger(0);
    final CacheConfig config = cache.getConfiguration(CacheConfig.class);
    registerInvalidationListener(new CacheEventListener() {

        @Override
        public void handleEvent(Object eventObject) {
            if (eventObject instanceof Invalidation) {
                Invalidation event = (Invalidation) eventObject;
                if (null == event.getKey() && config.getNameWithPrefix().equals(event.getName())) {
                    counter.incrementAndGet();
                }
            }
        }
    }, config.getNameWithPrefix());
    cache.destroy();
    // Make sure that at least 1 invalidation event has been received
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(counter.get() >= 1);
        }
    }, 2);
    // Make sure that no more than INSTNACE_COUNT events are received ever
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(counter.get() <= INSTANCE_COUNT);
        }
    }, 3);
}
Also used : CacheEventListener(com.hazelcast.cache.impl.CacheEventListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertTask(com.hazelcast.test.AssertTask) Invalidation(com.hazelcast.internal.nearcache.impl.invalidation.Invalidation) CacheConfig(com.hazelcast.config.CacheConfig) CacheException(javax.cache.CacheException) ExecutionException(java.util.concurrent.ExecutionException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with Invalidation

use of com.hazelcast.internal.nearcache.impl.invalidation.Invalidation in project hazelcast by hazelcast.

the class CacheClearTest method testInvalidationListenerCallCount.

@Test
public void testInvalidationListenerCallCount() {
    final ICache<String, String> cache = createCache();
    Map<String, String> entries = createAndFillEntries();
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        cache.put(entry.getKey(), entry.getValue());
    }
    // Verify that put works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        String actualValue = cache.get(key);
        assertEquals(expectedValue, actualValue);
    }
    final AtomicInteger counter = new AtomicInteger(0);
    final CacheConfig config = cache.getConfiguration(CacheConfig.class);
    registerInvalidationListener(eventObject -> {
        if (eventObject instanceof Invalidation) {
            Invalidation event = (Invalidation) eventObject;
            if (null == event.getKey() && config.getNameWithPrefix().equals(event.getName())) {
                counter.incrementAndGet();
            }
        }
    }, config.getNameWithPrefix());
    cache.clear();
    // Make sure that one event is received
    assertTrueEventually(() -> assertEquals(1, counter.get()), 5);
    // Make sure that the callback is not called for a while
    assertTrueAllTheTime(() -> assertTrue(counter.get() <= 1), 3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Invalidation(com.hazelcast.internal.nearcache.impl.invalidation.Invalidation) HashMap(java.util.HashMap) Map(java.util.Map) CacheConfig(com.hazelcast.config.CacheConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with Invalidation

use of com.hazelcast.internal.nearcache.impl.invalidation.Invalidation in project hazelcast by hazelcast.

the class InvalidationTest method setUp.

@Before
public void setUp() {
    Config config = getBaseConfig();
    HazelcastInstance hz = createHazelcastInstance(config);
    serializationService = getSerializationService(hz);
    Data key = serializationService.toData("key");
    String mapName = "mapName";
    UUID sourceUuid = UUID.randomUUID();
    UUID partitionUuid = UUID.randomUUID();
    singleInvalidation = new SingleNearCacheInvalidation(key, mapName, sourceUuid, partitionUuid, 1);
    List<Invalidation> invalidations = Collections.singletonList(singleInvalidation);
    batchInvalidation = new BatchNearCacheInvalidation(mapName, invalidations);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) NearCacheTestUtils.getBaseConfig(com.hazelcast.internal.nearcache.impl.NearCacheTestUtils.getBaseConfig) BatchNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.BatchNearCacheInvalidation) Data(com.hazelcast.internal.serialization.Data) SingleNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.SingleNearCacheInvalidation) BatchNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.BatchNearCacheInvalidation) SingleNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.SingleNearCacheInvalidation) Invalidation(com.hazelcast.internal.nearcache.impl.invalidation.Invalidation) UUID(java.util.UUID) Before(org.junit.Before)

Example 4 with Invalidation

use of com.hazelcast.internal.nearcache.impl.invalidation.Invalidation in project hazelcast by hazelcast.

the class BatchNearCacheInvalidationTest method equals_itself_after_deserialization.

@Test
public void equals_itself_after_deserialization() throws Exception {
    Data key = ss.toData("key");
    String mapName = "mapName";
    String sourceUuid = "sourceUuid";
    UUID partitionUuid = UUID.randomUUID();
    List<Invalidation> invalidations = new ArrayList<Invalidation>();
    invalidations.add(new SingleNearCacheInvalidation(key, mapName, sourceUuid, partitionUuid, 1));
    BatchNearCacheInvalidation batch = new BatchNearCacheInvalidation(mapName, invalidations);
    Data data = ss.toData(batch);
    Object object = ss.toObject(data);
    assertInstanceOf(BatchNearCacheInvalidation.class, object);
    List<Invalidation> actualInvalidations = ((BatchNearCacheInvalidation) object).getInvalidations();
    assertDeserializedEqualsExpected(key, mapName, partitionUuid, actualInvalidations);
}
Also used : BatchNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.BatchNearCacheInvalidation) ArrayList(java.util.ArrayList) Data(com.hazelcast.nio.serialization.Data) BatchNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.BatchNearCacheInvalidation) SingleNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.SingleNearCacheInvalidation) Invalidation(com.hazelcast.internal.nearcache.impl.invalidation.Invalidation) SingleNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.SingleNearCacheInvalidation) UUID(java.util.UUID) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with Invalidation

use of com.hazelcast.internal.nearcache.impl.invalidation.Invalidation in project hazelcast by hazelcast.

the class BatchNearCacheInvalidationTest method assertDeserializedEqualsExpected.

private void assertDeserializedEqualsExpected(Data key, String mapName, UUID partitionUuid, List<Invalidation> invalidations) {
    for (Invalidation invalidation : invalidations) {
        Data invalidationKey = invalidation.getKey();
        assertTrue(invalidationKey.equals(key));
        assertEquals(mapName, invalidation.getName());
        assertEquals(partitionUuid, invalidation.getPartitionUuid());
    }
}
Also used : BatchNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.BatchNearCacheInvalidation) SingleNearCacheInvalidation(com.hazelcast.internal.nearcache.impl.invalidation.SingleNearCacheInvalidation) Invalidation(com.hazelcast.internal.nearcache.impl.invalidation.Invalidation) Data(com.hazelcast.nio.serialization.Data)

Aggregations

Invalidation (com.hazelcast.internal.nearcache.impl.invalidation.Invalidation)6 BatchNearCacheInvalidation (com.hazelcast.internal.nearcache.impl.invalidation.BatchNearCacheInvalidation)4 SingleNearCacheInvalidation (com.hazelcast.internal.nearcache.impl.invalidation.SingleNearCacheInvalidation)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Test (org.junit.Test)4 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 CacheConfig (com.hazelcast.config.CacheConfig)2 Data (com.hazelcast.internal.serialization.Data)2 Data (com.hazelcast.nio.serialization.Data)2 UUID (java.util.UUID)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CacheEventListener (com.hazelcast.cache.impl.CacheEventListener)1 Config (com.hazelcast.config.Config)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 NearCacheTestUtils.getBaseConfig (com.hazelcast.internal.nearcache.impl.NearCacheTestUtils.getBaseConfig)1 AssertTask (com.hazelcast.test.AssertTask)1 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1