blob: e561c99b9005f32586dd2e9f225a478f0b1f59c1 [file] [log] [blame]
shreyaxbfe97432018-09-10 14:49:05 -07001// 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.
14package com.google.devtools.build.lib.skyframe;
15
16import static com.google.common.truth.Truth.assertThat;
shreyax31262052019-08-13 13:40:06 -070017import static org.mockito.Mockito.mock;
shreyaxbfe97432018-09-10 14:49:05 -070018
19import com.google.common.collect.ImmutableList;
shreyax31262052019-08-13 13:40:06 -070020import com.google.devtools.build.lib.cmdline.Label;
shreyaxbfe97432018-09-10 14:49:05 -070021import com.google.devtools.build.skyframe.SkyFunctionName;
22import com.google.devtools.build.skyframe.SkyKey;
23import java.util.ArrayList;
shreyax31262052019-08-13 13:40:06 -070024import java.util.HashMap;
shreyaxbfe97432018-09-10 14:49:05 -070025import java.util.List;
shreyax31262052019-08-13 13:40:06 -070026import java.util.Map;
shreyaxbfe97432018-09-10 14:49:05 -070027import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.JUnit4;
shreyaxbfe97432018-09-10 14:49:05 -070030
brandjon24912642020-05-27 11:28:45 -070031/** Tests for {@link CachedBzlLoadData}. */
shreyaxbfe97432018-09-10 14:49:05 -070032@RunWith(JUnit4.class)
brandjon24912642020-05-27 11:28:45 -070033public class CachedBzlLoadDataTest {
shreyaxbfe97432018-09-10 14:49:05 -070034 @Test
35 public void testDepsAreNotVisitedMultipleTimesForDiamondDependencies() throws Exception {
brandjon771a0292020-05-26 12:04:16 -070036 // Graph structure of BzlLoadValues:
shreyaxbfe97432018-09-10 14:49:05 -070037 //
38 // p
39 // / \
40 // c1 c2
41 // \ /
42 // gc
43
brandjon771a0292020-05-26 12:04:16 -070044 BzlLoadValue dummyValue = mock(BzlLoadValue.class);
brandjon24912642020-05-27 11:28:45 -070045 CachedBzlLoadDataBuilderFactory cachedBzlLoadDataBuilderFactory =
46 new CachedBzlLoadDataBuilderFactory();
shreyaxbfe97432018-09-10 14:49:05 -070047
brandjon771a0292020-05-26 12:04:16 -070048 BzlLoadValue.Key gcKey = createStarlarkKey("//gc");
shreyaxbfe97432018-09-10 14:49:05 -070049 SkyKey gcKey1 = createKey("gc key1");
50 SkyKey gcKey2 = createKey("gc key2");
51 SkyKey gcKey3 = createKey("gc key3");
brandjon24912642020-05-27 11:28:45 -070052 CachedBzlLoadData gc =
53 cachedBzlLoadDataBuilderFactory
54 .newCachedBzlLoadDataBuilder()
shreyaxbfe97432018-09-10 14:49:05 -070055 .addDep(gcKey1)
56 .addDeps(ImmutableList.of(gcKey2, gcKey3))
shreyax31262052019-08-13 13:40:06 -070057 .setKey(gcKey)
shreyaxbfe97432018-09-10 14:49:05 -070058 .setValue(dummyValue)
59 .build();
60
brandjon771a0292020-05-26 12:04:16 -070061 BzlLoadValue.Key c1Key = createStarlarkKey("//c1");
shreyaxbfe97432018-09-10 14:49:05 -070062 SkyKey c1Key1 = createKey("c1 key1");
brandjon24912642020-05-27 11:28:45 -070063 CachedBzlLoadData c1 =
64 cachedBzlLoadDataBuilderFactory
65 .newCachedBzlLoadDataBuilder()
shreyaxbfe97432018-09-10 14:49:05 -070066 .addDep(c1Key1)
67 .addTransitiveDeps(gc)
68 .setValue(dummyValue)
shreyax31262052019-08-13 13:40:06 -070069 .setKey(c1Key)
shreyaxbfe97432018-09-10 14:49:05 -070070 .build();
71
brandjon771a0292020-05-26 12:04:16 -070072 BzlLoadValue.Key c2Key = createStarlarkKey("//c2");
shreyaxbfe97432018-09-10 14:49:05 -070073 SkyKey c2Key1 = createKey("c2 key1");
74 SkyKey c2Key2 = createKey("c2 key2");
brandjon24912642020-05-27 11:28:45 -070075 CachedBzlLoadData c2 =
76 cachedBzlLoadDataBuilderFactory
77 .newCachedBzlLoadDataBuilder()
shreyaxbfe97432018-09-10 14:49:05 -070078 .addDeps(ImmutableList.of(c2Key1, c2Key2))
79 .addTransitiveDeps(gc)
80 .setValue(dummyValue)
shreyax31262052019-08-13 13:40:06 -070081 .setKey(c2Key)
shreyaxbfe97432018-09-10 14:49:05 -070082 .build();
83
brandjon771a0292020-05-26 12:04:16 -070084 BzlLoadValue.Key pKey = createStarlarkKey("//p");
shreyaxbfe97432018-09-10 14:49:05 -070085 SkyKey pKey1 = createKey("p key1");
brandjon24912642020-05-27 11:28:45 -070086 CachedBzlLoadData p =
87 cachedBzlLoadDataBuilderFactory
88 .newCachedBzlLoadDataBuilder()
shreyaxbfe97432018-09-10 14:49:05 -070089 .addDep(pKey1)
90 .addTransitiveDeps(c1)
91 .addTransitiveDeps(c2)
92 .setValue(dummyValue)
shreyax31262052019-08-13 13:40:06 -070093 .setKey(pKey)
shreyaxbfe97432018-09-10 14:49:05 -070094 .build();
95
96 List<Iterable<SkyKey>> registeredDeps = new ArrayList<>();
brandjon24912642020-05-27 11:28:45 -070097 Map<BzlLoadValue.Key, CachedBzlLoadData> visitedBzls = new HashMap<>();
98 p.traverse(registeredDeps::add, visitedBzls);
shreyaxbfe97432018-09-10 14:49:05 -070099
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();
shreyax31262052019-08-13 13:40:06 -0700108
brandjon24912642020-05-27 11:28:45 -0700109 assertThat(visitedBzls).containsExactly(pKey, p, c1Key, c1, c2Key, c2, gcKey, gc);
shreyaxbfe97432018-09-10 14:49:05 -0700110 }
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 }
shreyax31262052019-08-13 13:40:06 -0700126
brandjon771a0292020-05-26 12:04:16 -0700127 private static BzlLoadValue.Key createStarlarkKey(String name) {
brandjonb28a76b2020-05-26 12:53:22 -0700128 return BzlLoadValue.keyForBuild(Label.parseAbsoluteUnchecked(name));
shreyax31262052019-08-13 13:40:06 -0700129 }
shreyaxbfe97432018-09-10 14:49:05 -0700130}