Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9A93A18143 for ; Fri, 12 Feb 2016 13:09:08 +0000 (UTC) Received: (qmail 35321 invoked by uid 500); 12 Feb 2016 13:09:08 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 35128 invoked by uid 500); 12 Feb 2016 13:09:08 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 34761 invoked by uid 99); 12 Feb 2016 13:09:08 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Feb 2016 13:09:08 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1DB6AE698A; Fri, 12 Feb 2016 13:09:08 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.apache.org Date: Fri, 12 Feb 2016 13:09:16 -0000 Message-Id: <17c929061f6149478847e3ac7a41a23e@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [10/22] ignite git commit: Added test. Added test. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/5539cbad Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/5539cbad Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/5539cbad Branch: refs/heads/ignite-2407 Commit: 5539cbadb81195c35889b71486b1e449b637d8c0 Parents: 16927ab Author: sboikov Authored: Wed Feb 10 15:32:03 2016 +0300 Committer: sboikov Committed: Wed Feb 10 15:32:03 2016 +0300 ---------------------------------------------------------------------- ...niteCacheEntryListenerExpiredEventsTest.java | 202 +++++++++++++++++++ 1 file changed, 202 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/5539cbad/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheEntryListenerExpiredEventsTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheEntryListenerExpiredEventsTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheEntryListenerExpiredEventsTest.java new file mode 100644 index 0000000..d9fdfac --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheEntryListenerExpiredEventsTest.java @@ -0,0 +1,202 @@ +/* + * 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.concurrent.atomic.AtomicInteger; +import javax.cache.configuration.CacheEntryListenerConfiguration; +import javax.cache.configuration.Factory; +import javax.cache.configuration.MutableCacheEntryListenerConfiguration; +import javax.cache.event.CacheEntryEvent; +import javax.cache.event.CacheEntryExpiredListener; +import javax.cache.event.CacheEntryListener; +import javax.cache.expiry.Duration; +import javax.cache.expiry.ModifiedExpiryPolicy; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMemoryMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.util.lang.GridAbsPredicate; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; +import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static org.apache.ignite.cache.CacheAtomicWriteOrderMode.PRIMARY; +import static org.apache.ignite.cache.CacheAtomicityMode.ATOMIC; +import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL; +import static org.apache.ignite.cache.CacheMemoryMode.OFFHEAP_TIERED; +import static org.apache.ignite.cache.CacheMemoryMode.ONHEAP_TIERED; +import static org.apache.ignite.cache.CacheMode.PARTITIONED; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC; + +/** + * + */ +public class IgniteCacheEntryListenerExpiredEventsTest extends GridCommonAbstractTest { + /** */ + private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); + + /** */ + private static AtomicInteger evtCntr; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(IP_FINDER); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + startGrid(0); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + + super.afterTestsStopped(); + } + + /** + * @throws Exception If failed. + */ + public void testExpiredEventAtomic() throws Exception { + checkExpiredEvents(cacheConfiguration(PARTITIONED, ATOMIC, ONHEAP_TIERED)); + } + + /** + * @throws Exception If failed. + */ + public void testExpiredEventAtomicOffheap() throws Exception { + checkExpiredEvents(cacheConfiguration(PARTITIONED, ATOMIC, OFFHEAP_TIERED)); + } + + /** + * @throws Exception If failed. + */ + public void testExpiredEventTx() throws Exception { + checkExpiredEvents(cacheConfiguration(PARTITIONED, TRANSACTIONAL, ONHEAP_TIERED)); + } + + /** + * @throws Exception If failed. + */ + public void testExpiredEventTxOffheap() throws Exception { + checkExpiredEvents(cacheConfiguration(PARTITIONED, TRANSACTIONAL, OFFHEAP_TIERED)); + } + + /** + * @param ccfg Cache configuration. + * @throws Exception If failed. + */ + private void checkExpiredEvents(CacheConfiguration ccfg) throws Exception { + IgniteCache cache = ignite(0).createCache(ccfg); + + try { + evtCntr = new AtomicInteger(); + + CacheEntryListenerConfiguration lsnrCfg = new MutableCacheEntryListenerConfiguration<>( + new ExpiredListenerFactory(), + null, + true, + false + ); + + cache.registerCacheEntryListener(lsnrCfg); + + IgniteCache expiryCache = + cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, 500))); + + expiryCache.put(1, 1); + + for (int i = 0; i < 10; i++) + cache.get(i); + + boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() { + @Override public boolean apply() { + return evtCntr.get() > 0; + } + }, 5000); + + assertTrue(wait); + + U.sleep(100); + + assertEquals(1, evtCntr.get()); + } + finally { + ignite(0).destroyCache(cache.getName()); + } + } + + /** + * + * @param cacheMode Cache mode. + * @param atomicityMode Cache atomicity mode. + * @param memoryMode Cache memory mode. + * @return Cache configuration. + */ + private CacheConfiguration cacheConfiguration( + CacheMode cacheMode, + CacheAtomicityMode atomicityMode, + CacheMemoryMode memoryMode) { + CacheConfiguration ccfg = new CacheConfiguration<>(); + + ccfg.setAtomicityMode(atomicityMode); + ccfg.setCacheMode(cacheMode); + ccfg.setMemoryMode(memoryMode); + ccfg.setWriteSynchronizationMode(FULL_SYNC); + ccfg.setAtomicWriteOrderMode(PRIMARY); + + if (cacheMode == PARTITIONED) + ccfg.setBackups(1); + + return ccfg; + } + + /** + * + */ + private static class ExpiredListenerFactory implements Factory> { + /** {@inheritDoc} */ + @Override public CacheEntryListener create() { + return new ExpiredListener(); + } + } + + /** + * + */ + private static class ExpiredListener implements CacheEntryExpiredListener { + /** {@inheritDoc} */ + @Override public void onExpired(Iterable> evts) { + for (CacheEntryEvent evt : evts) + evtCntr.incrementAndGet(); + } + } +}