blob: 2175abb43d39e8d636eef047f17245e53d8ac370 [file] [log] [blame]
/*
* 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.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.store.CacheStore;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.lang.IgniteBiInClosure;
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.junits.common.GridCommonAbstractTest;
import org.apache.ignite.transactions.Transaction;
import org.jetbrains.annotations.Nullable;
import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
import static org.apache.ignite.cache.CacheMode.PARTITIONED;
import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;
/**
* Tests for reproduce problem with GG-6895:
* putx calls CacheStore.load() when null GridPredicate passed in to avoid IDE warnings
*/
public class GridCacheStorePutxSelfTest extends GridCommonAbstractTest {
/** */
private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true);
/** */
private static AtomicInteger loads;
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setCacheMode(PARTITIONED);
ccfg.setAtomicityMode(TRANSACTIONAL);
ccfg.setWriteSynchronizationMode(FULL_SYNC);
ccfg.setCacheStoreFactory(singletonFactory(new TestStore()));
ccfg.setReadThrough(true);
ccfg.setWriteThrough(true);
ccfg.setLoadPreviousValue(true);
cfg.setCacheConfiguration(ccfg);
TcpDiscoverySpi disco = new TcpDiscoverySpi();
disco.setIpFinder(IP_FINDER);
cfg.setDiscoverySpi(disco);
return cfg;
}
/** {@inheritDoc} */
@Override protected void beforeTest() throws Exception {
loads = new AtomicInteger();
startGrid();
}
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
stopGrid();
}
/**
* @throws Exception If failed.
*/
public void testPutShouldNotTriggerLoad() throws Exception {
jcache().put(1, 1);
jcache().put(2, 2);
assertEquals(0, loads.get());
}
/**
* @throws Exception If failed.
*/
public void testPutShouldNotTriggerLoadWithTx() throws Exception {
IgniteCache<Integer, Integer> cache = jcache();
try (Transaction tx = grid().transactions().txStart()) {
cache.put(1, 1);
cache.put(2, 2);
tx.commit();
}
assertEquals(0, loads.get());
}
/** */
private static class TestStore implements CacheStore<Integer, Integer> {
/** {@inheritDoc} */
@Nullable @Override public Integer load(Integer key) {
loads.incrementAndGet();
return null;
}
/** {@inheritDoc} */
@Override public void loadCache(IgniteBiInClosure<Integer, Integer> clo, @Nullable Object... args) {
// No-op.
}
/** {@inheritDoc} */
@Override public Map<Integer, Integer> loadAll(Iterable<? extends Integer> keys) {
return Collections.emptyMap();
}
/** {@inheritDoc} */
@Override public void write(javax.cache.Cache.Entry<? extends Integer, ? extends Integer> entry) {
// No-op.
}
/** {@inheritDoc} */
@Override public void writeAll(Collection<javax.cache.Cache.Entry<? extends Integer, ? extends Integer>> entries) {
// No-op.
}
/** {@inheritDoc} */
@Override public void delete(Object key) {
// No-op.
}
/** {@inheritDoc} */
@Override public void deleteAll(Collection<?> keys) {
// No-op.
}
/** {@inheritDoc} */
@Override public void sessionEnd(boolean commit) {
// No-op.
}
}
}