shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [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; |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 17 | import static org.mockito.Mockito.mock; |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.Label; |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 22 | import com.google.devtools.build.skyframe.SkyKey; |
| 23 | import java.util.ArrayList; |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 24 | import java.util.HashMap; |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 25 | import java.util.List; |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 26 | import java.util.Map; |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.junit.runners.JUnit4; |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 30 | |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 31 | /** Tests for {@link CachedBzlLoadData}. */ |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 32 | @RunWith(JUnit4.class) |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 33 | public class CachedBzlLoadDataTest { |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 34 | @Test |
| 35 | public void testDepsAreNotVisitedMultipleTimesForDiamondDependencies() throws Exception { |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 36 | // Graph structure of BzlLoadValues: |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 37 | // |
| 38 | // p |
| 39 | // / \ |
| 40 | // c1 c2 |
| 41 | // \ / |
| 42 | // gc |
| 43 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 44 | BzlLoadValue dummyValue = mock(BzlLoadValue.class); |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 45 | CachedBzlLoadDataBuilderFactory cachedBzlLoadDataBuilderFactory = |
| 46 | new CachedBzlLoadDataBuilderFactory(); |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 47 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 48 | BzlLoadValue.Key gcKey = createStarlarkKey("//gc"); |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 49 | SkyKey gcKey1 = createKey("gc key1"); |
| 50 | SkyKey gcKey2 = createKey("gc key2"); |
| 51 | SkyKey gcKey3 = createKey("gc key3"); |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 52 | CachedBzlLoadData gc = |
| 53 | cachedBzlLoadDataBuilderFactory |
| 54 | .newCachedBzlLoadDataBuilder() |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 55 | .addDep(gcKey1) |
| 56 | .addDeps(ImmutableList.of(gcKey2, gcKey3)) |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 57 | .setKey(gcKey) |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 58 | .setValue(dummyValue) |
| 59 | .build(); |
| 60 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 61 | BzlLoadValue.Key c1Key = createStarlarkKey("//c1"); |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 62 | SkyKey c1Key1 = createKey("c1 key1"); |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 63 | CachedBzlLoadData c1 = |
| 64 | cachedBzlLoadDataBuilderFactory |
| 65 | .newCachedBzlLoadDataBuilder() |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 66 | .addDep(c1Key1) |
| 67 | .addTransitiveDeps(gc) |
| 68 | .setValue(dummyValue) |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 69 | .setKey(c1Key) |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 70 | .build(); |
| 71 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 72 | BzlLoadValue.Key c2Key = createStarlarkKey("//c2"); |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 73 | SkyKey c2Key1 = createKey("c2 key1"); |
| 74 | SkyKey c2Key2 = createKey("c2 key2"); |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 75 | CachedBzlLoadData c2 = |
| 76 | cachedBzlLoadDataBuilderFactory |
| 77 | .newCachedBzlLoadDataBuilder() |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 78 | .addDeps(ImmutableList.of(c2Key1, c2Key2)) |
| 79 | .addTransitiveDeps(gc) |
| 80 | .setValue(dummyValue) |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 81 | .setKey(c2Key) |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 82 | .build(); |
| 83 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 84 | BzlLoadValue.Key pKey = createStarlarkKey("//p"); |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 85 | SkyKey pKey1 = createKey("p key1"); |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 86 | CachedBzlLoadData p = |
| 87 | cachedBzlLoadDataBuilderFactory |
| 88 | .newCachedBzlLoadDataBuilder() |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 89 | .addDep(pKey1) |
| 90 | .addTransitiveDeps(c1) |
| 91 | .addTransitiveDeps(c2) |
| 92 | .setValue(dummyValue) |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 93 | .setKey(pKey) |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 94 | .build(); |
| 95 | |
| 96 | List<Iterable<SkyKey>> registeredDeps = new ArrayList<>(); |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 97 | Map<BzlLoadValue.Key, CachedBzlLoadData> visitedBzls = new HashMap<>(); |
| 98 | p.traverse(registeredDeps::add, visitedBzls); |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 99 | |
| 100 | assertThat(registeredDeps) |
| 101 | .containsExactly( |
| 102 | ImmutableList.of(pKey1), |
| 103 | ImmutableList.of(c1Key1), |
| 104 | ImmutableList.of(gcKey1), |
| 105 | ImmutableList.of(gcKey2, gcKey3), |
| 106 | ImmutableList.of(c2Key1, c2Key2)) |
| 107 | .inOrder(); |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 108 | |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 109 | assertThat(visitedBzls).containsExactly(pKey, p, c1Key, c1, c2Key, c2, gcKey, gc); |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private static SkyKey createKey(String name) { |
| 113 | return new SkyKey() { |
| 114 | @Override |
| 115 | public SkyFunctionName functionName() { |
| 116 | return SkyFunctionName.createHermetic(name); |
| 117 | } |
| 118 | |
| 119 | // Override toString to assist debugging. |
| 120 | @Override |
| 121 | public String toString() { |
| 122 | return name; |
| 123 | } |
| 124 | }; |
| 125 | } |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 126 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 127 | private static BzlLoadValue.Key createStarlarkKey(String name) { |
brandjon | b28a76b | 2020-05-26 12:53:22 -0700 | [diff] [blame] | 128 | return BzlLoadValue.keyForBuild(Label.parseAbsoluteUnchecked(name)); |
shreyax | 3126205 | 2019-08-13 13:40:06 -0700 | [diff] [blame] | 129 | } |
shreyax | bfe9743 | 2018-09-10 14:49:05 -0700 | [diff] [blame] | 130 | } |