Skip to content

Commit

Permalink
Split out serialization/testutils/RoundTripping from testutils/TestUt…
Browse files Browse the repository at this point in the history
…ils.

PiperOrigin-RevId: 636520733
Change-Id: Ie68adc32de5ceb0cc0e4e9a8ccaa526118d0ec03
  • Loading branch information
aoeui authored and copybara-github committed May 23, 2024
1 parent 792d17c commit 5239fe6
Show file tree
Hide file tree
Showing 25 changed files with 179 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public abstract class AbstractObjectCodecTest<T> {
private ObjectCodecTester<T> objectCodecTester;

/** Construct with the given codec and subjects. */
protected AbstractObjectCodecTest(
ObjectCodec<T> underTest, @SuppressWarnings("unchecked") T... subjects) {
protected AbstractObjectCodecTest(ObjectCodec<T> underTest, T... subjects) {
this.underTest = underTest;
this.subjects = ImmutableList.copyOf(subjects);
}
Expand Down Expand Up @@ -81,13 +80,13 @@ public void testDeserializeBadDataThrowsSerializationException() {

protected T fromBytes(DeserializationContext context, byte[] bytes)
throws SerializationException, IOException {
return TestUtils.fromBytes(context, underTest, bytes);
return RoundTripping.fromBytes(context, underTest, bytes);
}

/** Serialize subject using the {@link ObjectCodec} under test. */
protected byte[] toBytes(SerializationContext context, T subject)
throws IOException, SerializationException {
return TestUtils.toBytes(context, underTest, subject);
return RoundTripping.toBytes(context, underTest, subject);
}

protected void verifyDeserialization(T deserialized, T subject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ java_library(
["*.java"],
exclude = DUMPER_SOURCES + [
"FakeDirectories.java",
"RoundTripping.java",
"SerializationDepsUtils.java",
],
),
runtime_deps = [
"//src/test/java/com/google/devtools/build/lib/vfs/util", # For FileSystem modules
],
deps = [
":round-tripping",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
Expand All @@ -45,6 +47,16 @@ java_library(
],
)

java_library(
name = "round-tripping",
srcs = ["RoundTripping.java"],
deps = [
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization",
"//third_party:guava",
"//third_party/protobuf:protobuf_java",
],
)

java_library(
name = "depsutils",
srcs = ["SerializationDepsUtils.java"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ void testDeserializeJunkData() {
}

private T fromBytes(byte[] bytes) throws SerializationException, IOException {
return TestUtils.fromBytes(readContext, underTest, bytes);
return RoundTripping.fromBytes(readContext, underTest, bytes);
}

private byte[] toBytes(T subject) throws IOException, SerializationException {
return TestUtils.toBytes(writeContext, underTest, subject);
return RoundTripping.toBytes(writeContext, underTest, subject);
}

/** Builder for {@link ObjectCodecTester}. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2017 The Bazel Authors. 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.google.devtools.build.lib.skyframe.serialization.testutils;

import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.devtools.build.lib.skyframe.serialization.AutoRegistry;
import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecRegistry;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecs;
import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/** Helpers for round tripping in serialization tests. */
public class RoundTripping {

private RoundTripping() {}

/** Serialize a value to a new byte array. */
public static <T> byte[] toBytes(SerializationContext context, ObjectCodec<T> codec, T value)
throws IOException, SerializationException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
CodedOutputStream codedOut = CodedOutputStream.newInstance(bytes);
codec.serialize(context, value, codedOut);
codedOut.flush();
return bytes.toByteArray();
}

public static <T> ByteString toBytes(SerializationContext serializationContext, T value)
throws IOException, SerializationException {
ByteString.Output output = ByteString.newOutput();
CodedOutputStream codedOut = CodedOutputStream.newInstance(output);
serializationContext.serialize(value, codedOut);
codedOut.flush();
return output.toByteString();
}

public static Object fromBytes(DeserializationContext deserializationContext, ByteString bytes)
throws IOException, SerializationException {
return deserializationContext.deserialize(bytes.newCodedInput());
}

/** Deserialize a value from a byte array. */
public static <T> T fromBytes(DeserializationContext context, ObjectCodec<T> codec, byte[] bytes)
throws SerializationException, IOException {
return codec.deserialize(context, CodedInputStream.newInstance(bytes));
}

public static <T> T roundTrip(T value, ObjectCodecRegistry registry)
throws IOException, SerializationException {
return roundTrip(value, new ObjectCodecs(registry));
}

public static <T> T roundTrip(T value, ImmutableClassToInstanceMap<Object> dependencies)
throws IOException, SerializationException {
return roundTrip(value, new ObjectCodecs(dependencies));
}

public static <T> T roundTrip(T value) throws IOException, SerializationException {
return roundTrip(value, new ObjectCodecs());
}

private static <T> T roundTrip(T value, ObjectCodecs codecs) throws SerializationException {
@SuppressWarnings("unchecked")
T result = (T) codecs.deserialize(codecs.serialize(value));
return result;
}

public static ByteString toBytesMemoized(Object original, ObjectCodecRegistry registry)
throws IOException, SerializationException {
return new ObjectCodecs(registry).serializeMemoized(original);
}

public static Object fromBytesMemoized(ByteString bytes, ObjectCodecRegistry registry)
throws SerializationException {
return new ObjectCodecs(registry).deserializeMemoized(bytes);
}

@SuppressWarnings("unchecked")
public static <T> T roundTripMemoized(T original, ObjectCodecRegistry registry)
throws IOException, SerializationException {
ObjectCodecs codecs = new ObjectCodecs(registry);
return (T) codecs.deserializeMemoized(codecs.serializeMemoized(original));
}

public static <T> T roundTripMemoized(T original, ObjectCodec<?>... codecs)
throws IOException, SerializationException {
ObjectCodecRegistry.Builder builder = AutoRegistry.get().getBuilder();
for (ObjectCodec<?> codec : codecs) {
builder.add(codec);
}
return roundTripMemoized(original, builder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,16 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.devtools.build.lib.skyframe.serialization.AutoRegistry;
import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecRegistry;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecs;
import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import net.starlark.java.eval.Module;

/** Helpers for serialization tests. */
public class TestUtils {

private TestUtils() {}

/** Serialize a value to a new byte array. */
public static <T> byte[] toBytes(SerializationContext context, ObjectCodec<T> codec, T value)
throws IOException, SerializationException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
CodedOutputStream codedOut = CodedOutputStream.newInstance(bytes);
codec.serialize(context, value, codedOut);
codedOut.flush();
return bytes.toByteArray();
}

public static <T> ByteString toBytes(SerializationContext serializationContext, T value)
throws IOException, SerializationException {
ByteString.Output output = ByteString.newOutput();
CodedOutputStream codedOut = CodedOutputStream.newInstance(output);
serializationContext.serialize(value, codedOut);
codedOut.flush();
return output.toByteString();
}

public static Object fromBytes(DeserializationContext deserializationContext, ByteString bytes)
throws IOException, SerializationException {
return deserializationContext.deserialize(bytes.newCodedInput());
}

/** Deserialize a value from a byte array. */
public static <T> T fromBytes(DeserializationContext context, ObjectCodec<T> codec, byte[] bytes)
throws SerializationException, IOException {
return codec.deserialize(context, CodedInputStream.newInstance(bytes));
}

public static <T> T roundTrip(T value, ObjectCodecRegistry registry)
throws IOException, SerializationException {
return roundTrip(value, new ObjectCodecs(registry));
}

public static <T> T roundTrip(T value, ImmutableClassToInstanceMap<Object> dependencies)
throws IOException, SerializationException {
return roundTrip(value, new ObjectCodecs(dependencies));
}

public static <T> T roundTrip(T value) throws IOException, SerializationException {
return roundTrip(value, new ObjectCodecs());
}

private static <T> T roundTrip(T value, ObjectCodecs codecs) throws SerializationException {
@SuppressWarnings("unchecked")
T result = (T) codecs.deserialize(codecs.serialize(value));
return result;
}

/**
* Asserts that two {@link Module}s have the same structure. Needed because {@link Module} doesn't
* override {@link Object#equals}.
Expand All @@ -98,28 +38,6 @@ public static void assertModulesEqual(Module module1, Module module2) {
.inOrder();
}

public static ByteString toBytesMemoized(Object original, ObjectCodecRegistry registry)
throws IOException, SerializationException {
return new ObjectCodecs(registry).serializeMemoized(original);
}

public static Object fromBytesMemoized(ByteString bytes, ObjectCodecRegistry registry)
throws SerializationException {
return new ObjectCodecs(registry).deserializeMemoized(bytes);
}

@SuppressWarnings("unchecked")
public static <T> T roundTripMemoized(T original, ObjectCodecRegistry registry)
throws IOException, SerializationException {
ObjectCodecs codecs = new ObjectCodecs(registry);
return (T) codecs.deserializeMemoized(codecs.serializeMemoized(original));
}

public static <T> T roundTripMemoized(T original, ObjectCodec<?>... codecs)
throws IOException, SerializationException {
return roundTripMemoized(original, getBuilderWithAdditionalCodecs(codecs).build());
}

public static ObjectCodecRegistry.Builder getBuilderWithAdditionalCodecs(
ObjectCodec<?>... codecs) {
ObjectCodecRegistry.Builder builder = AutoRegistry.get().getBuilder();
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/packages/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/skyframe:tests_for_target_pattern_value",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils:round-tripping",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/util:filetype",
"//src/main/java/com/google/devtools/build/lib/vfs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.google.devtools.build.lib.events.EventKind;
import com.google.devtools.build.lib.packages.util.PackageLoadingTestCase;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.devtools.build.lib.skyframe.serialization.testutils.TestUtils;
import com.google.devtools.build.lib.skyframe.serialization.testutils.RoundTripping;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -274,7 +274,7 @@ public void testReduceForSerialization() throws Exception {
}

private TargetData roundTrip(Target target) throws SerializationException, IOException {
return TestUtils.roundTrip(
return RoundTripping.roundTrip(
target.reduceForSerialization(),
ImmutableClassToInstanceMap.of(
RuleClassProvider.class, skyframeExecutor.getRuleClassProviderForTesting()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ java_test(
deps = [
"//src/main/java/com/google/devtools/build/lib/packages/semantics",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils:round-tripping",
"//src/main/java/com/google/devtools/common/options",
"//src/main/java/net/starlark/java/eval",
"//third_party:guava",
"//third_party:junit4",
"//third_party:truth",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.devtools.build.lib.skyframe.serialization.DynamicCodec;
import com.google.devtools.build.lib.skyframe.serialization.ImmutableDeserializationContext;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecs;
import com.google.devtools.build.lib.skyframe.serialization.testutils.TestUtils;
import com.google.devtools.build.lib.skyframe.serialization.testutils.RoundTripping;
import com.google.devtools.common.options.Options;
import com.google.devtools.common.options.OptionsParser;
import java.util.Arrays;
Expand Down Expand Up @@ -84,10 +84,10 @@ public void serializationRoundTrip() throws Exception {
StarlarkSemantics semantics = buildRandomSemantics(new Random(i));
StarlarkSemantics deserialized =
(StarlarkSemantics)
TestUtils.fromBytes(
RoundTripping.fromBytes(
new ImmutableDeserializationContext(),
codec,
TestUtils.toBytes(
RoundTripping.toBytes(
new ObjectCodecs().getSerializationContextForTesting(), codec, semantics));
assertThat(deserialized).isEqualTo(semantics);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/rules/cpp",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils:round-tripping",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/net/starlark/java/eval",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import com.google.devtools.build.lib.rules.cpp.CcToolchainVariables.VariableValueBuilder;
import com.google.devtools.build.lib.skyframe.serialization.AutoRegistry;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecs;
import com.google.devtools.build.lib.skyframe.serialization.testutils.RoundTripping;
import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester;
import com.google.devtools.build.lib.skyframe.serialization.testutils.TestUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
Expand Down Expand Up @@ -1257,7 +1257,7 @@ public void testConfiguration() throws Exception {
.getCommandLine(CppActionNames.CPP_COMPILE, createVariables("v", "1")))
.containsExactly("-f", "1");

CcToolchainFeatures deserialized = TestUtils.roundTrip(features);
CcToolchainFeatures deserialized = RoundTripping.roundTrip(features);
assertThat(getEnabledFeatures(deserialized, "b")).containsExactly("a", "b");
assertThat(
features
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/skyframe/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant-annotation",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils:depsutils",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils:round-tripping",
"//src/main/java/com/google/devtools/build/lib/skyframe/toolchains:no_matching_platform_data",
"//src/main/java/com/google/devtools/build/lib/skyframe/toolchains:platform_lookup_util",
"//src/main/java/com/google/devtools/build/lib/skyframe/toolchains:toolchain_context_key",
Expand Down
Loading

0 comments on commit 5239fe6

Please sign in to comment.