Skip to content

Commit 930a0dd

Browse files
authored
adding SparkTestUtils.roundTripThroughJavaSerialization (#5257)
* adding in a new SparkTestUtils.roundTripThroughJavaSerialization method * updating existing code to use the new method
1 parent 868a32e commit 930a0dd

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

src/test/java/org/broadinstitute/hellbender/engine/FeatureInputUnitTest.java

+4-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import htsjdk.variant.vcf.VCFCodec;
77
import org.broadinstitute.barclay.argparser.CommandLineException;
88
import org.broadinstitute.hellbender.GATKBaseTest;
9+
import org.broadinstitute.hellbender.testutils.SparkTestUtils;
910
import org.testng.Assert;
1011
import org.testng.annotations.DataProvider;
1112
import org.testng.annotations.Test;
@@ -210,22 +211,9 @@ public void testFeatureCodecCache() {
210211
@SuppressWarnings("unchecked")
211212
@Test
212213
public void testFeatureCodecCacheSerialization() throws IOException, ClassNotFoundException {
213-
FeatureInput<VariantContext> featureInput = getVariantFeatureInputWithCachedCodec();
214-
215-
// serialize
216-
byte[] serializedFeatureInput;
217-
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream();
218-
final ObjectOutputStream oos = new ObjectOutputStream(bos)) {
219-
oos.writeObject(featureInput);
220-
serializedFeatureInput = bos.toByteArray();
221-
}
222-
Assert.assertNotNull(serializedFeatureInput);
223-
224-
// deserialize
225-
FeatureInput<VariantContext> roundTrippedFeatureInput;
226-
try (final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedFeatureInput))) {
227-
roundTrippedFeatureInput = (FeatureInput<VariantContext>) ois.readObject();
228-
}
214+
final FeatureInput<VariantContext>featureInput = getVariantFeatureInputWithCachedCodec();
215+
216+
final FeatureInput<VariantContext> roundTrippedFeatureInput = SparkTestUtils.roundTripThroughJavaSerialization(featureInput);
229217
Assert.assertNotNull(roundTrippedFeatureInput);
230218

231219
// we expect to lose the cached feature codec class on serialization, but retain the feature path

src/test/java/org/broadinstitute/hellbender/engine/spark/datasources/ReferenceFileSparkSourceUnitTest.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import com.google.common.jimfs.Configuration;
44
import com.google.common.jimfs.Jimfs;
5+
import org.broadinstitute.hellbender.GATKBaseTest;
6+
import org.broadinstitute.hellbender.exceptions.UserException;
7+
import org.broadinstitute.hellbender.testutils.SparkTestUtils;
8+
import org.testng.annotations.Test;
9+
510
import java.io.*;
611
import java.nio.file.FileSystem;
712
import java.nio.file.Files;
813
import java.nio.file.Path;
9-
import org.broadinstitute.hellbender.GATKBaseTest;
10-
import org.broadinstitute.hellbender.engine.spark.datasources.ReferenceFileSparkSource;
11-
import org.broadinstitute.hellbender.exceptions.UserException;
12-
import org.testng.annotations.Test;
1314

1415
public class ReferenceFileSparkSourceUnitTest extends GATKBaseTest {
1516

@@ -42,13 +43,9 @@ public void testDatasourcesReferenceSerializes() throws IOException, ClassNotFou
4243

4344
final ReferenceFileSparkSource referenceFileSource = new ReferenceFileSparkSource(refPath);
4445

45-
// Can we serialize it?
46-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
47-
ObjectOutputStream os = new ObjectOutputStream(baos);
48-
os.writeObject(referenceFileSource);
46+
//can we serialize it?
47+
ReferenceFileSparkSource otherSide = SparkTestUtils.roundTripThroughJavaSerialization(referenceFileSource);
4948

50-
ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
51-
ReferenceFileSparkSource otherSide = (ReferenceFileSparkSource)is.readObject();
5249
// After deserialization, will it crash?
5350
otherSide.getReferenceSequenceDictionary(null);
5451

src/testUtils/java/org/broadinstitute/hellbender/testutils/SparkTestUtils.java

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import scala.reflect.ClassTag;
99
import scala.reflect.ClassTag$;
1010

11+
import java.io.*;
12+
1113
public final class SparkTestUtils {
1214
private SparkTestUtils() {}
1315

@@ -29,4 +31,28 @@ public static <T> T roundTripInKryo(final T input, final Class<?> inputClazz, fi
2931
final ClassTag<T> tag = ClassTag$.MODULE$.apply(inputClazz);
3032
return sparkSerializer.deserialize(sparkSerializer.serialize(input, tag), tag);
3133
}
34+
35+
/**
36+
* Takes an input object and returns the value of the object after it has been serialized and then deserialized
37+
* using Java's built in serialization.
38+
*
39+
* @param input an object to be serialized. Never {@code null}
40+
* @return serialized and deserialized instance of input, may throw if serialization fails
41+
*/
42+
@SuppressWarnings("unchecked")
43+
public static <T> T roundTripThroughJavaSerialization(T input) throws IOException, ClassNotFoundException {
44+
Utils.nonNull(input);
45+
final byte[] serializedBytes;
46+
try(final ByteArrayOutputStream baos = new ByteArrayOutputStream();
47+
final ObjectOutputStream out = new ObjectOutputStream(baos)){
48+
out.writeObject(input);
49+
serializedBytes = baos.toByteArray();
50+
}
51+
52+
try (final ByteArrayInputStream bais = new ByteArrayInputStream(serializedBytes);
53+
final ObjectInputStream in = new ObjectInputStream(bais)){
54+
55+
return (T) in.readObject();
56+
}
57+
}
3258
}

0 commit comments

Comments
 (0)