forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gzip compression reads bytes ahead. That was causing serialization bugs when more data available after a object serialized with compression enabled. As fix, we are rewinding the stream to correct position so that next data in the stream can be read correctly. Thanks @jerrinot for the fix. fixes hazelcast/hazelcast-enterprise#1750 fixes hazelcast#12104
- Loading branch information
sancar
committed
Jan 9, 2018
1 parent
173734d
commit 951ad92
Showing
2 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
hazelcast/src/test/java/com/hazelcast/internal/serialization/impl/CompressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved. | ||
* | ||
* Licensed 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 com.hazelcast.internal.serialization.impl; | ||
|
||
import com.hazelcast.nio.serialization.Data; | ||
import com.hazelcast.spi.serialization.SerializationService; | ||
import com.hazelcast.test.HazelcastParallelClassRunner; | ||
import com.hazelcast.test.annotation.QuickTest; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.Externalizable; | ||
import java.io.IOException; | ||
import java.io.ObjectInput; | ||
import java.io.ObjectOutput; | ||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(HazelcastParallelClassRunner.class) | ||
@Category({QuickTest.class}) | ||
public class CompressionTest { | ||
|
||
public static class SampleSerializable implements Serializable { | ||
|
||
private int x; | ||
|
||
public SampleSerializable() { | ||
} | ||
|
||
public SampleSerializable(int x) { | ||
this.x = x; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
SampleSerializable that = (SampleSerializable) o; | ||
|
||
return x == that.x; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return x; | ||
} | ||
} | ||
|
||
public static class SampleExternalizable implements Externalizable { | ||
|
||
private int x; | ||
|
||
public SampleExternalizable() { | ||
} | ||
|
||
public SampleExternalizable(int x) { | ||
this.x = x; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
SampleExternalizable that = (SampleExternalizable) o; | ||
|
||
return x == that.x; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return x; | ||
} | ||
|
||
@Override | ||
public void writeExternal(ObjectOutput out) throws IOException { | ||
out.writeInt(x); | ||
} | ||
|
||
@Override | ||
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { | ||
x = in.readInt(); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void testCompression_serializable() { | ||
DefaultSerializationServiceBuilder defaultSerializationServiceBuilder = new DefaultSerializationServiceBuilder(); | ||
SerializationService ss = defaultSerializationServiceBuilder.setEnableCompression(true).build(); | ||
|
||
SampleSerializable expected = new SampleSerializable(5); | ||
Data data = ss.toData(expected); | ||
SampleSerializable result = ss.toObject(data); | ||
|
||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testCompression_serializable_withArrayList() { | ||
DefaultSerializationServiceBuilder defaultSerializationServiceBuilder = new DefaultSerializationServiceBuilder(); | ||
SerializationService ss = defaultSerializationServiceBuilder.setEnableCompression(true).build(); | ||
|
||
ArrayList<SampleSerializable> expected = new ArrayList<SampleSerializable>(); | ||
for (int i = 0; i < 10; i++) { | ||
expected.add(new SampleSerializable(i)); | ||
} | ||
Data data = ss.toData(expected); | ||
ArrayList<SampleSerializable> result = ss.toObject(data); | ||
|
||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testCompression_externalizable() { | ||
DefaultSerializationServiceBuilder defaultSerializationServiceBuilder = new DefaultSerializationServiceBuilder(); | ||
SerializationService ss = defaultSerializationServiceBuilder.setEnableCompression(true).build(); | ||
|
||
SampleExternalizable expected = new SampleExternalizable(5); | ||
Data data = ss.toData(expected); | ||
SampleExternalizable result = ss.toObject(data); | ||
|
||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testCompression_externalizable_withArrayList() { | ||
DefaultSerializationServiceBuilder defaultSerializationServiceBuilder = new DefaultSerializationServiceBuilder(); | ||
SerializationService ss = defaultSerializationServiceBuilder.setEnableCompression(true).build(); | ||
|
||
ArrayList<SampleExternalizable> expected = new ArrayList<SampleExternalizable>(); | ||
for (int i = 0; i < 10; i++) { | ||
expected.add(new SampleExternalizable(i)); | ||
} | ||
Data data = ss.toData(expected); | ||
ArrayList<SampleExternalizable> result = ss.toObject(data); | ||
|
||
assertEquals(expected, result); | ||
} | ||
|
||
} |