Search in sources :

Example 1 with ValueComputer

use of com.google.devtools.build.skyframe.GraphTester.ValueComputer in project bazel by bazelbuild.

the class MemoizingEvaluatorTest method limitEvaluatorThreads.

@Test
public void limitEvaluatorThreads() throws Exception {
    initializeTester();
    int numKeys = 10;
    final Object lock = new Object();
    final AtomicInteger inProgressCount = new AtomicInteger();
    final int[] maxValue = { 0 };
    SkyKey topLevel = GraphTester.toSkyKey("toplevel");
    TestFunction topLevelBuilder = tester.getOrCreate(topLevel);
    for (int i = 0; i < numKeys; i++) {
        topLevelBuilder.addDependency("subKey" + i);
        tester.getOrCreate("subKey" + i).setComputedValue(new ValueComputer() {

            @Override
            public SkyValue compute(Map<SkyKey, SkyValue> deps, SkyFunction.Environment env) {
                int val = inProgressCount.incrementAndGet();
                synchronized (lock) {
                    if (val > maxValue[0]) {
                        maxValue[0] = val;
                    }
                }
                Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS);
                inProgressCount.decrementAndGet();
                return new StringValue("abc");
            }
        });
    }
    topLevelBuilder.setConstantValue(new StringValue("xyz"));
    EvaluationResult<StringValue> result = tester.eval(/*keepGoing=*/
    true, /*numThreads=*/
    5, topLevel);
    assertFalse(result.hasError());
    assertEquals(5, maxValue[0]);
}
Also used : TestFunction(com.google.devtools.build.skyframe.GraphTester.TestFunction) ValueComputer(com.google.devtools.build.skyframe.GraphTester.ValueComputer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Environment(com.google.devtools.build.skyframe.SkyFunction.Environment) NotComparableStringValue(com.google.devtools.build.skyframe.GraphTester.NotComparableStringValue) StringValue(com.google.devtools.build.skyframe.GraphTester.StringValue) Test(org.junit.Test)

Example 2 with ValueComputer

use of com.google.devtools.build.skyframe.GraphTester.ValueComputer in project bazel by bazelbuild.

the class MemoizingEvaluatorTest method checkNotComparableNotPruned.

private void checkNotComparableNotPruned(boolean hasEvent) throws Exception {
    initializeTester();
    SkyKey parent = GraphTester.toSkyKey("parent");
    SkyKey child = GraphTester.toSkyKey("child");
    NotComparableStringValue notComparableString = new NotComparableStringValue("not comparable");
    if (hasEvent) {
        tester.getOrCreate(child).setConstantValue(notComparableString).setWarning("shmoop");
    } else {
        tester.getOrCreate(child).setConstantValue(notComparableString);
    }
    final AtomicInteger parentEvaluated = new AtomicInteger();
    final String val = "some val";
    tester.getOrCreate(parent).addDependency(child).setComputedValue(new ValueComputer() {

        @Override
        public SkyValue compute(Map<SkyKey, SkyValue> deps, Environment env) throws InterruptedException {
            parentEvaluated.incrementAndGet();
            return new StringValue(val);
        }
    });
    assertStringValue(val, tester.evalAndGet(/*keepGoing=*/
    false, parent));
    assertThat(parentEvaluated.get()).isEqualTo(1);
    if (hasEvent) {
        assertContainsEvent(eventCollector, "shmoop");
    } else {
        assertEventCount(0, eventCollector);
    }
    tester.resetPlayedEvents();
    tester.getOrCreate(child, /*markAsModified=*/
    true);
    tester.invalidate();
    assertStringValue(val, tester.evalAndGet(/*keepGoing=*/
    false, parent));
    assertThat(parentEvaluated.get()).isEqualTo(2);
    if (hasEvent) {
        assertContainsEvent(eventCollector, "shmoop");
    } else {
        assertEventCount(0, eventCollector);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NotComparableStringValue(com.google.devtools.build.skyframe.GraphTester.NotComparableStringValue) Environment(com.google.devtools.build.skyframe.SkyFunction.Environment) NotComparableStringValue(com.google.devtools.build.skyframe.GraphTester.NotComparableStringValue) StringValue(com.google.devtools.build.skyframe.GraphTester.StringValue) ValueComputer(com.google.devtools.build.skyframe.GraphTester.ValueComputer)

Example 3 with ValueComputer

use of com.google.devtools.build.skyframe.GraphTester.ValueComputer in project bazel by bazelbuild.

the class MemoizingEvaluatorTest method changedChildChangesDepOfParent.

@Test
public void changedChildChangesDepOfParent() throws Exception {
    initializeTester();
    final SkyKey buildFile = GraphTester.toSkyKey("buildFile");
    ValueComputer authorDrink = new ValueComputer() {

        @Override
        public SkyValue compute(Map<SkyKey, SkyValue> deps, SkyFunction.Environment env) throws InterruptedException {
            String author = ((StringValue) deps.get(buildFile)).getValue();
            StringValue beverage;
            switch(author) {
                case "hemingway":
                    beverage = (StringValue) env.getValue(GraphTester.toSkyKey("absinthe"));
                    break;
                case "joyce":
                    beverage = (StringValue) env.getValue(GraphTester.toSkyKey("whiskey"));
                    break;
                default:
                    throw new IllegalStateException(author);
            }
            if (beverage == null) {
                return null;
            }
            return new StringValue(author + " drank " + beverage.getValue());
        }
    };
    tester.set(buildFile, new StringValue("hemingway"));
    SkyKey absinthe = GraphTester.toSkyKey("absinthe");
    tester.set(absinthe, new StringValue("absinthe"));
    SkyKey whiskey = GraphTester.toSkyKey("whiskey");
    tester.set(whiskey, new StringValue("whiskey"));
    SkyKey top = GraphTester.toSkyKey("top");
    tester.getOrCreate(top).addDependency(buildFile).setComputedValue(authorDrink);
    StringValue topValue = (StringValue) tester.evalAndGet("top");
    assertEquals("hemingway drank absinthe", topValue.getValue());
    tester.set(buildFile, new StringValue("joyce"));
    // Don't evaluate absinthe successfully anymore.
    tester.getOrCreate(absinthe).setHasError(true);
    tester.invalidate();
    topValue = (StringValue) tester.evalAndGet("top");
    assertEquals("joyce drank whiskey", topValue.getValue());
    assertThat(tester.getDirtyKeys()).containsExactly(buildFile, top);
    assertThat(tester.getDeletedKeys()).isEmpty();
}
Also used : Environment(com.google.devtools.build.skyframe.SkyFunction.Environment) NotComparableStringValue(com.google.devtools.build.skyframe.GraphTester.NotComparableStringValue) StringValue(com.google.devtools.build.skyframe.GraphTester.StringValue) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ValueComputer(com.google.devtools.build.skyframe.GraphTester.ValueComputer) Test(org.junit.Test)

Aggregations

NotComparableStringValue (com.google.devtools.build.skyframe.GraphTester.NotComparableStringValue)3 StringValue (com.google.devtools.build.skyframe.GraphTester.StringValue)3 ValueComputer (com.google.devtools.build.skyframe.GraphTester.ValueComputer)3 Environment (com.google.devtools.build.skyframe.SkyFunction.Environment)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 TestFunction (com.google.devtools.build.skyframe.GraphTester.TestFunction)1 Map (java.util.Map)1