IGNITE-443 Added test.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/38c794c0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/38c794c0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/38c794c0 Branch: refs/heads/ignite-443 Commit: 38c794c06ff535d18a9edd32782bb21c39a5b80f Parents: 2cbf440 Author: nikolay_tikhonov <ntikho...@gridgain.com> Authored: Thu Mar 12 09:43:03 2015 +0300 Committer: nikolay_tikhonov <ntikho...@gridgain.com> Committed: Thu Mar 12 09:43:03 2015 +0300 ---------------------------------------------------------------------- ...CacheConflictResolverLocalStoreSelfTest.java | 233 +++++++++++++++++++ .../ignite/testsuites/IgniteCacheTestSuite.java | 1 + 2 files changed, 234 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/38c794c0/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConflictResolverLocalStoreSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConflictResolverLocalStoreSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConflictResolverLocalStoreSelfTest.java new file mode 100644 index 0000000..608d616 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConflictResolverLocalStoreSelfTest.java @@ -0,0 +1,233 @@ +/* + * 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 org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.store.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.processors.cache.store.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; +import org.apache.ignite.testframework.junits.common.*; +import org.jetbrains.annotations.*; + +import javax.cache.*; +import javax.cache.configuration.*; +import javax.cache.integration.*; +import java.util.*; +import java.util.concurrent.*; + +import static org.apache.ignite.cache.CacheMode.*; +import static org.apache.ignite.cache.CachePreloadMode.*; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; + +/** + * + */ +public class GridCacheConflictResolverLocalStoreSelfTest extends GridCommonAbstractTest { + /** */ + private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); + + /** */ + public static final TestLocalStore<Integer, Integer> LOCAL_STORE_1 = new TestLocalStore<>(); + + /** */ + public static final TestLocalStore<Integer, Integer> LOCAL_STORE_2 = new TestLocalStore<>(); + + /** */ + public static final int MERGE_VAL = -42; + + /** */ + public static final int KEYS = 1000; + + /** + * + */ + public GridCacheConflictResolverLocalStoreSelfTest() { + super(false /* doesn't start grid */); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + CacheConfiguration cacheCfg = cache(gridName, null, 0); + + cfg.setCacheConfiguration(cacheCfg); + + TcpDiscoverySpi spi = new TcpDiscoverySpi(); + + spi.setIpFinder(IP_FINDER); + + cfg.setDiscoverySpi(spi); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + + LOCAL_STORE_1.clear(); + LOCAL_STORE_2.clear(); + } + + /** + * @param gridName Grid name. + * @param cacheName Cache name. + * @param backups Number of backups. + * @return Configuration. + */ + @SuppressWarnings("unchecked") + private CacheConfiguration cache(String gridName, String cacheName, int backups) { + CacheConfiguration cacheCfg = new CacheConfiguration(); + + cacheCfg.setName(cacheName); + cacheCfg.setCacheMode(PARTITIONED); + cacheCfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); + cacheCfg.setDistributionMode(CacheDistributionMode.PARTITIONED_ONLY); + cacheCfg.setWriteSynchronizationMode(FULL_SYNC); + cacheCfg.setPreloadMode(SYNC); + + if (gridName.endsWith("1")) + cacheCfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<CacheStore>(LOCAL_STORE_1)); + else if (gridName.endsWith("2")) + cacheCfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<CacheStore>(LOCAL_STORE_2)); + + cacheCfg.setWriteThrough(true); + cacheCfg.setReadThrough(true); + cacheCfg.setBackups(backups); + cacheCfg.setOffHeapMaxMemory(0); + cacheCfg.setSwapEnabled(true); + + cacheCfg.setConflictResolver(new TestConflictResolver()); + cacheCfg.setConflictResolverMode(CacheConflictResolverMode.ALWAYS); + + return cacheCfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + } + + /** + * @throws Exception If failed. + */ + public void testConflictResolver() throws Exception { + Ignite ignite1 = startGrid(1); + + for (int i = 0; i < KEYS; i++) + ignite1.jcache(null).put(i, i); + + Ignite ignite2 = startGrid(2); + + for (int i = KEYS; i < KEYS * 2; i++) + ignite2.jcache(null).put(i, i); + + for (int i = 0; i < KEYS * 2; i++) { + assert LOCAL_STORE_1.load(i) == null || LOCAL_STORE_1.load(i).get1() == MERGE_VAL; + assert LOCAL_STORE_2.load(i) == null || LOCAL_STORE_2.load(i).get1() == MERGE_VAL; + } + } + + /** + * + */ + @CacheLocalStore + public static class TestLocalStore<K, V> implements CacheStore<K, IgniteBiTuple<V, ?>> { + /** */ + private Map<K, IgniteBiTuple<V, ?>> map = new ConcurrentHashMap<>(); + + /** {@inheritDoc} */ + @Override public void loadCache(IgniteBiInClosure<K, IgniteBiTuple<V, ?>> clo, @Nullable Object... args) + throws CacheLoaderException { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void txEnd(boolean commit) throws CacheWriterException { + // No-op. + } + + /** {@inheritDoc} */ + @Override public IgniteBiTuple<V, ?> load(K key) throws CacheLoaderException { + return map.get(key); + } + + /** {@inheritDoc} */ + @Override public Map<K, IgniteBiTuple<V, ?>> loadAll(Iterable<? extends K> keys) throws CacheLoaderException { + Map<K, IgniteBiTuple<V, ?>> res = new HashMap<>(); + + for (K key : keys) { + IgniteBiTuple<V, ?> val = map.get(key); + + if (val != null) + res.put(key, val); + } + + return res; + } + + /** {@inheritDoc} */ + @Override public void write(Cache.Entry<? extends K, ? extends IgniteBiTuple<V, ?>> entry) + throws CacheWriterException { + map.put(entry.getKey(), entry.getValue()); + } + + /** {@inheritDoc} */ + @Override public void writeAll(Collection<Cache.Entry<? extends K, ? extends IgniteBiTuple<V, ?>>> entries) + throws CacheWriterException { + for (Cache.Entry<? extends K, ? extends IgniteBiTuple<V, ?>> e : entries) + map.put(e.getKey(), e.getValue()); + } + + /** {@inheritDoc} */ + @Override public void delete(Object key) throws CacheWriterException { + map.remove(key); + } + + /** {@inheritDoc} */ + @Override public void deleteAll(Collection<?> keys) throws CacheWriterException { + for (Object key : keys) + map.remove(key); + } + + /** + * Clear store. + */ + public void clear(){ + map.clear(); + } + } + + /** + * + */ + public static class TestConflictResolver implements CacheConflictResolver { + /** {@inheritDoc} */ + @Override public void resolve(CacheConflictContext ctx) { + ctx.merge(MERGE_VAL, ctx.newEntry().ttl()); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/38c794c0/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java index 4f37982..e89d54c 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java @@ -140,6 +140,7 @@ public class IgniteCacheTestSuite extends TestSuite { suite.addTestSuite(GridCacheReplicatedLocalStoreSelfTest.class); suite.addTestSuite(GridCachePartitionedOffHeapLocalStoreSelfTest.class); suite.addTestSuite(GridCacheTxPartitionedLocalStoreSelfTest.class); + suite.addTestSuite(GridCacheConflictResolverLocalStoreSelfTest.class); suite.addTestSuite(IgniteCacheSystemTransactionsSelfTest.class); // Heuristic exception handling. TODO IGNITE-257