Add a test cleanup step for unprocessed exception potentially left over in `BugReport`.

When a test fails due to a crash from `BugReport`, an exception will be stored in `BugReport::unprocessedThrowableInTest`. That means that until we clean it up, any following call to `BlazeRuntimeWrapper::executeBuild` will throw that exception, even if that is in the following test.

Add a cleanup step to make sure a test failure cannot taint a following one with an exception stored in `BugReport`.

PiperOrigin-RevId: 393834868
diff --git a/src/test/java/com/google/devtools/build/lib/buildeventservice/BazelBuildEventServiceModuleTest.java b/src/test/java/com/google/devtools/build/lib/buildeventservice/BazelBuildEventServiceModuleTest.java
index 30bd505..4ae061ca 100644
--- a/src/test/java/com/google/devtools/build/lib/buildeventservice/BazelBuildEventServiceModuleTest.java
+++ b/src/test/java/com/google/devtools/build/lib/buildeventservice/BazelBuildEventServiceModuleTest.java
@@ -774,6 +774,7 @@
     assertThat(runtimeWrapper.getCrashMessages())
         .containsExactly(
             TestConstants.PRODUCT_NAME + " is crashing: Crashed: (java.lang.OutOfMemoryError) ");
+    assertAndClearBugReporterStoredCrash(OutOfMemoryError.class);
   }
 
   /**
diff --git a/src/test/java/com/google/devtools/build/lib/buildtool/util/BuildIntegrationTestCase.java b/src/test/java/com/google/devtools/build/lib/buildtool/util/BuildIntegrationTestCase.java
index 98babb9..4c297cc 100644
--- a/src/test/java/com/google/devtools/build/lib/buildtool/util/BuildIntegrationTestCase.java
+++ b/src/test/java/com/google/devtools/build/lib/buildtool/util/BuildIntegrationTestCase.java
@@ -20,6 +20,7 @@
 import static com.google.common.collect.ImmutableSet.toImmutableSet;
 import static com.google.common.truth.Truth.assertThat;
 import static com.google.common.truth.Truth.assertWithMessage;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.fail;
 
 import com.google.common.collect.ImmutableList;
@@ -286,10 +287,23 @@
     }
     LoggingUtil.installRemoteLoggerForTesting(null);
     testRoot.deleteTreesBelow(); // (comment out during debugging)
+    // Make sure that a test which crashes with on a bug report does not taint following ones with
+    // an unprocessed exception stored statically in BugReport.
+    BugReport.maybePropagateUnprocessedThrowableIfInTest();
     Thread.interrupted(); // If there was a crash in test case, main thread was interrupted.
   }
 
   /**
+   * Check and clear crash was reported in {@link BugReport}.
+   *
+   * <p>{@link BugReport} stores information about crashes in a static variable when running tests.
+   * Tests which deliberately cause crashes, need to clear that flag not to taint the environment.
+   */
+  protected static void assertAndClearBugReporterStoredCrash(Class<? extends Throwable> expected) {
+    assertThrows(expected, BugReport::maybePropagateUnprocessedThrowableIfInTest);
+  }
+
+  /**
    * A helper class that can be used to record exceptions that occur on the event bus, by passing an
    * instance of it to BlazeRuntime#setEventBusExceptionHandler.
    */