Skip to content

Commit

Permalink
Migrate injectListenerAtStartOfNextBuild method from `RewindingTest…
Browse files Browse the repository at this point in the history
…sHelper` to `BuildIntegrationTestCase`

PiperOrigin-RevId: 497014872
Change-Id: I3522b735dadf7db3fa84c5f4cd99b6389f0f1b19
  • Loading branch information
yuyue730 authored and copybara-github committed Dec 21, 2022
1 parent 90bf727 commit 051c9a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ java_library(
"//src/test/java/com/google/devtools/build/lib/testutil:TestConstants",
"//src/test/java/com/google/devtools/build/lib/testutil:TestUtils",
"//src/test/java/com/google/devtools/build/lib/vfs/util",
"//src/test/java/com/google/devtools/build/skyframe:testutil",
"//third_party:error_prone_annotations",
"//third_party:guava",
"//third_party:guava-testlib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.eventbus.Subscribe;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;
import com.google.devtools.build.lib.actions.Action;
Expand Down Expand Up @@ -61,6 +62,7 @@
import com.google.devtools.build.lib.buildtool.BuildRequest;
import com.google.devtools.build.lib.buildtool.BuildResult;
import com.google.devtools.build.lib.buildtool.BuildTool;
import com.google.devtools.build.lib.buildtool.buildevent.BuildStartingEvent;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
Expand Down Expand Up @@ -126,10 +128,12 @@
import com.google.devtools.build.lib.vfs.Symlinks;
import com.google.devtools.build.lib.vfs.util.FileSystems;
import com.google.devtools.build.lib.worker.WorkerModule;
import com.google.devtools.build.skyframe.NotifyingHelper;
import com.google.devtools.common.options.OptionsBase;
import com.google.devtools.common.options.OptionsParser;
import com.google.devtools.common.options.OptionsParsingResult;
import com.google.errorprone.annotations.FormatMethod;
import com.google.errorprone.annotations.Keep;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
Expand Down Expand Up @@ -248,6 +252,34 @@ public final BlazeRuntimeWrapper getRuntimeWrapper() {
return runtimeWrapper;
}

/**
* Lazily injects the given listener at the start of the next build.
*
* <p>Injecting the listener immediately would reach the <em>current</em> evaluator, but the next
* build may create a new evaluator, which happens when {@link
* com.google.devtools.build.lib.skyframe.SequencedSkyframeExecutor} is not tracking incremental
* state.
*/
public final void injectListenerAtStartOfNextBuild(NotifyingHelper.Listener listener) {
getRuntimeWrapper()
.registerSubscriber(
new Object() {
private boolean injected = false;

@Subscribe
@Keep
void buildStarting(@SuppressWarnings("unused") BuildStartingEvent event) {
if (!injected) {
getSkyframeExecutor()
.getEvaluator()
.injectGraphTransformerForTesting(
NotifyingHelper.makeNotifyingTransformer(listener));
injected = true;
}
}
});
}

/**
* Creates an uncaught exception handler to be used in {@link
* Thread#setDefaultUncaughtExceptionHandler}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.eventbus.Subscribe;
import com.google.common.util.concurrent.Uninterruptibles;
import com.google.devtools.build.lib.actions.Action;
import com.google.devtools.build.lib.actions.ActionExecutionContext;
Expand All @@ -49,7 +48,6 @@
import com.google.devtools.build.lib.actions.SpawnResult;
import com.google.devtools.build.lib.analysis.config.CoreOptions;
import com.google.devtools.build.lib.bugreport.BugReporter;
import com.google.devtools.build.lib.buildtool.buildevent.BuildStartingEvent;
import com.google.devtools.build.lib.buildtool.util.BuildIntegrationTestCase;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.exec.SpawnExecException;
Expand All @@ -76,7 +74,6 @@
import com.google.devtools.build.skyframe.TrackingAwaiter;
import com.google.devtools.build.skyframe.proto.GraphInconsistency.Inconsistency;
import com.google.errorprone.annotations.ForOverride;
import com.google.errorprone.annotations.Keep;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -215,7 +212,8 @@ public final List<SkyKey> collectOrderedDirtiedKeys() {

final List<SkyKey> collectOrderedDirtiedKeys(boolean exactNestedSets) {
List<SkyKey> dirtiedKeys = new ArrayList<>();
injectListenerAtStartOfNextBuild(collectOrderedDirtyKeysListener(dirtiedKeys, exactNestedSets));
testCase.injectListenerAtStartOfNextBuild(
collectOrderedDirtyKeysListener(dirtiedKeys, exactNestedSets));
return dirtiedKeys;
}

Expand Down Expand Up @@ -253,36 +251,6 @@ public static NotifyingHelper.Listener collectOrderedDirtyKeysListener(
};
}

/**
* Lazily injects the given listener at the start of the next build.
*
* <p>Injecting the listener immediately would reach the <em>current</em> evaluator, but the next
* build may create a new evaluator, which happens when {@link
* com.google.devtools.build.lib.skyframe.SequencedSkyframeExecutor} is not tracking incremental
* state.
*/
private void injectListenerAtStartOfNextBuild(NotifyingHelper.Listener listener) {
testCase
.getRuntimeWrapper()
.registerSubscriber(
new Object() {
private boolean injected = false;

@Subscribe
@Keep
void buildStarting(@SuppressWarnings("unused") BuildStartingEvent event) {
if (!injected) {
testCase
.getSkyframeExecutor()
.getEvaluator()
.injectGraphTransformerForTesting(
NotifyingHelper.makeNotifyingTransformer(listener));
injected = true;
}
}
});
}

static void assertOnlyActionsDirtied(List<SkyKey> dirtiedKeys) {
for (SkyKey key : dirtiedKeys) {
if (!(key instanceof ArtifactNestedSetKey)) {
Expand Down Expand Up @@ -396,7 +364,7 @@ public final void runBuildingParentFoundUndoneChildNotToleratedWithoutRewinding(
"foo/BUILD",
"genrule(name = 'top', outs = ['top.out'], srcs = [':dep'], cmd = 'cp $< $@')",
"genrule(name = 'dep', outs = ['dep.out'], cmd = 'touch $@')");
injectListenerAtStartOfNextBuild(
testCase.injectListenerAtStartOfNextBuild(
(key, type, order, context) -> {
if (type == EventType.GET_BATCH
&& order == Order.BEFORE
Expand Down Expand Up @@ -539,7 +507,7 @@ public final void runInputDiscoveringActionNoticesMissingDep() throws Exception
CountDownLatch cppRestarted = new CountDownLatch(1);
Label cppLabel = Label.parseAbsoluteUnchecked("//test:loser");
Label gensrcLabel = Label.parseAbsoluteUnchecked("//test:gensrc");
injectListenerAtStartOfNextBuild(
testCase.injectListenerAtStartOfNextBuild(
(key, type, order, context) -> {
if (EventType.ADD_REVERSE_DEP.equals(type)
&& isActionExecutionKey(context, cppLabel)
Expand Down Expand Up @@ -1383,7 +1351,7 @@ public final void runParallelTrackSharedActionsRewound() throws Exception {
AtomicInteger shared2ADirtied = new AtomicInteger(0);
AtomicInteger shared2AReady = new AtomicInteger(0);
AtomicInteger shared2BReady = new AtomicInteger(0);
injectListenerAtStartOfNextBuild(
testCase.injectListenerAtStartOfNextBuild(
(key, type, order, context) -> {
// Count the times shared_1{A,B} are dirtied.
if (type.equals(NotifyingHelper.EventType.MARK_DIRTY) && order.equals(Order.AFTER)) {
Expand Down Expand Up @@ -2558,7 +2526,7 @@ public final void runDoneToDirtyDepForNodeInError() throws Exception {
return createLostInputsExecException(
context, lostInputs, new ActionInputDepOwnerMap(lostInputs));
});
injectListenerAtStartOfNextBuild(
testCase.injectListenerAtStartOfNextBuild(
(key, type, order, context) -> {
if (isActionExecutionKey(key, fail) && type == EventType.CREATE_IF_ABSENT) {
awaitUninterruptibly(depDone);
Expand Down

0 comments on commit 051c9a5

Please sign in to comment.