| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.ignite.internal.processors.cache; |
| |
| import java.util.Collections; |
| import java.util.concurrent.CountDownLatch; |
| import java.util.concurrent.TimeUnit; |
| import java.util.concurrent.atomic.AtomicReference; |
| import org.apache.ignite.Ignite; |
| import org.apache.ignite.IgniteCache; |
| import org.apache.ignite.cache.CacheAtomicityMode; |
| import org.apache.ignite.cache.CacheMode; |
| import org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicy; |
| import org.apache.ignite.configuration.CacheConfiguration; |
| import org.apache.ignite.configuration.IgniteConfiguration; |
| import org.apache.ignite.events.CacheEvent; |
| import org.apache.ignite.events.Event; |
| import org.apache.ignite.events.EventType; |
| import org.apache.ignite.lang.IgnitePredicate; |
| import org.apache.ignite.testframework.MvccFeatureChecker; |
| import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; |
| import org.junit.Before; |
| import org.junit.Test; |
| |
| import static org.apache.ignite.events.EventType.EVT_CACHE_ENTRY_EVICTED; |
| import static org.apache.ignite.events.EventType.EVT_JOB_MAPPED; |
| import static org.apache.ignite.events.EventType.EVT_TASK_FAILED; |
| import static org.apache.ignite.events.EventType.EVT_TASK_FINISHED; |
| |
| /** |
| * Eviction event self test. |
| */ |
| public abstract class GridCacheEvictionEventAbstractTest extends GridCommonAbstractTest { |
| /** */ |
| @Before |
| public void beforeGridCacheEvictionEventAbstractTest() { |
| MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.EVICTION); |
| MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.CACHE_EVENTS); |
| } |
| |
| /** |
| * |
| */ |
| protected GridCacheEvictionEventAbstractTest() { |
| super(true); // Start node. |
| } |
| |
| /** {@inheritDoc} */ |
| @Override protected IgniteConfiguration getConfiguration() throws Exception { |
| MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.EVICTION); |
| MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.CACHE_EVENTS); |
| |
| IgniteConfiguration c = super.getConfiguration(); |
| |
| CacheConfiguration cc = defaultCacheConfiguration(); |
| |
| cc.setCacheMode(cacheMode()); |
| cc.setAtomicityMode(atomicityMode()); |
| cc.setEvictionPolicy(new FifoEvictionPolicy()); |
| cc.setOnheapCacheEnabled(true); |
| |
| c.setCacheConfiguration(cc); |
| |
| c.setIncludeEventTypes(EVT_CACHE_ENTRY_EVICTED, EVT_TASK_FAILED, EVT_TASK_FINISHED, EVT_JOB_MAPPED); |
| |
| return c; |
| } |
| |
| /** |
| * @return Cache mode. |
| */ |
| protected abstract CacheMode cacheMode(); |
| |
| /** |
| * @return Atomicity mode. |
| */ |
| protected abstract CacheAtomicityMode atomicityMode(); |
| |
| /** |
| * @throws Exception If failed. |
| */ |
| @Test |
| public void testEvictionEvent() throws Exception { |
| Ignite g = grid(); |
| |
| final CountDownLatch latch = new CountDownLatch(1); |
| |
| final AtomicReference<String> oldVal = new AtomicReference<>(); |
| |
| g.events().localListen(new IgnitePredicate<Event>() { |
| @Override public boolean apply(Event evt) { |
| CacheEvent e = (CacheEvent) evt; |
| |
| oldVal.set((String) e.oldValue()); |
| |
| latch.countDown(); |
| |
| return true; |
| } |
| }, EventType.EVT_CACHE_ENTRY_EVICTED); |
| |
| IgniteCache<String, String> c = g.cache(DEFAULT_CACHE_NAME); |
| |
| c.put("1", "val1"); |
| |
| c.localEvict(Collections.singleton("1")); |
| |
| assertTrue("Failed to wait for eviction event", latch.await(10, TimeUnit.SECONDS)); |
| } |
| } |