IGNITE-2649 - Ignition.localIgnite() unreliable under Gateways and cause wrong components deserialization.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/087f84f0 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/087f84f0 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/087f84f0 Branch: refs/heads/ignite-2649 Commit: 087f84f09f820948fa590e73509bf5568185ded4 Parents: 09a30f3 Author: dkarachentsev <dkarachent...@gridgain.com> Authored: Wed Aug 3 18:36:49 2016 +0300 Committer: dkarachentsev <dkarachent...@gridgain.com> Committed: Wed Aug 3 18:36:49 2016 +0300 ---------------------------------------------------------------------- .../cache/GridLocalIgniteSerializationTest.java | 373 +++++++++++++++++++ .../testsuites/IgniteCacheTestSuite4.java | 3 + 2 files changed, 376 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/087f84f0/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridLocalIgniteSerializationTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridLocalIgniteSerializationTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridLocalIgniteSerializationTest.java new file mode 100644 index 0000000..4a8c60c --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridLocalIgniteSerializationTest.java @@ -0,0 +1,373 @@ +/* + * 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.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.Ignition; +import org.apache.ignite.binary.BinaryObjectException; +import org.apache.ignite.binary.BinaryReader; +import org.apache.ignite.binary.BinaryWriter; +import org.apache.ignite.binary.Binarylizable; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.binary.BinaryMarshaller; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectInputStream; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.concurrent.Callable; + +/** + * + */ +public class GridLocalIgniteSerializationTest extends GridCommonAbstractTest { + /** */ + private static final String CACHE_NAME = "cache_name"; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(final String gridName) throws Exception { + final IgniteConfiguration cfg = super.getConfiguration(gridName); + + if (gridName != null && gridName.startsWith("binary")) + cfg.setMarshaller(new BinaryMarshaller()); + + return cfg; + } + + /** + * Test that calling {@link Ignition#localIgnite()} + * is safe for binary marshaller. + * + * @throws Exception + */ + public void testPutGetSimple() throws Exception { + testPutGet(new SimpleTestObject("one"), null); + } + + /** + * @throws Exception If failed. + */ + public void testPutGetSerializable() throws Exception { + testPutGet(new SerializableTestObject("test"), null); + } + + /** + * @throws Exception If failed. + */ + public void testPutGetExternalizable() throws Exception { + testPutGet(new ExternalizableTestObject("test"), null); + } + + /** + * @throws Exception If failed. + */ + public void testPutGetBinarylizable() throws Exception { + testPutGet(new BinarylizableTestObject("test"), "binaryIgnite"); + } + + /** + * @throws Exception If failed. + */ + private void testPutGet(final TestObject obj, final String gridName) throws Exception { + // Run async to emulate user thread. + GridTestUtils.runAsync(new Callable<Object>() { + @Override public Object call() throws Exception { + try (final Ignite ignite = startGrid(gridName)) { + final IgniteCache<Integer, TestObject> cache = ignite.getOrCreateCache(CACHE_NAME); + + assertNull(obj.ignite()); + + cache.put(1, obj); + + assertNotNull(obj.ignite()); + + final TestObject loadedObj = cache.get(1); + + assertNotNull(loadedObj.ignite()); + + assertEquals(obj, loadedObj); + } + + return null; + } + }).get(); + } + + /** + * + */ + private interface TestObject { + /** + * @return Ignite instance. + */ + Ignite ignite(); + } + + /** + * Test object. + */ + private static class SimpleTestObject implements TestObject { + /** */ + private final String val; + + /** */ + private transient Ignite ignite; + + /** */ + private SimpleTestObject(final String val) { + this.val = val; + } + + /** + * @return Object. + */ + @SuppressWarnings("unused") + private Object readResolve() { + ignite = Ignition.localIgnite(); + + return this; + } + + /** + * @return Object. + */ + @SuppressWarnings("unused") + private Object writeReplace() { + ignite = Ignition.localIgnite(); + + return this; + } + + /** */ + @Override public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + final SimpleTestObject simpleTestObj = (SimpleTestObject) o; + + return val != null ? val.equals(simpleTestObj.val) : simpleTestObj.val == null; + + } + + /** */ + @Override public int hashCode() { + return val != null ? val.hashCode() : 0; + } + + /** {@inheritDoc} */ + @Override public Ignite ignite() { + return ignite; + } + } + + /** + * + */ + private static class SerializableTestObject implements Serializable, TestObject { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private String val; + + /** */ + private transient Ignite ignite; + + /** + * + */ + public SerializableTestObject() { + } + + /** + * @param val Value + */ + public SerializableTestObject(final String val) { + this.val = val; + } + + /** + * @param out Object output. + * @throws IOException If fail. + */ + private void writeObject(ObjectOutputStream out) throws IOException { + U.writeString(out, val); + + ignite = Ignition.localIgnite(); + } + + /** + * @param in Object input. + * @throws IOException If fail. + */ + private void readObject(ObjectInputStream in) throws IOException { + val = U.readString(in); + + ignite = Ignition.localIgnite(); + } + + /** {@inheritDoc} */ + @Override public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + final SerializableTestObject that = (SerializableTestObject) o; + + return val != null ? val.equals(that.val) : that.val == null; + + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return val != null ? val.hashCode() : 0; + } + + /** {@inheritDoc} */ + @Override public Ignite ignite() { + return ignite; + } + } + + /** + * + */ + private static class ExternalizableTestObject implements Externalizable, TestObject { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private String val; + + /** */ + private transient Ignite ignite; + + /** + * + */ + public ExternalizableTestObject() { + } + + /** + * @param val Value. + */ + public ExternalizableTestObject(final String val) { + this.val = val; + } + + /** {@inheritDoc} */ + @Override public void writeExternal(final ObjectOutput out) throws IOException { + U.writeString(out, val); + + ignite = Ignition.localIgnite(); + } + + /** {@inheritDoc} */ + @Override public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { + val = U.readString(in); + + ignite = Ignition.localIgnite(); + } + + /** {@inheritDoc} */ + @Override public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + final ExternalizableTestObject that = (ExternalizableTestObject) o; + + return val != null ? val.equals(that.val) : that.val == null; + + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return val != null ? val.hashCode() : 0; + } + + /** {@inheritDoc} */ + @Override public Ignite ignite() { + return ignite; + } + } + + /** + * + */ + private static class BinarylizableTestObject implements Binarylizable, TestObject { + /** */ + private String val; + + /** */ + private transient Ignite ignite; + + /** + * + */ + public BinarylizableTestObject() { + } + + /** + * @param val Value. + */ + public BinarylizableTestObject(final String val) { + this.val = val; + } + + /** {@inheritDoc} */ + @Override public void writeBinary(final BinaryWriter writer) throws BinaryObjectException { + writer.rawWriter().writeString(val); + + ignite = Ignition.localIgnite(); + } + + /** {@inheritDoc} */ + @Override public void readBinary(final BinaryReader reader) throws BinaryObjectException { + val = reader.rawReader().readString(); + + ignite = Ignition.localIgnite(); + } + + /** {@inheritDoc} */ + @Override public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + final BinarylizableTestObject that = (BinarylizableTestObject) o; + + return val != null ? val.equals(that.val) : that.val == null; + + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return val != null ? val.hashCode() : 0; + } + + /** {@inheritDoc} */ + @Override public Ignite ignite() { + return ignite; + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/087f84f0/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java index 883f426..4d03b8a 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java @@ -51,6 +51,7 @@ import org.apache.ignite.internal.processors.cache.GridCacheMultinodeUpdateSelfT import org.apache.ignite.internal.processors.cache.GridCacheStoreManagerDeserializationTest; import org.apache.ignite.internal.processors.cache.GridCacheVersionMultinodeTest; import org.apache.ignite.internal.processors.cache.GridLocalCacheStoreManagerDeserializationTest; +import org.apache.ignite.internal.processors.cache.GridLocalIgniteSerializationTest; import org.apache.ignite.internal.processors.cache.IgniteCacheAtomicCopyOnReadDisabledTest; import org.apache.ignite.internal.processors.cache.IgniteCacheAtomicLocalPeekModesTest; import org.apache.ignite.internal.processors.cache.IgniteCacheAtomicLocalStoreValueTest; @@ -319,6 +320,8 @@ public class IgniteCacheTestSuite4 extends TestSuite { suite.addTestSuite(MarshallerCacheJobRunNodeRestartTest.class); + suite.addTestSuite(GridLocalIgniteSerializationTest.class); + return suite; } } \ No newline at end of file