aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/skyframe
diff options
context:
space:
mode:
authorGravatar Michajlo Matijkiw <michajlo@google.com>2015-09-28 22:13:27 +0000
committerGravatar Florian Weikert <fwe@google.com>2015-09-30 09:32:22 +0000
commitaa05828b76e9f715aa932fea266aaa58fdf8091e (patch)
treec69dcc8ea6a913d6efc3c5002a95cc30de5e9443 /src/test/java/com/google/devtools/build/skyframe
parent1c95806886fc980a011e121352b1fb7c43e9d3cf (diff)
Refactor ErrorInfo creation to share single constructor
Single constructor allows us to enforce/document high-level constraints in a single place. Move previous constructors to static methods which do their custom transformations but ultimately funnel their final calculated fields through the common constructor. Also added some tests to demonstrate expected behavior of static methods. -- MOS_MIGRATED_REVID=104142909
Diffstat (limited to 'src/test/java/com/google/devtools/build/skyframe')
-rw-r--r--src/test/java/com/google/devtools/build/skyframe/ErrorInfoTest.java160
-rw-r--r--src/test/java/com/google/devtools/build/skyframe/InMemoryNodeEntryTest.java10
2 files changed, 165 insertions, 5 deletions
diff --git a/src/test/java/com/google/devtools/build/skyframe/ErrorInfoTest.java b/src/test/java/com/google/devtools/build/skyframe/ErrorInfoTest.java
new file mode 100644
index 0000000000..2a5db2d2be
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/skyframe/ErrorInfoTest.java
@@ -0,0 +1,160 @@
+// Copyright 2014 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.skyframe;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
+import com.google.devtools.build.lib.collect.nestedset.Order;
+import com.google.devtools.build.skyframe.SkyFunctionException.ReifiedSkyFunctionException;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.io.IOException;
+
+/** Tests for the non-trivial creation logic of {@link ErrorInfo}. */
+@RunWith(JUnit4.class)
+public class ErrorInfoTest {
+
+ /** Dummy SkyFunctionException implementation for the sake of testing. */
+ private static class DummySkyFunctionException extends SkyFunctionException {
+ private final boolean isCatastrophic;
+
+ public DummySkyFunctionException(Exception cause, boolean isTransient,
+ boolean isCatastrophic) {
+ super(cause, isTransient ? Transience.TRANSIENT : Transience.PERSISTENT);
+ this.isCatastrophic = isCatastrophic;
+ }
+
+ @Override
+ public boolean isCatastrophic() {
+ return isCatastrophic;
+ }
+ }
+
+ @Test
+ public void testFromException() {
+ Exception exception = new IOException("ehhhhh");
+ SkyKey causeOfException = new SkyKey(SkyFunctionName.create("CAUSE"), 1234);
+ DummySkyFunctionException dummyException =
+ new DummySkyFunctionException(exception, /*isTransient=*/ true, /*isCatastrophic=*/ false);
+
+ ErrorInfo errorInfo = ErrorInfo.fromException(
+ new ReifiedSkyFunctionException(dummyException, causeOfException));
+
+ assertThat(errorInfo.getRootCauses()).containsExactly(causeOfException);
+ assertThat(errorInfo.getException()).isSameAs(exception);
+ assertThat(errorInfo.getRootCauseOfException()).isSameAs(causeOfException);
+ assertThat(errorInfo.getCycleInfo()).isEmpty();
+ assertThat(errorInfo.isTransient()).isTrue();
+ assertThat(errorInfo.isCatastrophic()).isFalse();
+ }
+
+ @Test
+ public void testFromCycle() {
+ CycleInfo cycle = new CycleInfo(
+ ImmutableList.of(new SkyKey(SkyFunctionName.create("PATH"), 1234)),
+ ImmutableList.of(new SkyKey(SkyFunctionName.create("CYCLE"), 4321)));
+
+ ErrorInfo errorInfo = ErrorInfo.fromCycle(cycle);
+
+ assertThat(errorInfo.getRootCauses()).isEmpty();
+ assertThat(errorInfo.getException()).isNull();
+ assertThat(errorInfo.getRootCauseOfException()).isNull();
+ assertThat(errorInfo.isTransient()).isFalse();
+ assertThat(errorInfo.isCatastrophic()).isFalse();
+ }
+
+ @Test
+ public void testFromChildErrors() {
+ CycleInfo cycle = new CycleInfo(
+ ImmutableList.of(new SkyKey(SkyFunctionName.create("PATH"), 1234)),
+ ImmutableList.of(new SkyKey(SkyFunctionName.create("CYCLE"), 4321)));
+ ErrorInfo cycleErrorInfo = ErrorInfo.fromCycle(cycle);
+
+ Exception exception1 = new IOException("ehhhhh");
+ SkyKey causeOfException1 = new SkyKey(SkyFunctionName.create("CAUSE1"), 1234);
+ DummySkyFunctionException dummyException1 =
+ new DummySkyFunctionException(exception1, /*isTransient=*/ true, /*isCatastrophic=*/ false);
+ ErrorInfo exceptionErrorInfo1 = ErrorInfo.fromException(
+ new ReifiedSkyFunctionException(dummyException1, causeOfException1));
+
+ // N.B this ErrorInfo will be catastrophic.
+ Exception exception2 = new IOException("blahhhhh");
+ SkyKey causeOfException2 = new SkyKey(SkyFunctionName.create("CAUSE2"), 5678);
+ DummySkyFunctionException dummyException2 =
+ new DummySkyFunctionException(exception2, /*isTransient=*/ true, /*isCatastrophic=*/ true);
+ ErrorInfo exceptionErrorInfo2 = ErrorInfo.fromException(
+ new ReifiedSkyFunctionException(dummyException2, causeOfException2));
+
+ SkyKey currentKey = new SkyKey(SkyFunctionName.create("CURRENT"), 9876);
+
+ ErrorInfo errorInfo = ErrorInfo.fromChildErrors(
+ currentKey, ImmutableList.of(cycleErrorInfo, exceptionErrorInfo1, exceptionErrorInfo2));
+
+ assertThat(errorInfo.getRootCauses()).containsExactly(causeOfException1, causeOfException2);
+
+ // For simplicity we test the current implementation detail that we choose the first non-null
+ // (exception, cause) pair that we encounter. This isn't necessarily a requirement of the
+ // interface, but it makes the test convenient and is a way to document the current behavior.
+ assertThat(errorInfo.getException()).isSameAs(exception1);
+ assertThat(errorInfo.getRootCauseOfException()).isSameAs(causeOfException1);
+
+ assertThat(errorInfo.getCycleInfo()).containsExactly(
+ new CycleInfo(
+ ImmutableList.of(currentKey, Iterables.getOnlyElement(cycle.getPathToCycle())),
+ cycle.getCycle()));
+ assertThat(errorInfo.isTransient()).isFalse();
+ assertThat(errorInfo.isCatastrophic()).isTrue();
+ }
+
+ @Test
+ public void testCannotCreateErrorInfoWithoutExceptionOrCycle() {
+ try {
+ new ErrorInfo(
+ NestedSetBuilder.<SkyKey>emptySet(Order.COMPILE_ORDER),
+ /*exception=*/ null,
+ /*rootCauseOfException=*/ null,
+ ImmutableList.<CycleInfo>of(),
+ false,
+ false);
+ } catch (IllegalStateException e) {
+ // Brittle, but confirms we failed for the right reason.
+ assertThat(e)
+ .hasMessage("At least one of exception and cycles must be non-null/empty, respectively");
+ }
+ }
+
+ @Test
+ public void testCannotCreateErrorInfoWithExceptionButNoRootCause() {
+ try {
+ new ErrorInfo(
+ NestedSetBuilder.<SkyKey>emptySet(Order.COMPILE_ORDER),
+ new IOException("foo"),
+ /*rootCauseOfException=*/ null,
+ ImmutableList.<CycleInfo>of(),
+ false,
+ false);
+ } catch (IllegalStateException e) {
+ // Brittle, but confirms we failed for the right reason.
+ assertThat(e.getMessage())
+ .startsWith("exception and rootCauseOfException must both be null or non-null");
+ }
+ }
+}
+
diff --git a/src/test/java/com/google/devtools/build/skyframe/InMemoryNodeEntryTest.java b/src/test/java/com/google/devtools/build/skyframe/InMemoryNodeEntryTest.java
index ffd6a2c137..de1224a299 100644
--- a/src/test/java/com/google/devtools/build/skyframe/InMemoryNodeEntryTest.java
+++ b/src/test/java/com/google/devtools/build/skyframe/InMemoryNodeEntryTest.java
@@ -115,7 +115,7 @@ public class InMemoryNodeEntryTest {
ReifiedSkyFunctionException exception = new ReifiedSkyFunctionException(
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("cause"));
- ErrorInfo errorInfo = new ErrorInfo(exception);
+ ErrorInfo errorInfo = ErrorInfo.fromException(exception);
assertThat(setValue(entry, /*value=*/null, errorInfo, /*graphVersion=*/0L)).isEmpty();
assertTrue(entry.isDone());
assertNull(entry.getValue());
@@ -129,7 +129,7 @@ public class InMemoryNodeEntryTest {
ReifiedSkyFunctionException exception = new ReifiedSkyFunctionException(
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("cause"));
- ErrorInfo errorInfo = new ErrorInfo(exception);
+ ErrorInfo errorInfo = ErrorInfo.fromException(exception);
setValue(entry, new SkyValue() {}, errorInfo, /*graphVersion=*/0L);
assertTrue(entry.isDone());
assertEquals(errorInfo, entry.getErrorInfo());
@@ -447,7 +447,7 @@ public class InMemoryNodeEntryTest {
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("cause"));
assertThat(entry.markRebuildingAndGetAllRemainingDirtyDirectDeps()).isEmpty();
- setValue(entry, new IntegerValue(5), new ErrorInfo(exception),
+ setValue(entry, new IntegerValue(5), ErrorInfo.fromException(exception),
/*graphVersion=*/1L);
assertTrue(entry.isDone());
assertEquals("Version increments when setValue changes", new IntVersion(1), entry.getVersion());
@@ -463,7 +463,7 @@ public class InMemoryNodeEntryTest {
ReifiedSkyFunctionException exception = new ReifiedSkyFunctionException(
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("cause"));
- ErrorInfo errorInfo = new ErrorInfo(exception);
+ ErrorInfo errorInfo = ErrorInfo.fromException(exception);
setValue(entry, /*value=*/null, errorInfo, /*graphVersion=*/0L);
entry.markDirty(/*isChanged=*/false);
entry.addReverseDepAndCheckIfDone(null); // Restart evaluation.
@@ -522,7 +522,7 @@ public class InMemoryNodeEntryTest {
ReifiedSkyFunctionException exception = new ReifiedSkyFunctionException(
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("key"));
- setValue(entry, null, new ErrorInfo(exception), 0L);
+ setValue(entry, null, ErrorInfo.fromException(exception), 0L);
entry.markDirty(/*isChanged=*/false);
entry.addReverseDepAndCheckIfDone(null); // Restart evaluation.
assertEquals(NodeEntry.DirtyState.CHECK_DEPENDENCIES, entry.getDirtyState());