aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com
diff options
context:
space:
mode:
authorGravatar janakr <janakr@google.com>2018-03-10 17:10:34 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-10 17:12:35 -0800
commit2eb211464db3a9515665accde85c62ba2bf4e3a4 (patch)
treecbce5cde5feb8d0577519fe2556827e4e16796c4 /src/test/java/com
parent163b3925a701df925cda02a4db52028a898c0251 (diff)
Open-source ImmutableMapCodec and make it able to handle arbitrary keys/values without injecting the codecs. Also allow it to handle ImmutableSortedMap, since we were always silently degrading to ImmutableMap for objects that weren't declared as ImmutableSortedMap, and there's no good way to handle non-natural comparators. This will lead to runtime failures if a class actually needs an ImmutableSortedMap with a different comparator than the natural one.
This changes the semantics of ImmutableMap serialization. Previously, we went off the declared type. In the case of a declared ImmutableSortedMap, we ignored the comparator, potentially leading to incorrect serialization (new test added in AutoCodecProcessorTest that would have caught that). Moreover, declared ImmutableMaps were deserialized as ImmutableMaps even if they were actually ImmutableSortedMaps. Now, we preserve the ordering unconditionally, and preserve the type if possible. I think this is a better state to be in. This is needed to kill off MemoizingCodec, since MemoizingCodec has an ImmutableMapMemoizingCodec, which I want to get rid of in favor of this. PiperOrigin-RevId: 188619637
Diffstat (limited to 'src/test/java/com')
-rw-r--r--src/test/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableMapCodecTest.java61
-rw-r--r--src/test/java/com/google/devtools/build/lib/skyframe/serialization/OptionalCodecTest.java30
2 files changed, 91 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableMapCodecTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableMapCodecTest.java
new file mode 100644
index 0000000000..2791f0adcc
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableMapCodecTest.java
@@ -0,0 +1,61 @@
+// Copyright 2018 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;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSortedMap;
+import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester;
+import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester.VerificationFunction;
+import com.google.devtools.build.lib.skyframe.serialization.testutils.TestUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link ImmutableMapCodec}. */
+@RunWith(JUnit4.class)
+public class ImmutableMapCodecTest {
+ @Test
+ public void smoke() throws Exception {
+ new SerializationTester(
+ ImmutableMap.of(),
+ ImmutableMap.of("A", "//foo:A"),
+ ImmutableMap.of("B", "//foo:B"),
+ ImmutableSortedMap.of(),
+ ImmutableSortedMap.of("A", "//foo:A"),
+ ImmutableSortedMap.of("B", "//foo:B"),
+ ImmutableSortedMap.reverseOrder().put("a", "b").put("c", "d").build())
+ // Check for order.
+ .setVerificationFunction(
+ (VerificationFunction<ImmutableMap<?, ?>>)
+ (deserialized, subject) -> {
+ assertThat(deserialized).isEqualTo(subject);
+ assertThat(deserialized).containsExactlyEntriesIn(subject).inOrder();
+ })
+ .runTests();
+ }
+
+ @Test
+ public void unnaturallySortedMapComesBackUnsortedInCorrectOrder() throws Exception {
+ ImmutableMap<?, ?> deserialized =
+ TestUtils.roundTrip(
+ ImmutableSortedMap.reverseOrder().put("a", "b").put("c", "d").build(),
+ ImmutableMap.of());
+ assertThat(deserialized).isInstanceOf(ImmutableMap.class);
+ assertThat(deserialized).isNotInstanceOf(ImmutableSortedMap.class);
+ assertThat(deserialized).containsExactly("c", "d", "a", "b").inOrder();
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/OptionalCodecTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/OptionalCodecTest.java
new file mode 100644
index 0000000000..302a50314b
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/OptionalCodecTest.java
@@ -0,0 +1,30 @@
+// Copyright 2018 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;
+
+import com.google.common.base.Optional;
+import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link OptionalCodec}. */
+@RunWith(JUnit4.class)
+public class OptionalCodecTest {
+ @Test
+ public void smoke() throws Exception {
+ new SerializationTester(Optional.absent(), Optional.of("string")).runTests();
+ }
+}