Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.skyframe; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Iterables; |
| 20 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 21 | import com.google.devtools.build.lib.collect.nestedset.Order; |
| 22 | import com.google.devtools.build.skyframe.SkyFunctionException.ReifiedSkyFunctionException; |
janakr | bfdad90 | 2017-05-03 21:38:28 +0200 | [diff] [blame] | 23 | import java.io.IOException; |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 28 | /** Tests for the non-trivial creation logic of {@link ErrorInfo}. */ |
| 29 | @RunWith(JUnit4.class) |
| 30 | public class ErrorInfoTest { |
| 31 | |
| 32 | /** Dummy SkyFunctionException implementation for the sake of testing. */ |
| 33 | private static class DummySkyFunctionException extends SkyFunctionException { |
| 34 | private final boolean isCatastrophic; |
| 35 | |
| 36 | public DummySkyFunctionException(Exception cause, boolean isTransient, |
| 37 | boolean isCatastrophic) { |
| 38 | super(cause, isTransient ? Transience.TRANSIENT : Transience.PERSISTENT); |
| 39 | this.isCatastrophic = isCatastrophic; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public boolean isCatastrophic() { |
| 44 | return isCatastrophic; |
| 45 | } |
| 46 | } |
| 47 | |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 48 | private void runTestFromException(boolean isDirectlyTransient, boolean isTransitivelyTransient) { |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 49 | Exception exception = new IOException("ehhhhh"); |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 50 | SkyKey causeOfException = GraphTester.toSkyKey("CAUSE, 1234"); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 51 | DummySkyFunctionException dummyException = |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 52 | new DummySkyFunctionException(exception, isDirectlyTransient, /*isCatastrophic=*/ false); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 53 | |
| 54 | ErrorInfo errorInfo = ErrorInfo.fromException( |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 55 | new ReifiedSkyFunctionException(dummyException, causeOfException), |
| 56 | isTransitivelyTransient); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 57 | |
| 58 | assertThat(errorInfo.getRootCauses()).containsExactly(causeOfException); |
| 59 | assertThat(errorInfo.getException()).isSameAs(exception); |
| 60 | assertThat(errorInfo.getRootCauseOfException()).isSameAs(causeOfException); |
| 61 | assertThat(errorInfo.getCycleInfo()).isEmpty(); |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 62 | assertThat(errorInfo.isDirectlyTransient()).isEqualTo(isDirectlyTransient); |
| 63 | assertThat(errorInfo.isTransitivelyTransient()).isEqualTo( |
| 64 | isDirectlyTransient || isTransitivelyTransient); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 65 | assertThat(errorInfo.isCatastrophic()).isFalse(); |
| 66 | } |
| 67 | |
| 68 | @Test |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 69 | public void testFromException_NonTransient() { |
| 70 | runTestFromException(/*isDirectlyTransient=*/ false, /*isTransitivelyTransient= */ false); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testFromException_DirectlyTransient() { |
| 75 | runTestFromException(/*isDirectlyTransient=*/ true, /*isTransitivelyTransient= */ false); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testFromException_TransitivelyTransient() { |
| 80 | runTestFromException(/*isDirectlyTransient=*/ false, /*isTransitivelyTransient= */ true); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testFromException_DirectlyAndTransitivelyTransient() { |
| 85 | runTestFromException(/*isDirectlyTransient=*/ true, /*isTransitivelyTransient= */ true); |
| 86 | } |
| 87 | |
| 88 | @Test |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 89 | public void testFromCycle() { |
Janak Ramakrishnan | f745e99 | 2016-03-03 08:08:50 +0000 | [diff] [blame] | 90 | CycleInfo cycle = |
| 91 | new CycleInfo( |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 92 | ImmutableList.of(GraphTester.toSkyKey("PATH, 1234")), |
| 93 | ImmutableList.of(GraphTester.toSkyKey("CYCLE, 4321"))); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 94 | |
| 95 | ErrorInfo errorInfo = ErrorInfo.fromCycle(cycle); |
| 96 | |
| 97 | assertThat(errorInfo.getRootCauses()).isEmpty(); |
| 98 | assertThat(errorInfo.getException()).isNull(); |
| 99 | assertThat(errorInfo.getRootCauseOfException()).isNull(); |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 100 | assertThat(errorInfo.isTransitivelyTransient()).isFalse(); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 101 | assertThat(errorInfo.isCatastrophic()).isFalse(); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testFromChildErrors() { |
Janak Ramakrishnan | f745e99 | 2016-03-03 08:08:50 +0000 | [diff] [blame] | 106 | CycleInfo cycle = |
| 107 | new CycleInfo( |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 108 | ImmutableList.of(GraphTester.toSkyKey("PATH, 1234")), |
| 109 | ImmutableList.of(GraphTester.toSkyKey("CYCLE, 4321"))); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 110 | ErrorInfo cycleErrorInfo = ErrorInfo.fromCycle(cycle); |
| 111 | |
| 112 | Exception exception1 = new IOException("ehhhhh"); |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 113 | SkyKey causeOfException1 = GraphTester.toSkyKey("CAUSE1, 1234"); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 114 | DummySkyFunctionException dummyException1 = |
| 115 | new DummySkyFunctionException(exception1, /*isTransient=*/ true, /*isCatastrophic=*/ false); |
| 116 | ErrorInfo exceptionErrorInfo1 = ErrorInfo.fromException( |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 117 | new ReifiedSkyFunctionException(dummyException1, causeOfException1), |
| 118 | /*isTransitivelyTransient=*/ false); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 119 | |
| 120 | // N.B this ErrorInfo will be catastrophic. |
| 121 | Exception exception2 = new IOException("blahhhhh"); |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 122 | SkyKey causeOfException2 = GraphTester.toSkyKey("CAUSE2, 5678"); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 123 | DummySkyFunctionException dummyException2 = |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 124 | new DummySkyFunctionException(exception2, /*isTransient=*/ false, /*isCatastrophic=*/ true); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 125 | ErrorInfo exceptionErrorInfo2 = ErrorInfo.fromException( |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 126 | new ReifiedSkyFunctionException(dummyException2, causeOfException2), |
| 127 | /*isTransitivelyTransient=*/ false); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 128 | |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 129 | SkyKey currentKey = GraphTester.toSkyKey("CURRENT, 9876"); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 130 | |
| 131 | ErrorInfo errorInfo = ErrorInfo.fromChildErrors( |
| 132 | currentKey, ImmutableList.of(cycleErrorInfo, exceptionErrorInfo1, exceptionErrorInfo2)); |
| 133 | |
| 134 | assertThat(errorInfo.getRootCauses()).containsExactly(causeOfException1, causeOfException2); |
| 135 | |
| 136 | // For simplicity we test the current implementation detail that we choose the first non-null |
| 137 | // (exception, cause) pair that we encounter. This isn't necessarily a requirement of the |
| 138 | // interface, but it makes the test convenient and is a way to document the current behavior. |
| 139 | assertThat(errorInfo.getException()).isSameAs(exception1); |
| 140 | assertThat(errorInfo.getRootCauseOfException()).isSameAs(causeOfException1); |
| 141 | |
| 142 | assertThat(errorInfo.getCycleInfo()).containsExactly( |
| 143 | new CycleInfo( |
| 144 | ImmutableList.of(currentKey, Iterables.getOnlyElement(cycle.getPathToCycle())), |
| 145 | cycle.getCycle())); |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 146 | assertThat(errorInfo.isTransitivelyTransient()).isTrue(); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 147 | assertThat(errorInfo.isCatastrophic()).isTrue(); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testCannotCreateErrorInfoWithoutExceptionOrCycle() { |
| 152 | try { |
| 153 | new ErrorInfo( |
| 154 | NestedSetBuilder.<SkyKey>emptySet(Order.COMPILE_ORDER), |
| 155 | /*exception=*/ null, |
| 156 | /*rootCauseOfException=*/ null, |
| 157 | ImmutableList.<CycleInfo>of(), |
| 158 | false, |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 159 | false, |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 160 | false); |
| 161 | } catch (IllegalStateException e) { |
| 162 | // Brittle, but confirms we failed for the right reason. |
| 163 | assertThat(e) |
diamondm | ad04da6 | 2019-03-19 09:54:50 -0700 | [diff] [blame] | 164 | .hasMessageThat() |
| 165 | .isEqualTo("At least one of exception and cycles must be non-null/empty, respectively"); |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testCannotCreateErrorInfoWithExceptionButNoRootCause() { |
| 171 | try { |
| 172 | new ErrorInfo( |
| 173 | NestedSetBuilder.<SkyKey>emptySet(Order.COMPILE_ORDER), |
| 174 | new IOException("foo"), |
| 175 | /*rootCauseOfException=*/ null, |
| 176 | ImmutableList.<CycleInfo>of(), |
| 177 | false, |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 178 | false, |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 179 | false); |
| 180 | } catch (IllegalStateException e) { |
| 181 | // Brittle, but confirms we failed for the right reason. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 182 | assertThat(e) |
| 183 | .hasMessageThat() |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 184 | .startsWith("exception and rootCauseOfException must both be null or non-null"); |
| 185 | } |
| 186 | } |
| 187 | } |