shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 1 | // Copyright 2018 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.lib.skyframe; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | import static org.mockito.Mockito.when; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
| 21 | import com.google.devtools.build.lib.cmdline.Label; |
| 22 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 23 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 24 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
| 25 | import com.google.devtools.build.lib.packages.Package; |
| 26 | import com.google.devtools.build.lib.skyframe.TransitiveBaseTraversalFunction.TargetAndErrorIfAnyImpl; |
adonovan | c2fe3d8 | 2020-09-29 13:49:55 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils; |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 28 | import com.google.devtools.build.lib.util.GroupedList; |
| 29 | import com.google.devtools.build.lib.util.GroupedList.GroupedListHelper; |
adonovan | c2fe3d8 | 2020-09-29 13:49:55 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.skyframe.EvaluationResult; |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 31 | import com.google.devtools.build.skyframe.SkyFunction; |
| 32 | import com.google.devtools.build.skyframe.SkyKey; |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.skyframe.ValueOrException2; |
| 34 | import com.google.devtools.build.skyframe.ValueOrUntypedException; |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 35 | import java.util.concurrent.atomic.AtomicBoolean; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.junit.runners.JUnit4; |
Googler | a670577 | 2019-05-28 09:39:18 -0700 | [diff] [blame] | 39 | import org.mockito.ArgumentMatchers; |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 40 | import org.mockito.Mockito; |
| 41 | |
| 42 | /** Test for {@link TransitiveTraversalFunction}. */ |
| 43 | @RunWith(JUnit4.class) |
| 44 | public class TransitiveTraversalFunctionTest extends BuildViewTestCase { |
| 45 | |
| 46 | @Test |
| 47 | public void noRepeatedLabelVisitationForTransitiveTraversalFunction() throws Exception { |
| 48 | // Create a basic package with a target //foo:foo. |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 49 | Label label = Label.parseAbsolute("//foo:foo", ImmutableMap.of()); |
adonovan | c2fe3d8 | 2020-09-29 13:49:55 -0700 | [diff] [blame] | 50 | scratch.file("foo/BUILD", "sh_library(name = '" + label.getName() + "')"); |
| 51 | Package pkg = loadPackage(label.getPackageIdentifier()); |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 52 | TargetAndErrorIfAnyImpl targetAndErrorIfAny = |
| 53 | new TargetAndErrorIfAnyImpl( |
| 54 | /*packageLoadedSuccessfully=*/ true, |
| 55 | /*errorLoadingTarget=*/ null, |
| 56 | pkg.getTarget(label.getName())); |
| 57 | TransitiveTraversalFunction function = |
| 58 | new TransitiveTraversalFunction() { |
| 59 | @Override |
ulfjack | 731451f | 2019-05-09 04:30:35 -0700 | [diff] [blame] | 60 | TargetAndErrorIfAny loadTarget(Environment env, Label label) { |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 61 | return targetAndErrorIfAny; |
| 62 | } |
| 63 | }; |
| 64 | // Create the GroupedList saying we had already requested two targets the last time we called |
| 65 | // #compute. |
| 66 | GroupedListHelper<SkyKey> helper = new GroupedListHelper<>(); |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 67 | SkyKey fakeDep1 = function.getKey(Label.parseAbsolute("//foo:bar", ImmutableMap.of())); |
| 68 | SkyKey fakeDep2 = function.getKey(Label.parseAbsolute("//foo:baz", ImmutableMap.of())); |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 69 | helper.add(PackageValue.key(label.getPackageIdentifier())); |
| 70 | helper.startGroup(); |
| 71 | // Note that these targets don't actually exist in the package we created initially. It doesn't |
| 72 | // matter for the purpose of this test, the original package was just to create some objects |
| 73 | // that we needed. |
| 74 | helper.add(fakeDep1); |
| 75 | helper.add(fakeDep2); |
| 76 | helper.endGroup(); |
| 77 | GroupedList<SkyKey> groupedList = new GroupedList<>(); |
| 78 | groupedList.append(helper); |
| 79 | AtomicBoolean wasOptimizationUsed = new AtomicBoolean(false); |
| 80 | SkyFunction.Environment mockEnv = Mockito.mock(SkyFunction.Environment.class); |
| 81 | when(mockEnv.getTemporaryDirectDeps()).thenReturn(groupedList); |
| 82 | when(mockEnv.getValuesOrThrow( |
ulfjack | 731451f | 2019-05-09 04:30:35 -0700 | [diff] [blame] | 83 | groupedList.get(1), NoSuchPackageException.class, NoSuchTargetException.class)) |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 84 | .thenAnswer( |
| 85 | (invocationOnMock) -> { |
| 86 | wasOptimizationUsed.set(true); |
| 87 | // It doesn't matter what this map is, we'll return false in the valuesMissing() call. |
| 88 | return ImmutableMap.of(); |
| 89 | }); |
| 90 | when(mockEnv.valuesMissing()).thenReturn(true); |
| 91 | |
| 92 | // Run the compute function and check that we returned null. |
| 93 | assertThat(function.compute(function.getKey(label), mockEnv)).isNull(); |
| 94 | |
| 95 | // Verify that the mock was called with the arguments we expected. |
| 96 | assertThat(wasOptimizationUsed.get()).isTrue(); |
| 97 | } |
| 98 | |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 99 | @Test |
| 100 | public void multipleErrorsForTransitiveTraversalFunction() throws Exception { |
| 101 | Label label = Label.parseAbsolute("//foo:foo", ImmutableMap.of()); |
adonovan | c2fe3d8 | 2020-09-29 13:49:55 -0700 | [diff] [blame] | 102 | scratch.file( |
| 103 | "foo/BUILD", "sh_library(name = '" + label.getName() + "', deps = [':bar', ':baz'])"); |
| 104 | Package pkg = loadPackage(label.getPackageIdentifier()); |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 105 | TargetAndErrorIfAnyImpl targetAndErrorIfAny = |
| 106 | new TargetAndErrorIfAnyImpl( |
| 107 | /*packageLoadedSuccessfully=*/ true, |
| 108 | /*errorLoadingTarget=*/ null, |
| 109 | pkg.getTarget(label.getName())); |
| 110 | TransitiveTraversalFunction function = |
| 111 | new TransitiveTraversalFunction() { |
| 112 | @Override |
ulfjack | 731451f | 2019-05-09 04:30:35 -0700 | [diff] [blame] | 113 | TargetAndErrorIfAny loadTarget(Environment env, Label label) { |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 114 | return targetAndErrorIfAny; |
| 115 | } |
| 116 | }; |
| 117 | SkyKey dep1 = function.getKey(Label.parseAbsolute("//foo:bar", ImmutableMap.of())); |
| 118 | SkyKey dep2 = function.getKey(Label.parseAbsolute("//foo:baz", ImmutableMap.of())); |
| 119 | ImmutableMap<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> |
| 120 | returnedDeps = |
| 121 | ImmutableMap.of(dep1, makeException("bad bar"), dep2, makeException("bad baz")); |
| 122 | SkyFunction.Environment mockEnv = Mockito.mock(SkyFunction.Environment.class); |
| 123 | // Try two evaluations, with the environment reversing the order of the map it returns. |
| 124 | when(mockEnv.getValuesOrThrow( |
Googler | a670577 | 2019-05-28 09:39:18 -0700 | [diff] [blame] | 125 | ArgumentMatchers.any(), |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 126 | Mockito.eq(NoSuchPackageException.class), |
| 127 | Mockito.eq(NoSuchTargetException.class))) |
| 128 | .thenReturn(returnedDeps); |
| 129 | when(mockEnv.valuesMissing()).thenReturn(false); |
| 130 | |
| 131 | assertThat( |
| 132 | ((TransitiveTraversalValue) function.compute(function.getKey(label), mockEnv)) |
| 133 | .getErrorMessage()) |
| 134 | .isEqualTo("bad bar"); |
| 135 | ImmutableMap<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> |
| 136 | reversedDeps = |
| 137 | ImmutableMap.of(dep2, makeException("bad baz"), dep1, makeException("bad bar")); |
| 138 | when(mockEnv.getValuesOrThrow( |
Googler | a670577 | 2019-05-28 09:39:18 -0700 | [diff] [blame] | 139 | ArgumentMatchers.any(), |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 140 | Mockito.eq(NoSuchPackageException.class), |
| 141 | Mockito.eq(NoSuchTargetException.class))) |
| 142 | .thenReturn(reversedDeps); |
| 143 | assertThat( |
| 144 | ((TransitiveTraversalValue) function.compute(function.getKey(label), mockEnv)) |
| 145 | .getErrorMessage()) |
| 146 | .isEqualTo("bad bar"); |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void selfErrorWins() throws Exception { |
| 151 | Label label = Label.parseAbsolute("//foo:foo", ImmutableMap.of()); |
adonovan | c2fe3d8 | 2020-09-29 13:49:55 -0700 | [diff] [blame] | 152 | scratch.file( |
| 153 | "foo/BUILD", "sh_library(name = '" + label.getName() + "', deps = [':bar', ':baz'])"); |
| 154 | Package pkg = loadPackage(label.getPackageIdentifier()); |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 155 | TargetAndErrorIfAnyImpl targetAndErrorIfAny = |
| 156 | new TargetAndErrorIfAnyImpl( |
| 157 | /*packageLoadedSuccessfully=*/ true, |
| 158 | /*errorLoadingTarget=*/ new NoSuchTargetException("self error is long and last"), |
| 159 | pkg.getTarget(label.getName())); |
| 160 | TransitiveTraversalFunction function = |
| 161 | new TransitiveTraversalFunction() { |
| 162 | @Override |
ulfjack | 731451f | 2019-05-09 04:30:35 -0700 | [diff] [blame] | 163 | TargetAndErrorIfAny loadTarget(Environment env, Label label) { |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 164 | return targetAndErrorIfAny; |
| 165 | } |
| 166 | }; |
| 167 | SkyKey dep = function.getKey(Label.parseAbsolute("//foo:bar", ImmutableMap.of())); |
| 168 | SkyFunction.Environment mockEnv = Mockito.mock(SkyFunction.Environment.class); |
| 169 | when(mockEnv.getValuesOrThrow( |
Googler | a670577 | 2019-05-28 09:39:18 -0700 | [diff] [blame] | 170 | ArgumentMatchers.any(), |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 171 | Mockito.eq(NoSuchPackageException.class), |
| 172 | Mockito.eq(NoSuchTargetException.class))) |
| 173 | .thenReturn(ImmutableMap.of(dep, makeException("bad bar"))); |
| 174 | when(mockEnv.valuesMissing()).thenReturn(false); |
| 175 | |
ulfjack | 731451f | 2019-05-09 04:30:35 -0700 | [diff] [blame] | 176 | TransitiveTraversalValue transitiveTraversalValue = |
| 177 | (TransitiveTraversalValue) function.compute(function.getKey(label), mockEnv); |
| 178 | assertThat(transitiveTraversalValue.getErrorMessage()).isEqualTo("self error is long and last"); |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | private static ValueOrException2<NoSuchPackageException, NoSuchTargetException> makeException( |
| 182 | String errorMessage) { |
| 183 | ValueOrUntypedException exn = |
| 184 | ValueOrUntypedException.ofExn(new NoSuchTargetException(errorMessage)); |
| 185 | return ValueOrException2.fromUntypedException( |
| 186 | exn, NoSuchPackageException.class, NoSuchTargetException.class); |
| 187 | } |
| 188 | |
adonovan | c2fe3d8 | 2020-09-29 13:49:55 -0700 | [diff] [blame] | 189 | /* Invokes the loading phase, using Skyframe. */ |
| 190 | private Package loadPackage(PackageIdentifier pkgid) |
| 191 | throws InterruptedException, NoSuchPackageException { |
| 192 | SkyKey key = PackageValue.key(pkgid); |
| 193 | EvaluationResult<PackageValue> result = |
| 194 | SkyframeExecutorTestUtils.evaluate( |
| 195 | getSkyframeExecutor(), key, /*keepGoing=*/ false, reporter); |
| 196 | if (result.hasError()) { |
| 197 | throw (NoSuchPackageException) result.getError(key).getException(); |
| 198 | } |
| 199 | return result.get(key).getPackage(); |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 200 | } |
| 201 | } |