http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheSwapReloadSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheSwapReloadSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheSwapReloadSelfTest.java
new file mode 100644
index 0000000..d846b60
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheSwapReloadSelfTest.java
@@ -0,0 +1,240 @@
+/*
+ * 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.cache.*;
+import org.apache.ignite.cache.store.*;
+import org.apache.ignite.configuration.*;
+import org.apache.ignite.events.*;
+import org.apache.ignite.internal.*;
+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.spi.swapspace.*;
+import org.apache.ignite.spi.swapspace.file.*;
+import org.apache.ignite.internal.util.typedef.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import org.apache.ignite.testframework.junits.common.*;
+
+import javax.cache.*;
+import javax.cache.configuration.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+import static java.util.concurrent.TimeUnit.*;
+import static org.apache.ignite.events.IgniteEventType.*;
+import static org.apache.ignite.cache.GridCacheMode.*;
+import static org.apache.ignite.cache.GridCacheWriteSynchronizationMode.*;
+
+/**
+ * Test that swap is released after entry is reloaded.
+ */
+public class GridCacheSwapReloadSelfTest extends GridCommonAbstractTest {
+    /** IP finder. */
+    private static final TcpDiscoveryIpFinder IP_FINDER = new 
TcpDiscoveryVmIpFinder(true);
+
+    /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
+    @Override protected IgniteConfiguration getConfiguration(String gridName) 
throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        TcpDiscoverySpi disco = new TcpDiscoverySpi();
+
+        disco.setIpFinder(IP_FINDER);
+
+        cfg.setDiscoverySpi(disco);
+
+        FileSwapSpaceSpi swap = new FileSwapSpaceSpi();
+
+        swap.setWriteBufferSize(1);
+
+        cfg.setSwapSpaceSpi(swap);
+
+        CacheConfiguration cacheCfg = defaultCacheConfiguration();
+
+        cacheCfg.setCacheMode(REPLICATED);
+        cacheCfg.setSwapEnabled(true);
+        cacheCfg.setWriteSynchronizationMode(FULL_SYNC);
+        cacheCfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory(new 
TestStore()));
+        cacheCfg.setReadThrough(true);
+        cacheCfg.setWriteThrough(true);
+        cacheCfg.setLoadPreviousValue(true);
+
+        cfg.setCacheConfiguration(cacheCfg);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        startGrid();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopGrid();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testReload() throws Exception {
+        final CountDownLatch swapLatch = new CountDownLatch(1);
+        final CountDownLatch unswapLatch = new CountDownLatch(1);
+
+        grid().events().localListen(new IgnitePredicate<IgniteEvent>() {
+            @Override public boolean apply(IgniteEvent evt) {
+                switch (evt.type()) {
+                    case EVT_SWAP_SPACE_DATA_STORED:
+                        swapLatch.countDown();
+
+                        break;
+
+                    case EVT_SWAP_SPACE_DATA_REMOVED:
+                        unswapLatch.countDown();
+
+                        break;
+
+                    case EVT_SWAP_SPACE_DATA_EVICTED:
+                        assert false : "Data eviction happened.";
+
+                        break;
+
+                    default:
+                        assert false;
+                }
+
+                return true;
+            }
+        }, EVT_SWAP_SPACE_DATA_STORED, EVT_SWAP_SPACE_DATA_REMOVED, 
EVT_SWAP_SPACE_DATA_EVICTED);
+
+        assert swap() != null;
+
+        assert cache().putx("key", "val");
+
+        assert swap().size(spaceName()) == 0;
+
+        assert cache().evict("key");
+
+        assert swapLatch.await(1, SECONDS);
+        Thread.sleep(100);
+
+        assert swap().count(spaceName()) == 1;
+        assert swap().size(spaceName()) > 0;
+
+        assert "val".equals(cache().reload("key"));
+
+        assert unswapLatch.await(1, SECONDS);
+
+        assert swap().count(spaceName()) == 0;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testReloadAll() throws Exception {
+        final CountDownLatch swapLatch = new CountDownLatch(2);
+        final CountDownLatch unswapLatch = new CountDownLatch(2);
+
+        grid().events().localListen(new IgnitePredicate<IgniteEvent>() {
+            @Override public boolean apply(IgniteEvent evt) {
+                switch (evt.type()) {
+                    case EVT_SWAP_SPACE_DATA_STORED:
+                        swapLatch.countDown();
+
+                        break;
+
+                    case EVT_SWAP_SPACE_DATA_REMOVED:
+                        unswapLatch.countDown();
+
+                        break;
+
+                    default:
+                        assert false;
+                }
+
+                return true;
+            }
+        }, EVT_SWAP_SPACE_DATA_STORED, EVT_SWAP_SPACE_DATA_REMOVED);
+
+        assert swap() != null;
+
+        assert cache().putx("key1", "val1");
+        assert cache().putx("key2", "val2");
+
+        assert swap().size(spaceName()) == 0;
+
+        assert cache().evict("key1");
+        assert cache().evict("key2");
+
+        assert swapLatch.await(1, SECONDS);
+        Thread.sleep(100);
+
+        assert swap().count(spaceName()) == 2;
+        assert swap().size(spaceName()) > 0 : swap().size(spaceName());
+
+        cache().reloadAll(F.asList("key1", "key2"));
+
+        assert unswapLatch.await(1, SECONDS);
+
+        assert swap().count(spaceName()) == 0;
+    }
+
+    /**
+     * @return Swap space SPI.
+     */
+    private SwapSpaceSpi swap() {
+        return grid().configuration().getSwapSpaceSpi();
+    }
+
+    /**
+     * @return Swap space name.
+     */
+    private String spaceName() {
+        return 
CU.swapSpaceName(((GridKernal)grid()).internalCache().context());
+    }
+
+    /**
+     * Test store.
+     */
+    private static class TestStore extends CacheStoreAdapter<Object, Object> {
+        /** */
+        private Map<Object, Object> map = new ConcurrentHashMap<>();
+
+        /** */
+        void reset() {
+            map.clear();
+        }
+
+        /** {@inheritDoc} */
+        @Override public Object load(Object key) {
+            return map.get(key);
+        }
+
+        /** {@inheritDoc} */
+        @Override public void write(Cache.Entry<? extends Object, ? extends 
Object> entry) {
+            map.put(entry.getKey(), entry.getValue());
+        }
+
+        /** {@inheritDoc} */
+        @Override public void delete(Object key) {
+            map.remove(key);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestKey.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestKey.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestKey.java
new file mode 100644
index 0000000..45255c1
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestKey.java
@@ -0,0 +1,64 @@
+/*
+ * 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.internal.util.typedef.internal.*;
+import java.io.*;
+
+/**
+ * Test key.
+ */
+public class GridCacheTestKey implements Serializable {
+    /** */
+    private String val;
+
+    /**
+     *
+     */
+    public GridCacheTestKey() {
+        /* No-op. */
+    }
+
+    /**
+     * @param val Value.
+     */
+    public GridCacheTestKey(String val) {
+        this.val = val;
+    }
+
+    /**
+     *
+     * @return Value.
+     */
+    public String getValue() {
+        return val;
+    }
+
+    /**
+     *
+     * @param val Value.
+     */
+    public void setValue(String val) {
+        this.val = val;
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(GridCacheTestKey.class, this);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestStore.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestStore.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestStore.java
new file mode 100644
index 0000000..ff600e7
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestStore.java
@@ -0,0 +1,334 @@
+/*
+ * 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.store.*;
+import org.apache.ignite.internal.util.*;
+import org.apache.ignite.lang.*;
+import org.apache.ignite.transactions.*;
+import org.apache.ignite.internal.processors.cache.transactions.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import org.jetbrains.annotations.*;
+
+import javax.cache.*;
+import javax.cache.integration.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
+
+/**
+ * Test store.
+ */
+public final class GridCacheTestStore extends CacheStore<Integer, String> {
+    /** Store. */
+    private final Map<Integer, String> map;
+
+    /** Transactions. */
+    private final Collection<IgniteTx> txs = new GridConcurrentHashSet<>();
+
+    /** Last method called. */
+    private String lastMtd;
+
+    /** */
+    private long ts = System.currentTimeMillis();
+
+    /** {@link #load(Object)} method call counter .*/
+    private AtomicInteger loadCnt = new AtomicInteger();
+
+    /** {@link #write(Cache.Entry)} method call counter .*/
+    private AtomicInteger putCnt = new AtomicInteger();
+
+    /** {@link #writeAll(Collection)} method call counter .*/
+    private AtomicInteger putAllCnt = new AtomicInteger();
+
+    /** Flag indicating if methods of this store should fail. */
+    private volatile boolean shouldFail;
+
+    /** Configurable delay to simulate slow storage. */
+    private int operationDelay;
+
+    /**
+     * @param map Underlying store map.
+     */
+    public GridCacheTestStore(Map<Integer, String> map) {
+        this.map = map;
+    }
+
+    /**
+     * Default constructor.
+     */
+    public GridCacheTestStore() {
+        map = new ConcurrentHashMap<>();
+    }
+
+    /**
+     * @return Underlying map.
+     */
+    public Map<Integer, String> getMap() {
+        return Collections.unmodifiableMap(map);
+    }
+
+    /**
+     * Sets a flag indicating if methods of this class should fail with {@link 
IgniteCheckedException}.
+     *
+     * @param shouldFail {@code true} if should fail.
+     */
+    public void setShouldFail(boolean shouldFail) {
+        this.shouldFail = shouldFail;
+    }
+
+    /**
+     * Sets delay that this store should wait on each operation.
+     *
+     * @param operationDelay If zero, no delay applied, positive value means
+     *        delay in milliseconds.
+     */
+    public void setOperationDelay(int operationDelay) {
+        assert operationDelay >= 0;
+
+        this.operationDelay = operationDelay;
+    }
+
+    /**
+     * @return Transactions.
+     */
+    public Collection<IgniteTx> transactions() {
+        return txs;
+    }
+
+    /**
+     *
+     * @return Last method called.
+     */
+    public String getLastMethod() {
+        return lastMtd;
+    }
+
+    /**
+     * @return Last timestamp.
+     */
+    public long getTimestamp() {
+        return ts;
+    }
+
+    /**
+     * @return Integer timestamp.
+     */
+    public int getStart() {
+        return Math.abs((int)ts);
+    }
+
+    /**
+     * Sets last method to <tt>null</tt>.
+     */
+    public void resetLastMethod() {
+        lastMtd = null;
+    }
+
+    /**
+     * Resets timestamp.
+     */
+    public void resetTimestamp() {
+        ts = System.currentTimeMillis();
+    }
+
+    /**
+     * Resets the store to initial state.
+     */
+    public void reset() {
+        lastMtd = null;
+
+        map.clear();
+
+        loadCnt.set(0);
+        putCnt.set(0);
+        putAllCnt.set(0);
+
+        ts = System.currentTimeMillis();
+
+        txs.clear();
+    }
+
+    /**
+     * @return Count of {@link #load(Object)} method calls since last reset.
+     */
+    public int getLoadCount() {
+        return loadCnt.get();
+    }
+
+    /**
+     * @return Count of {@link #write(Cache.Entry)} method calls since last 
reset.
+     */
+    public int getPutCount() {
+        return putCnt.get();
+    }
+
+    /**
+     * @return Count of {@link #writeAll(Collection)} method calls since last 
reset.
+     */
+    public int getPutAllCount() {
+        return putAllCnt.get();
+    }
+
+    /** {@inheritDoc} */
+    @Override public String load(Integer key) {
+        checkTx(session());
+
+        lastMtd = "load";
+
+        checkOperation();
+
+        loadCnt.incrementAndGet();
+
+        return map.get(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void loadCache(IgniteBiInClosure<Integer, String> clo, 
Object[] args) {
+        lastMtd = "loadAllFull";
+
+        checkOperation();
+
+        int start = getStart();
+
+        int cnt = (Integer)args[0];
+
+        for (int i = start; i < start + cnt; i++) {
+            map.put(i, Integer.toString(i));
+
+            clo.apply(i, Integer.toString(i));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public Map<Integer, String> loadAll(Iterable<? extends Integer> 
keys) {
+        checkTx(session());
+
+        lastMtd = "loadAll";
+
+        checkOperation();
+
+        Map<Integer, String> loaded = new HashMap<>();
+
+        for (Integer key : keys) {
+            String val = map.get(key);
+
+            if (val != null)
+                loaded.put(key, val);
+        }
+
+        return loaded;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void write(Cache.Entry<? extends Integer, ? extends 
String> e)
+        throws CacheWriterException {
+        checkTx(session());
+
+        lastMtd = "put";
+
+        checkOperation();
+
+        map.put(e.getKey(), e.getValue());
+
+        putCnt.incrementAndGet();
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeAll(Collection<Cache.Entry<? extends Integer, ? 
extends String>> entries)
+        throws CacheWriterException {
+        checkTx(session());
+
+        lastMtd = "putAll";
+
+        checkOperation();
+
+        for (Cache.Entry<? extends Integer, ? extends String> e : entries)
+            this.map.put(e.getKey(), e.getValue());
+
+        putAllCnt.incrementAndGet();
+    }
+
+    /** {@inheritDoc} */
+    @Override public void delete(Object key) throws CacheWriterException {
+        checkTx(session());
+
+        lastMtd = "remove";
+
+        checkOperation();
+
+        map.remove(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void deleteAll(Collection<?> keys)
+        throws CacheWriterException {
+        checkTx(session());
+
+        lastMtd = "removeAll";
+
+        checkOperation();
+
+        for (Object key : keys)
+            map.remove(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void txEnd(boolean commit) {
+        // No-op.
+    }
+
+    /**
+     * Checks the flag and throws exception if it is set. Checks operation 
delay and sleeps
+     * for specified amount of time, if needed.
+     */
+    private void checkOperation() {
+        if (shouldFail)
+            throw new IgniteException("Store exception.");
+
+        if (operationDelay > 0) {
+            try {
+                U.sleep(operationDelay);
+            }
+            catch (IgniteInterruptedException e) {
+                throw new IgniteException(e);
+            }
+        }
+    }
+
+    /**
+     * @param ses Session.
+     */
+    private void checkTx(@Nullable CacheStoreSession ses) {
+        IgniteTx tx = ses != null ? ses.transaction() : null;
+
+        if (tx == null)
+            return;
+
+        txs.add(tx);
+
+        IgniteTxEx tx0 = (IgniteTxEx)tx;
+
+        if (!tx0.local())
+            throw new IgniteException("Tx is not local: " + tx);
+
+        if (tx0.dht())
+            throw new IgniteException("Tx is DHT: " + tx);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestValue.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestValue.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestValue.java
new file mode 100644
index 0000000..870ffcd
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestValue.java
@@ -0,0 +1,77 @@
+/*
+ * 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.cache.query.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import java.io.*;
+
+/**
+ * Test value.
+ */
+public class GridCacheTestValue implements Serializable, Cloneable {
+    /** */
+    @GridCacheQuerySqlField(unique = true)
+    private String val;
+
+    /**
+     *
+     */
+    public GridCacheTestValue() {
+        /* No-op. */
+    }
+
+    /**
+     *
+     * @param val Value.
+     */
+    public GridCacheTestValue(String val) {
+        this.val = val;
+    }
+
+    /**
+     * @return Value.
+     */
+    public String getValue() {
+        return val;
+    }
+
+    /**
+     *
+     * @param val Value.
+     */
+    public void setValue(String val) {
+        this.val = val;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected Object clone() throws CloneNotSupportedException {
+        return super.clone();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean equals(Object o) {
+        return this == o || !(o == null || getClass() != o.getClass())
+            && val != null && val.equals(((GridCacheTestValue)o).val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(GridCacheTestValue.class, this);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestValue2.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestValue2.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestValue2.java
new file mode 100644
index 0000000..3b26aba
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTestValue2.java
@@ -0,0 +1,67 @@
+/*
+ * 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.cache.query.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import java.io.*;
+
+/**
+ * Second test value.
+ */
+public class GridCacheTestValue2 implements Serializable {
+    /** */
+    @GridCacheQuerySqlField
+    private String val;
+
+    /**
+     *
+     */
+    public GridCacheTestValue2() {
+        /* No-op. */
+    }
+
+    /**
+     *
+     * @param val Value.
+     */
+    public GridCacheTestValue2(String val) {
+        this.val = val;
+    }
+
+    /**
+     *
+     * @return Value.
+     */
+    public String getValue() {
+        return val;
+    }
+
+    /**
+     *
+     * @param val Value.
+     */
+    public void setValue(String val) {
+        this.val = val;
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(GridCacheTestValue2.class, this);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTransactionalAbstractMetricsSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTransactionalAbstractMetricsSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTransactionalAbstractMetricsSelfTest.java
new file mode 100644
index 0000000..5a84427
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTransactionalAbstractMetricsSelfTest.java
@@ -0,0 +1,280 @@
+/*
+ * 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.cache.*;
+import org.apache.ignite.transactions.*;
+
+import static org.apache.ignite.transactions.IgniteTxConcurrency.*;
+import static org.apache.ignite.transactions.IgniteTxIsolation.*;
+
+/**
+ * Transactional cache metrics test.
+ */
+public abstract class GridCacheTransactionalAbstractMetricsSelfTest extends 
GridCacheAbstractMetricsSelfTest {
+    /** */
+    private static final int TX_CNT = 3;
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticReadCommittedCommits() throws Exception {
+        testCommits(OPTIMISTIC, READ_COMMITTED, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticReadCommittedCommitsNoData() throws Exception {
+        testCommits(OPTIMISTIC, READ_COMMITTED, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticRepeatableReadCommits() throws Exception {
+        testCommits(OPTIMISTIC, REPEATABLE_READ, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticRepeatableReadCommitsNoData() throws Exception {
+        testCommits(OPTIMISTIC, REPEATABLE_READ, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticSerializableCommits() throws Exception {
+        testCommits(OPTIMISTIC, SERIALIZABLE, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticSerializableCommitsNoData() throws Exception {
+        testCommits(OPTIMISTIC, SERIALIZABLE, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticReadCommittedCommits() throws Exception {
+        testCommits(PESSIMISTIC, READ_COMMITTED, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticReadCommittedCommitsNoData() throws Exception {
+        testCommits(PESSIMISTIC, READ_COMMITTED, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticRepeatableReadCommits() throws Exception {
+        testCommits(PESSIMISTIC, REPEATABLE_READ, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticRepeatableReadCommitsNoData() throws Exception {
+        testCommits(PESSIMISTIC, REPEATABLE_READ, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticSerializableCommits() throws Exception {
+        testCommits(PESSIMISTIC, SERIALIZABLE, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticSerializableCommitsNoData() throws Exception {
+        testCommits(PESSIMISTIC, SERIALIZABLE, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticReadCommittedRollbacks() throws Exception {
+        testRollbacks(OPTIMISTIC, READ_COMMITTED, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticReadCommittedRollbacksNoData() throws Exception {
+        testRollbacks(OPTIMISTIC, READ_COMMITTED, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticRepeatableReadRollbacks() throws Exception {
+        testRollbacks(OPTIMISTIC, REPEATABLE_READ, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticRepeatableReadRollbacksNoData() throws Exception 
{
+        testRollbacks(OPTIMISTIC, REPEATABLE_READ, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticSerializableRollbacks() throws Exception {
+        testRollbacks(OPTIMISTIC, SERIALIZABLE, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOptimisticSerializableRollbacksNoData() throws Exception {
+        testRollbacks(OPTIMISTIC, SERIALIZABLE, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticReadCommittedRollbacks() throws Exception {
+        testRollbacks(PESSIMISTIC, READ_COMMITTED, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticReadCommittedRollbacksNoData() throws Exception 
{
+        testRollbacks(PESSIMISTIC, READ_COMMITTED, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticRepeatableReadRollbacks() throws Exception {
+        testRollbacks(PESSIMISTIC, REPEATABLE_READ, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticRepeatableReadRollbacksNoData() throws 
Exception {
+        testRollbacks(PESSIMISTIC, REPEATABLE_READ, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticSerializableRollbacks() throws Exception {
+        testRollbacks(PESSIMISTIC, SERIALIZABLE, true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPessimisticSerializableRollbacksNoData() throws Exception {
+        testRollbacks(PESSIMISTIC, SERIALIZABLE, false);
+    }
+
+    /**
+     * @param concurrency Concurrency control.
+     * @param isolation Isolation level.
+     * @param put Put some data if {@code true}.
+     * @throws Exception If failed.
+     */
+    private void testCommits(IgniteTxConcurrency concurrency, 
IgniteTxIsolation isolation, boolean put)
+        throws Exception {
+        GridCache<Integer, Integer> cache = grid(0).cache(null);
+
+        for (int i = 0; i < TX_CNT; i++) {
+            IgniteTx tx = cache.txStart(concurrency, isolation);
+
+            if (put)
+                for (int j = 0; j < keyCount(); j++)
+                    cache.put(j, j);
+
+            tx.commit();
+        }
+
+        for (int i = 0; i < gridCount(); i++) {
+            IgniteTxMetrics metrics = grid(i).transactions().metrics();
+            GridCacheMetrics cacheMetrics = grid(i).cache(null).metrics();
+
+            if (i == 0) {
+                assertEquals(TX_CNT, metrics.txCommits());
+
+                if (put)
+                    assertEquals(TX_CNT, cacheMetrics.txCommits());
+            }
+            else {
+                assertEquals(0, metrics.txCommits());
+                assertEquals(0, cacheMetrics.txCommits());
+            }
+
+            assertEquals(0, metrics.txRollbacks());
+            assertEquals(0, cacheMetrics.txRollbacks());
+        }
+    }
+
+    /**
+     * @param concurrency Concurrency control.
+     * @param isolation Isolation level.
+     * @param put Put some data if {@code true}.
+     * @throws Exception If failed.
+     */
+    private void testRollbacks(IgniteTxConcurrency concurrency, 
IgniteTxIsolation isolation,
+        boolean put) throws Exception {
+        GridCache<Integer, Integer> cache = grid(0).cache(null);
+
+        for (int i = 0; i < TX_CNT; i++) {
+            IgniteTx tx = cache.txStart(concurrency, isolation);
+
+            if (put)
+                for (int j = 0; j < keyCount(); j++)
+                    cache.put(j, j);
+
+            tx.rollback();
+        }
+
+        for (int i = 0; i < gridCount(); i++) {
+            IgniteTxMetrics metrics = grid(i).transactions().metrics();
+            GridCacheMetrics cacheMetrics = grid(i).cache(null).metrics();
+
+            assertEquals(0, metrics.txCommits());
+            assertEquals(0, cacheMetrics.txCommits());
+
+            if (i == 0) {
+                assertEquals(TX_CNT, metrics.txRollbacks());
+
+                if (put)
+                    assertEquals(TX_CNT, cacheMetrics.txRollbacks());
+            }
+            else {
+                assertEquals(0, metrics.txRollbacks());
+                assertEquals(0, cacheMetrics.txRollbacks());
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTtlManagerLoadTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTtlManagerLoadTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTtlManagerLoadTest.java
new file mode 100644
index 0000000..c907f79
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTtlManagerLoadTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.internal.*;
+import org.apache.ignite.lang.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+
+import javax.cache.expiry.*;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
+
+import static java.util.concurrent.TimeUnit.*;
+import static org.apache.ignite.cache.GridCacheMode.*;
+
+/**
+ * Check ttl manager for memory leak.
+ */
+public class GridCacheTtlManagerLoadTest extends GridCacheTtlManagerSelfTest {
+    /**
+     * @throws Exception If failed.
+     */
+    public void testLoad() throws Exception {
+        cacheMode = REPLICATED;
+
+        final GridKernal g = (GridKernal)startGrid(0);
+
+        try {
+            final AtomicBoolean stop = new AtomicBoolean();
+
+            IgniteFuture<?> fut = multithreadedAsync(new Callable<Object>() {
+                @Override public Object call() throws Exception {
+                    IgniteCache<Object,Object> cache = g.jcache(null).
+                        withExpiryPolicy(new TouchedExpiryPolicy(new 
Duration(MILLISECONDS, 1000)));
+
+                    long key = 0;
+
+                    while (!stop.get()) {
+                        cache.put(key, key);
+
+                        key++;
+                    }
+
+                    return null;
+                }
+            }, 1);
+
+            GridCacheTtlManager<Object, Object> ttlMgr = 
g.internalCache().context().ttl();
+
+            for (int i = 0; i < 300; i++) {
+                U.sleep(1000);
+
+                ttlMgr.printMemoryStats();
+            }
+
+            stop.set(true);
+
+            fut.get();
+        }
+        finally {
+            stopAllGrids();
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected long getTestTimeout() {
+        return Long.MAX_VALUE;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTtlManagerSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTtlManagerSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTtlManagerSelfTest.java
new file mode 100644
index 0000000..9d1a3e7
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTtlManagerSelfTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.cache.*;
+import org.apache.ignite.configuration.*;
+import org.apache.ignite.internal.*;
+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.internal.util.typedef.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import org.apache.ignite.testframework.*;
+import org.apache.ignite.testframework.junits.common.*;
+
+import javax.cache.expiry.*;
+
+import static java.util.concurrent.TimeUnit.*;
+import static org.apache.ignite.cache.GridCacheMode.*;
+
+/**
+ * TTL manager self test.
+ */
+public class GridCacheTtlManagerSelfTest extends GridCommonAbstractTest {
+    /** IP finder. */
+    private static final TcpDiscoveryIpFinder IP_FINDER = new 
TcpDiscoveryVmIpFinder(true);
+
+    /** Test cache mode. */
+    protected GridCacheMode cacheMode;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String gridName) 
throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        TcpDiscoverySpi discoSpi = new TcpDiscoverySpi();
+
+        discoSpi.setIpFinder(IP_FINDER);
+
+        cfg.setDiscoverySpi(discoSpi);
+
+        CacheConfiguration ccfg = new CacheConfiguration();
+
+        ccfg.setCacheMode(cacheMode);
+        ccfg.setEagerTtl(true);
+
+        cfg.setCacheConfiguration(ccfg);
+
+        return cfg;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testLocalTtl() throws Exception {
+        checkTtl(LOCAL);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPartitionedTtl() throws Exception {
+        checkTtl(PARTITIONED);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testReplicatedTtl() throws Exception {
+        checkTtl(REPLICATED);
+    }
+
+    /**
+     * @param mode Cache mode.
+     * @throws Exception If failed.
+     */
+    private void checkTtl(GridCacheMode mode) throws Exception {
+        cacheMode = mode;
+
+        final GridKernal g = (GridKernal)startGrid(0);
+
+        try {
+            final String key = "key";
+
+            g.jcache(null).withExpiryPolicy(
+                    new TouchedExpiryPolicy(new Duration(MILLISECONDS, 
1000))).put(key, 1);
+
+            assertEquals(1, g.jcache(null).get(key));
+
+            U.sleep(1100);
+
+            GridTestUtils.retryAssert(log, 10, 100, new CAX() {
+                @Override public void applyx() {
+                    // Check that no more entries left in the map.
+                    assertNull(g.jcache(null).get(key));
+
+                    if (!g.internalCache().context().deferredDelete())
+                        assertNull(g.internalCache().map().getEntry(key));
+                }
+            });
+        }
+        finally {
+            stopAllGrids();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheUtilsSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheUtilsSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheUtilsSelfTest.java
new file mode 100644
index 0000000..056081d
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheUtilsSelfTest.java
@@ -0,0 +1,261 @@
+/*
+ * 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.internal.util.typedef.internal.*;
+import org.apache.ignite.testframework.*;
+import org.apache.ignite.testframework.junits.common.*;
+
+import java.io.*;
+import java.util.concurrent.*;
+
+/**
+ * Grid cache utils test.
+ */
+public class GridCacheUtilsSelfTest extends GridCommonAbstractTest {
+    /** */
+    private static final String EXTERNALIZABLE_WARNING = "For best performance 
you should implement " +
+        "java.io.Externalizable";
+
+    /**
+     * Does not override equals and hashCode.
+     */
+    private static class NoEqualsAndHashCode {
+    }
+
+    /**
+     * Does not override equals.
+     */
+    private static class NoEquals {
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return 1;
+        }
+    }
+
+    /**
+     * Does not override hashCode.
+     */
+    private static class NoHashCode {
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object obj) {
+            return super.equals(obj);
+        }
+    }
+
+    /**
+     * Defines equals with different signature.
+     */
+    private static class WrongEquals {
+        /**
+         * @param obj Object.
+         * @return {@code False}.
+         */
+        @SuppressWarnings("CovariantEquals")
+        public boolean equals(String obj) {
+            return false;
+        }
+    }
+
+    /**
+     * Overrides equals and hashCode.
+     */
+    private static class EqualsAndHashCode {
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return super.hashCode();
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object obj) {
+            return super.equals(obj);
+        }
+    }
+
+    /**
+     * Overrides equals and hashCode and implements {@link Externalizable}.
+     */
+    private static class ExternalizableEqualsAndHashCode implements 
Externalizable {
+        /**
+         * Constructor required by {@link Externalizable}.
+         */
+        public ExternalizableEqualsAndHashCode() {
+            // No-op.
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return super.hashCode();
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object obj) {
+            return super.equals(obj);
+        }
+
+        /** {@inheritDoc} */
+        @Override public void writeExternal(ObjectOutput out) throws 
IOException {
+            // No-op.
+        }
+
+        /** {@inheritDoc} */
+        @Override public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
+            // No-op.
+        }
+    }
+
+    /**
+     * Extends class which overrides equals and hashCode.
+     */
+    private static class ExtendsClassWithEqualsAndHashCode extends 
EqualsAndHashCode {
+    }
+
+    /**
+     * Extends class which overrides equals and hashCode, overrides equals and 
hashCode.
+     */
+    private static class ExtendsClassWithEqualsAndHashCode2 extends 
EqualsAndHashCode {
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return super.hashCode();
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object obj) {
+            return super.equals(obj);
+        }
+    }
+
+    /**
+     * Does not implement {@link Externalizable}.
+     */
+    private static class NoImplExternalizable {
+    }
+
+    /**
+     * Implements {@link Externalizable}.
+     */
+    private static class ImplExternalizable implements Externalizable  {
+        /**
+         * Constructor required by {@link Externalizable}.
+         */
+        public ImplExternalizable() {
+            // No-op.
+        }
+
+        /** {@inheritDoc} */
+        @Override public void writeExternal(ObjectOutput out) throws 
IOException {
+            // No-op.
+        }
+
+        /** {@inheritDoc} */
+        @Override public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
+            // No-op.
+        }
+    }
+
+    /**
+     * Extends class which implements {@link Externalizable}.
+     */
+    private static class ExtendsImplExternalizable extends ImplExternalizable {
+        /**
+         * Constructor required by {@link Externalizable}.
+         */
+        public ExtendsImplExternalizable() {
+            // No-op.
+        }
+    }
+
+    /**
+     */
+    public void testCacheKeyValidation() {
+        CU.validateCacheKey(log, "key");
+
+        CU.validateCacheKey(log, 1);
+
+        CU.validateCacheKey(log, 1L);
+
+        CU.validateCacheKey(log, 1.0);
+
+        CU.validateCacheKey(log, new ExtendsClassWithEqualsAndHashCode());
+
+        CU.validateCacheKey(log, new ExtendsClassWithEqualsAndHashCode2());
+
+        assertThrowsForInvalidKey(new NoEqualsAndHashCode());
+
+        assertThrowsForInvalidKey(new NoEquals());
+
+        assertThrowsForInvalidKey(new NoHashCode());
+
+        assertThrowsForInvalidKey(new WrongEquals());
+
+        IgniteLogger log = new GridStringLogger(false);
+
+        CU.validateCacheKey(log, new ExternalizableEqualsAndHashCode());
+
+        assertFalse(log.toString().contains(EXTERNALIZABLE_WARNING));
+
+        CU.validateCacheKey(log, "key");
+
+        assertFalse(log.toString().contains(EXTERNALIZABLE_WARNING));
+
+        CU.validateCacheKey(log, new EqualsAndHashCode());
+
+        assertTrue(log.toString().contains(EXTERNALIZABLE_WARNING));
+    }
+
+    /**
+     */
+    public void testCacheValueValidation() {
+        IgniteLogger log = new GridStringLogger(false);
+
+        CU.validateCacheValue(log, new ImplExternalizable());
+
+        assertFalse(log.toString().contains(EXTERNALIZABLE_WARNING));
+
+        CU.validateCacheValue(log, new ExtendsImplExternalizable());
+
+        assertFalse(log.toString().contains(EXTERNALIZABLE_WARNING));
+
+        CU.validateCacheValue(log, "val");
+
+        assertFalse(log.toString().contains(EXTERNALIZABLE_WARNING));
+
+        CU.validateCacheValue(log, new byte[10]);
+
+        assertFalse(log.toString().contains(EXTERNALIZABLE_WARNING));
+
+        CU.validateCacheValue(log, new NoImplExternalizable());
+
+        assertTrue(log.toString().contains(EXTERNALIZABLE_WARNING));
+    }
+
+    /**
+     * @param key Cache key.
+     */
+    private void assertThrowsForInvalidKey(final Object key) {
+        GridTestUtils.assertThrows(log, new Callable<Void>() {
+            @Override public Void call() throws Exception {
+                CU.validateCacheKey(log, key);
+
+                return null;
+            }
+        }, IllegalArgumentException.class, null);
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueBytesPreloadingSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueBytesPreloadingSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueBytesPreloadingSelfTest.java
new file mode 100644
index 0000000..f7f6262
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueBytesPreloadingSelfTest.java
@@ -0,0 +1,151 @@
+/*
+ * 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.cache.*;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.testframework.junits.common.*;
+
+import java.util.*;
+
+import static org.apache.ignite.cache.GridCacheAtomicityMode.*;
+import static org.apache.ignite.cache.GridCacheDistributionMode.*;
+import static org.apache.ignite.cache.GridCacheMemoryMode.*;
+import static org.apache.ignite.cache.GridCacheMode.*;
+import static org.apache.ignite.cache.GridCacheWriteSynchronizationMode.*;
+
+/**
+ *
+ */
+public class GridCacheValueBytesPreloadingSelfTest extends 
GridCommonAbstractTest {
+    /** Memory mode. */
+    private GridCacheMemoryMode memMode;
+
+    @Override protected IgniteConfiguration getConfiguration(String gridName) 
throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        cfg.setCacheConfiguration(cacheConfiguration(gridName));
+
+        return cfg;
+    }
+
+    /**
+     * @param gridName Grid name.
+     * @return Cache configuration.
+     * @throws Exception If failed.
+     */
+    protected CacheConfiguration cacheConfiguration(String gridName) throws 
Exception {
+        CacheConfiguration ccfg = new CacheConfiguration();
+
+        ccfg.setCacheMode(PARTITIONED);
+        ccfg.setBackups(1);
+        ccfg.setAtomicityMode(ATOMIC);
+        ccfg.setDistributionMode(PARTITIONED_ONLY);
+        ccfg.setStoreValueBytes(true);
+        ccfg.setWriteSynchronizationMode(FULL_SYNC);
+        ccfg.setMemoryMode(memMode);
+        ccfg.setOffHeapMaxMemory(1024 * 1024 * 1024);
+        ccfg.setPreloadMode(GridCachePreloadMode.SYNC);
+
+        return ccfg;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOnHeapTiered() throws Exception {
+        memMode = ONHEAP_TIERED;
+
+        startGrids(1);
+
+        try {
+            checkByteArrays();
+        }
+        finally {
+            stopAllGrids();
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOffHeapTiered() throws Exception {
+        memMode = OFFHEAP_TIERED;
+
+        startGrids(1);
+
+        try {
+            checkByteArrays();
+        }
+        finally {
+            stopAllGrids();
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testOffHeapValuesOnly() throws Exception {
+        memMode = OFFHEAP_VALUES;
+
+        startGrids(1);
+
+        try {
+            checkByteArrays();
+        }
+        finally {
+            stopAllGrids();
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void checkByteArrays() throws Exception {
+        int keyCnt = 1000;
+
+        byte[] val = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
+
+        for (int i = 0; i < keyCnt; i++)
+            grid(0).cache(null).put(String.valueOf(i), val);
+
+        for (int i = 0; i < keyCnt; i++)
+            grid(0).cache(null).get(String.valueOf(i));
+
+        startGrid(1);
+
+        if (memMode == ONHEAP_TIERED) {
+            for (int i = 0; i < keyCnt; i++)
+                grid(0).cache(null).evict(String.valueOf(i));
+
+            for (int i = 0; i < keyCnt; i++)
+                grid(0).cache(null).promote(String.valueOf(i));
+        }
+
+        startGrid(2);
+
+        for (int g = 0; g < 3; g++) {
+            for (int i = 0; i < keyCnt; i++) {
+                byte[] o = (byte[])grid(g).cache(null).get(String.valueOf(i));
+
+                assertTrue("Got invalid value [val=" + Arrays.toString(val) + 
", actual=" + Arrays.toString(o) + ']',
+                    Arrays.equals(val, o));
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyAbstractSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyAbstractSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyAbstractSelfTest.java
new file mode 100644
index 0000000..453124a
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyAbstractSelfTest.java
@@ -0,0 +1,322 @@
+/*
+ * 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.cluster.*;
+
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
+
+import static org.apache.ignite.IgniteSystemProperties.*;
+import static org.apache.ignite.cache.GridCacheAtomicWriteOrderMode.*;
+import static org.apache.ignite.cache.GridCacheMode.*;
+import static org.apache.ignite.cache.GridCacheDistributionMode.*;
+import static org.apache.ignite.cache.GridCachePreloadMode.*;
+import static org.apache.ignite.cache.GridCacheWriteSynchronizationMode.*;
+
+/**
+ *
+ */
+public abstract class GridCacheValueConsistencyAbstractSelfTest extends 
GridCacheAbstractSelfTest {
+    /** Number of threads for test. */
+    private static final int THREAD_CNT = 16;
+
+    /** */
+    private String sizePropVal;
+
+    /** {@inheritDoc} */
+    @Override protected int gridCount() {
+        return 4;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected long getTestTimeout() {
+        return 60000;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheConfiguration cacheConfiguration(String gridName) 
throws Exception {
+        CacheConfiguration cCfg = super.cacheConfiguration(gridName);
+
+        cCfg.setCacheMode(PARTITIONED);
+        cCfg.setAtomicityMode(atomicityMode());
+        cCfg.setAtomicWriteOrderMode(writeOrderMode());
+        cCfg.setDistributionMode(distributionMode());
+        cCfg.setPreloadMode(SYNC);
+        cCfg.setWriteSynchronizationMode(FULL_SYNC);
+        cCfg.setBackups(1);
+
+        return cCfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        // Need to increase value set in GridAbstractTest
+        sizePropVal = System.getProperty(GG_ATOMIC_CACHE_DELETE_HISTORY_SIZE);
+
+        System.setProperty(GG_ATOMIC_CACHE_DELETE_HISTORY_SIZE, "100000");
+
+        super.beforeTestsStarted();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() throws Exception {
+        super.afterTestsStopped();
+
+        System.setProperty(GG_ATOMIC_CACHE_DELETE_HISTORY_SIZE, sizePropVal != 
null ? sizePropVal : "");
+    }
+
+    /**
+     * @return Distribution mode.
+     */
+    @Override protected GridCacheDistributionMode distributionMode() {
+        return PARTITIONED_ONLY;
+    }
+
+    /**
+     * @return Atomic write order mode.
+     */
+    protected GridCacheAtomicWriteOrderMode writeOrderMode() {
+        return CLOCK;
+    }
+
+    /**
+     * @return Consistency test iteration count.
+     */
+    protected abstract int iterationCount();
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutRemove() throws Exception {
+        awaitPartitionMapExchange();
+
+        GridCache<String, Integer> cache = cache();
+
+        int keyCnt = 10;
+
+        for (int i = 0; i < keyCnt; i++)
+            cache.put("key" + i, i);
+
+        for (int g = 0; g < gridCount(); g++) {
+            GridCache<String, Integer> cache0 = cache(g);
+            ClusterNode locNode = grid(g).localNode();
+
+            for (int i = 0; i < keyCnt; i++) {
+                String key = "key" + i;
+
+                if 
(cache.affinity().mapKeyToPrimaryAndBackups(key).contains(locNode)) {
+                    info("Node is reported as affinity node for key [key=" + 
key + ", nodeId=" + locNode.id() + ']');
+
+                    assertEquals((Integer)i, cache0.peek(key));
+                }
+                else {
+                    info("Node is reported as NOT affinity node for key [key=" 
+ key +
+                        ", nodeId=" + locNode.id() + ']');
+
+                    if (distributionMode() == NEAR_PARTITIONED && cache == 
cache0)
+                        assertEquals((Integer)i, cache0.peek(key));
+                    else
+                        assertNull(cache0.peek(key));
+                }
+
+                assertEquals((Integer)i, cache0.get(key));
+            }
+        }
+
+        info("Removing values from cache.");
+
+        for (int i = 0; i < keyCnt; i++)
+            assertEquals((Integer)i, cache.remove("key" + i));
+
+        for (int g = 0; g < gridCount(); g++) {
+            GridCache<String, Integer> cache0 = cache(g);
+
+            for (int i = 0; i < keyCnt; i++) {
+                String key = "key" + i;
+
+                assertNull(cache0.peek(key));
+
+                assertNull(cache0.get(key));
+            }
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutRemoveAll() throws Exception {
+        awaitPartitionMapExchange();
+
+        GridCache<String, Integer> cache = cache();
+
+        int keyCnt = 10;
+
+        for (int i = 0; i < keyCnt; i++) {
+            info("Putting value to cache: " + i);
+
+            cache.put("key" + i, i);
+        }
+
+        for (int g = 0; g < gridCount(); g++) {
+            GridCache<String, Integer> cache0 = cache(g);
+            ClusterNode locNode = grid(g).localNode();
+
+            for (int i = 0; i < keyCnt; i++) {
+                String key = "key" + i;
+
+                if 
(cache.affinity().mapKeyToPrimaryAndBackups(key).contains(grid(g).localNode())) 
{
+                    info("Node is reported as affinity node for key [key=" + 
key + ", nodeId=" + locNode.id() + ']');
+
+                    assertEquals((Integer)i, cache0.peek(key));
+                }
+                else {
+                    info("Node is reported as NOT affinity node for key [key=" 
+ key +
+                        ", nodeId=" + locNode.id() + ']');
+
+                    if (distributionMode() == NEAR_PARTITIONED && cache == 
cache0)
+                        assertEquals((Integer)i, cache0.peek(key));
+                    else
+                        assertNull(cache0.peek(key));
+                }
+
+                assertEquals((Integer)i, cache0.get(key));
+            }
+        }
+
+        for (int g = 0; g < gridCount(); g++) {
+            info(">>>> Removing all values form cache: " + g);
+
+            cache(g).removeAll();
+        }
+
+        info(">>>> Starting values check");
+
+        for (int g = 0; g < gridCount(); g++) {
+            GridCache<String, Integer> cache0 = cache(g);
+
+            for (int i = 0; i < keyCnt; i++) {
+                String key = "key" + i;
+
+                assertNull(cache0.peek(key));
+                assertNull(cache0.get(key));
+            }
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutRemoveConsistencyMultithreaded() throws Exception {
+        final int range = 10000;
+
+        final int iterCnt = iterationCount();
+
+        final AtomicInteger iters = new AtomicInteger();
+
+        multithreadedAsync(new Callable<Object>() {
+            @Override public Object call() throws Exception {
+                Random rnd = new Random();
+
+                while (true) {
+                    int i = iters.getAndIncrement();
+
+                    if (i >= iterCnt)
+                        break;
+
+                    int g = rnd.nextInt(gridCount());
+
+                    Ignite ignite = grid(g);
+
+                    GridCache<Object, Object> cache = ignite.cache(null);
+
+                    int k = rnd.nextInt(range);
+
+                    boolean rmv = rnd.nextBoolean();
+
+                    if (!rmv)
+                        cache.put(k, Thread.currentThread().getId());
+                    else
+                        cache.remove(k);
+
+                    if (i > 0 && i % 5000 == 0)
+                        info("Completed: " + i);
+                }
+
+                return null;
+            }
+        }, THREAD_CNT).get();
+
+        int present = 0;
+        int absent = 0;
+
+        for (int i = 0; i < range; i++) {
+            Long firstVal = null;
+
+            for (int g = 0; g < gridCount(); g++) {
+                Long val = (Long)grid(g).cache(null).peek(i);
+
+                if (firstVal == null && val != null)
+                    firstVal = val;
+
+                assert val == null || firstVal.equals(val) : "Invalid value 
detected [val=" + val +
+                    ", firstVal=" + firstVal + ']';
+            }
+
+            if (firstVal == null)
+                absent++;
+            else
+                present++;
+        }
+
+        info("Finished check [present=" + present + ", absent=" + absent + 
']');
+
+        info("Checking keySet consistency");
+
+        for (int g = 0; g < gridCount(); g++)
+            checkKeySet(grid(g));
+    }
+
+    /**
+     * @param g Grid to check.
+     */
+    private void checkKeySet(Ignite g) {
+        GridCache<Object, Object> cache = g.cache(null);
+
+        Set<Object> keys = cache.keySet();
+
+        int cacheSize = cache.size();
+        int keySetSize = keys.size();
+
+        int itSize = 0;
+
+        for (Object ignored : keys)
+            itSize++;
+
+        int valsSize = cache.values().size();
+
+        info("cacheSize=" + cacheSize + ", keysSize=" + keySetSize + ", 
valsSize=" + valsSize +
+            ", itSize=" + itSize + ']');
+
+        assertEquals("cacheSize vs itSize", cacheSize, itSize);
+        assertEquals("cacheSize vs keySeySize", cacheSize, keySetSize);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyTransactionalNearEnabledSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyTransactionalNearEnabledSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyTransactionalNearEnabledSelfTest.java
new file mode 100644
index 0000000..d28b573
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyTransactionalNearEnabledSelfTest.java
@@ -0,0 +1,31 @@
+/*
+ * 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.cache.*;
+
+/**
+ * Tests cache values consistency for transactional cache with near cache 
enabled.
+ */
+public class GridCacheValueConsistencyTransactionalNearEnabledSelfTest
+    extends GridCacheValueConsistencyTransactionalSelfTest {
+    /** {@inheritDoc} */
+    @Override protected GridCacheDistributionMode distributionMode() {
+        return GridCacheDistributionMode.NEAR_PARTITIONED;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyTransactionalSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyTransactionalSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyTransactionalSelfTest.java
new file mode 100644
index 0000000..67a81c9
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheValueConsistencyTransactionalSelfTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.cache.*;
+
+import static org.apache.ignite.cache.GridCacheAtomicityMode.*;
+
+/**
+ * Tests cache values consistency for transactional cache.
+ */
+public class GridCacheValueConsistencyTransactionalSelfTest extends 
GridCacheValueConsistencyAbstractSelfTest {
+    /** {@inheritDoc} */
+    @Override protected GridCacheAtomicityMode atomicityMode() {
+        return TRANSACTIONAL;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected int iterationCount() {
+        return 100_000;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheVariableTopologySelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheVariableTopologySelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheVariableTopologySelfTest.java
new file mode 100644
index 0000000..558c597
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheVariableTopologySelfTest.java
@@ -0,0 +1,191 @@
+/*
+ * 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.configuration.*;
+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.transactions.*;
+import org.apache.ignite.internal.util.typedef.*;
+import org.apache.ignite.testframework.*;
+import org.apache.ignite.testframework.junits.common.*;
+
+import java.util.*;
+import java.util.concurrent.atomic.*;
+
+import static org.apache.ignite.cache.GridCacheAtomicityMode.*;
+import static org.apache.ignite.cache.GridCacheMode.*;
+
+/**
+ * Affinity routing tests.
+ */
+public class GridCacheVariableTopologySelfTest extends GridCommonAbstractTest {
+    /** */
+    private static final Random RAND = new Random();
+
+    /** */
+    private TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true);
+
+    /** Constructs test. */
+    public GridCacheVariableTopologySelfTest() {
+        super(/* don't start grid */ false);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String gridName) 
throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        TcpDiscoverySpi spi = new TcpDiscoverySpi();
+
+        spi.setIpFinder(ipFinder);
+
+        cfg.setDiscoverySpi(spi);
+
+        // Default cache configuration.
+        CacheConfiguration cacheCfg = defaultCacheConfiguration();
+
+        cacheCfg.setCacheMode(PARTITIONED);
+        cacheCfg.setBackups(1);
+        
cacheCfg.setWriteSynchronizationMode(GridCacheWriteSynchronizationMode.FULL_SYNC);
+        cacheCfg.setAtomicityMode(TRANSACTIONAL);
+
+        cfg.setCacheConfiguration(cacheCfg);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        stopAllGrids();
+
+        assert G.allGrids().isEmpty();
+    }
+
+    /**
+     * @param cnt Number of grids to starts.
+     * @param startIdx Start grid index.
+     * @throws Exception If failed to start grids.
+     */
+    private void startGrids(int cnt, int startIdx) throws Exception {
+        assert startIdx >= 0;
+        assert cnt >= 0;
+
+        for (int idx = startIdx; idx < startIdx + cnt; idx++)
+            startGrid(idx);
+    }
+
+    /**
+     * JUnit.
+     *
+     * @throws Exception If failed.
+     */
+    @SuppressWarnings({"TooBroadScope"})
+    public void testNodeStop() throws Exception {
+        // -- Test parameters. -- //
+        int nodeCnt = 3;
+        int threadCnt = 20;
+        final int txCnt = 1000;
+        final long txDelay = 0;
+        final int keyRange = 1000;
+        final int logMod = 20;
+
+        assert nodeCnt > 1 : "Node count for this test must be greater than 
1.";
+
+        startGrids(nodeCnt, 0);
+
+        final AtomicBoolean done = new AtomicBoolean();
+
+        IgniteFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CAX() {
+            /** */
+            private int cnt;
+
+            @SuppressWarnings({"BusyWait"})
+            @Override public void applyx() throws IgniteCheckedException {
+                while (cnt++ < txCnt && !done.get()) {
+                    GridCache<Object, Object> cache = grid(0).cache(null);
+
+                    if (cnt % logMod == 0)
+                        info("Starting transaction: " + cnt);
+
+                    try (IgniteTx tx = cache.txStart()) {
+                        int kv = RAND.nextInt(keyRange);
+
+                        cache.put(kv, kv);
+
+                        cache.get(kv);
+
+                        tx.commit();
+                    }
+                    catch (IgniteTxOptimisticException e) {
+                        info("Caught cache optimistic exception: " + e);
+                    }
+
+                    try {
+                        Thread.sleep(txDelay);
+                    }
+                    catch (InterruptedException ignored) {
+                        // No-op.
+                    }
+                }
+            }
+        }, threadCnt, "TEST-THREAD");
+
+        Thread.sleep(2000);
+
+        for (int idx = 1; idx < nodeCnt; idx++) {
+            info("Stopping node: " + idx);
+
+            stopGrid(idx);
+        }
+
+        // This is just for debugging.
+        /*
+        GridFuture<?> debugFut = GridTestUtils.runMultiThreadedAsync(new 
Runnable() {
+            @SuppressWarnings({"UnusedDeclaration"})
+            @Override public void run() {
+                GridCache<Object, Object> cache = grid(0).cache(null);
+
+                try {
+                    Thread.sleep(15000);
+                }
+                catch (InterruptedException ignored) {
+                    return;
+                }
+
+                info("Set breakpoint here.");
+            }
+        }, 1, "TEST-THREAD");
+        */
+
+        done.set(true);
+
+        fut.get();
+
+        stopGrid(0);
+
+        info("Grid 0 stopped.");
+
+        //debugFut.get();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheVersionSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheVersionSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheVersionSelfTest.java
new file mode 100644
index 0000000..cdf371b
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheVersionSelfTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.internal.processors.cache.*;
+import org.apache.ignite.testframework.*;
+import org.apache.ignite.testframework.junits.common.*;
+
+import java.util.concurrent.*;
+
+/**
+ *
+ */
+public class GridCacheVersionSelfTest extends GridCommonAbstractTest {
+    /**
+     * @throws Exception If failed.
+     */
+    public void testTopologyVersionDrId() throws Exception {
+        GridCacheVersion ver = version(10, 0);
+
+        assertEquals(10, ver.nodeOrder());
+        assertEquals(0, ver.dataCenterId());
+
+        // Check with max topology version and some dr IDs.
+        ver = version(0x7FFFFFF, 0);
+        assertEquals(0x7FFFFFF, ver.nodeOrder());
+        assertEquals(0, ver.dataCenterId());
+
+        ver = version(0x7FFFFFF, 15);
+        assertEquals(0x7FFFFFF, ver.nodeOrder());
+        assertEquals(15, ver.dataCenterId());
+
+        ver = version(0x7FFFFFF, 31);
+        assertEquals(0x7FFFFFF, ver.nodeOrder());
+        assertEquals(31, ver.dataCenterId());
+
+        // Check max dr ID with some topology versions.
+        ver = version(11, 31);
+        assertEquals(11, ver.nodeOrder());
+        assertEquals(31, ver.dataCenterId());
+
+        ver = version(256, 31);
+        assertEquals(256, ver.nodeOrder());
+        assertEquals(31, ver.dataCenterId());
+
+        ver = version(1025, 31);
+        assertEquals(1025, ver.nodeOrder());
+        assertEquals(31, ver.dataCenterId());
+
+        // Check overflow exception.
+        GridTestUtils.assertThrows(log, new Callable<Object>() {
+            @Override public Object call() throws Exception {
+                return version(0x7FFFFFF + 1, 1);
+            }
+        }, IllegalArgumentException.class, null);
+    }
+
+    /**
+     * @param nodeOrder Node order.
+     * @param drId Data center ID.
+     * @return Cache version.
+     */
+    private GridCacheVersion version(int nodeOrder, int drId) {
+        return new GridCacheVersion(0, 0, 0, nodeOrder, drId);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/968c3cf8/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStoreAbstractSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStoreAbstractSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStoreAbstractSelfTest.java
index 27e09c0..ff954de 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStoreAbstractSelfTest.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStoreAbstractSelfTest.java
@@ -17,11 +17,9 @@
 
 package org.apache.ignite.internal.processors.cache;
 
-import org.apache.ignite.internal.processors.cache.*;
 import org.apache.ignite.lang.*;
 import org.apache.ignite.internal.util.typedef.internal.*;
 import org.apache.ignite.testframework.junits.common.*;
-import org.gridgain.grid.kernal.processors.cache.*;
 
 import java.util.*;
 import java.util.concurrent.*;

Reply via email to