Skip to content

Commit

Permalink
Add a way to test StateMachine producers.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 599934800
Change-Id: Ie03488c30a7e60e067e9345c839758ef030d510b
  • Loading branch information
katre authored and copybara-github committed Jan 19, 2024
1 parent d32ea79 commit addf41b
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ filegroup(
"//src/test/java/com/google/devtools/build/lib/analysis/extra:srcs",
"//src/test/java/com/google/devtools/build/lib/analysis/mock:srcs",
"//src/test/java/com/google/devtools/build/lib/analysis/platform:srcs",
"//src/test/java/com/google/devtools/build/lib/analysis/producers:srcs",
"//src/test/java/com/google/devtools/build/lib/analysis/starlark/annotations/processor:srcs",
"//src/test/java/com/google/devtools/build/lib/analysis/testing:srcs",
"//src/test/java/com/google/devtools/build/lib/analysis/util:srcs",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@rules_java//java:defs.bzl", "java_test")

package(
default_applicable_licenses = ["//:license"],
default_testonly = 1,
default_visibility = ["//src:__subpackages__"],
)

filegroup(
name = "srcs",
testonly = 0,
srcs = glob(["*"]),
visibility = ["//src:__subpackages__"],
)

java_test(
name = "ProducersTests",
srcs = glob(["*.java"]),
test_class = "com.google.devtools.build.lib.AllTests",
runtime_deps = [
"//src/test/java/com/google/devtools/build/lib:test_runner",
],
deps = [
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/analysis/producers",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster",
"//src/main/java/com/google/devtools/build/lib/skyframe/toolchains:platform_lookup_util",
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//src/test/java/com/google/devtools/build/lib/analysis/util",
"//third_party:jsr305",
"//third_party:junit4",
"//third_party:truth",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2024 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.analysis.producers;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.skyframe.toolchains.PlatformLookupUtil.InvalidPlatformException;
import com.google.devtools.build.skyframe.state.StateMachine;
import javax.annotation.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests of {@link PlatformInfoProducer}. */
@RunWith(JUnit4.class)
public class PlatformInfoProducerTest extends ProducerTestCase {
@Test
public void infoLookup() throws Exception {
scratch.overwriteFile(
"lookup/BUILD",
"constraint_setting(name = 'setting1')",
"constraint_value(name = 'value1', constraint_setting = ':setting1')",
"platform(",
" name = 'basic',",
" constraint_values = [':value1'],",
")");

Label platformLabel = Label.parseCanonicalUnchecked("//lookup:basic");
PlatformInfo result = fetch(platformLabel);

assertThat(result).isNotNull();
assertThat(result.label()).isEqualTo(platformLabel);
}

@Test
public void infoLookup_alias() throws Exception {
scratch.overwriteFile(
"lookup/BUILD",
"constraint_setting(name = 'setting1')",
"constraint_value(name = 'value1', constraint_setting = ':setting1')",
"platform(",
" name = 'basic',",
" constraint_values = [':value1'],",
")",
"alias(name = 'alias', actual = ':basic')");

Label platformLabel = Label.parseCanonicalUnchecked("//lookup:alias");
PlatformInfo result = fetch(platformLabel);

assertThat(result).isNotNull();
assertThat(result.label()).isEqualTo(Label.parseCanonicalUnchecked("//lookup:basic"));
}

@Test
public void infoLookup_error() throws Exception {
scratch.overwriteFile("lookup/BUILD", "filegroup(", " name = 'basic',", ")");

Label platformLabel = Label.parseCanonicalUnchecked("//lookup:basic");
assertThrows(InvalidPlatformException.class, () -> fetch(platformLabel));
}

private PlatformInfo fetch(Label platformLabel)
throws InvalidPlatformException, InterruptedException {
PlatformInfoSink sink = new PlatformInfoSink();
PlatformInfoProducer producer =
new PlatformInfoProducer(platformLabel, sink, StateMachine.DONE);
assertThat(executeProducer(producer)).isTrue();
return sink.platformInfo();
}

/** Receiver for platform info from {@link PlatformInfoProducer}. */
private static class PlatformInfoSink implements PlatformInfoProducer.ResultSink {
@Nullable private PlatformInfo platformInfo = null;
@Nullable private InvalidPlatformException platformInfoError = null;

@Override
public void acceptPlatformInfo(PlatformInfo info) {
this.platformInfo = info;
}

@Override
public void acceptPlatformInfoError(InvalidPlatformException error) {
this.platformInfoError = error;
}

PlatformInfo platformInfo() throws InvalidPlatformException {
if (this.platformInfoError != null) {
throw this.platformInfoError;
}
if (this.platformInfo != null) {
return platformInfo;
}
throw new IllegalStateException("Value and exception not set");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.analysis.producers;

import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
import com.google.devtools.build.skyframe.EvaluationContext;
import com.google.devtools.build.skyframe.EvaluationResult;
import com.google.devtools.build.skyframe.SkyValue;
import com.google.devtools.build.skyframe.state.StateMachine;
import com.google.devtools.build.skyframe.state.StateMachineEvaluatorForTesting;

/** Base class for tests of producers. */
public abstract class ProducerTestCase extends BuildViewTestCase {
/**
* Use a {@link StateMachineEvaluatorForTesting} to drive the given {@link StateMachine} until it
* finishes (with a result or an error). Results should be retrieved from whatever result sink the
* {@link StateMachine} is designed for.
*
* @return {@code true} on success
*/
public boolean executeProducer(StateMachine producer) throws InterruptedException {
EvaluationContext context =
EvaluationContext.newBuilder()
.setKeepGoing(true)
.setParallelism(SkyframeExecutor.DEFAULT_THREAD_COUNT)
.setEventHandler(reporter)
.build();
try {
getSkyframeExecutor().getSkyframeBuildView().enableAnalysis(true);
EvaluationResult<SkyValue> result =
StateMachineEvaluatorForTesting.run(
producer, getSkyframeExecutor().getEvaluator(), context);
if (result != null) {
return !result.hasError();
}
} finally {
getSkyframeExecutor().getSkyframeBuildView().enableAnalysis(false);
}
return true;
}
}

0 comments on commit addf41b

Please sign in to comment.