Disable crashing on uncaught exceptions in `BazelBuildEventServiceModuleTest`.

The test became flaky after we started crashing on uncaught exceptions in test in https://github.com/bazelbuild/bazel/commit/882321bd2cd126e586cb555e7f4ae535d965ec3d. Disable it for the test since it leaves runaway threads which are fine to fail.

PiperOrigin-RevId: 415367233
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 7d4e952..ec9c6f0 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
@@ -81,6 +81,7 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.Thread.UncaughtExceptionHandler;
 import java.time.Duration;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
@@ -165,6 +166,14 @@
     runtimeWrapper.newCommand();
   }
 
+  @Override
+  @Nullable
+  protected UncaughtExceptionHandler createUncaughtExceptionHandler() {
+    // Disable the crash handler since this test leaves runaway threads e.g. accessing shut down
+    // fakeServer.
+    return null;
+  }
+
   @Before
   public void setUp() throws Exception {
     serviceRegistry.addService(buildEventService);
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 036f750..6ea3c4a 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
@@ -130,9 +130,6 @@
 import javax.annotation.concurrent.GuardedBy;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Rule;
-import org.junit.rules.TestRule;
-import org.junit.runners.model.Statement;
 
 /**
  * A base class for integration tests that use the {@link BuildTool}. These tests basically run a
@@ -161,8 +158,6 @@
     }
   }
 
-  @Rule public TestRule crashHandler = createHandleCrashHandlerRule();
-
   protected FileSystem fileSystem;
   protected EventCollectionApparatus events = createEvents();
   protected OutErr outErr = OutErr.SYSTEM_OUT_ERR;
@@ -180,6 +175,8 @@
   private Path workspace;
   protected RecordingExceptionHandler subscriberException = new RecordingExceptionHandler();
 
+  @Nullable private UncaughtExceptionHandler oldExceptionHandler;
+
   private static final ImmutableList<Injected> BAZEL_REPOSITORY_PRECOMPUTED_VALUES =
       ImmutableList.of(
           PrecomputedValue.injected(
@@ -224,6 +221,29 @@
     AnalysisMock.get().setupMockToolsRepository(mockToolsConfig);
   }
 
+  @Before
+  public final void setUncaughtExceptionHandler() {
+    oldExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
+    Thread.setDefaultUncaughtExceptionHandler(createUncaughtExceptionHandler());
+  }
+
+  @After
+  public final void restoreUncaughtExceptionHandler() {
+    Thread.setDefaultUncaughtExceptionHandler(oldExceptionHandler);
+  }
+
+  /**
+   * Creates an uncaught exception handler to be used in {@link
+   * Thread#setDefaultUncaughtExceptionHandler}.
+   *
+   * <p>Returns {@code null} if ne exception handler should be used.
+   */
+  @Nullable
+  protected UncaughtExceptionHandler createUncaughtExceptionHandler() {
+    return (ignored, exception) ->
+        BugReport.handleCrash(Crash.from(exception), CrashContext.keepAlive());
+  }
+
   protected ServerDirectories createServerDirectories() {
     return new ServerDirectories(
         /*installBase=*/ outputBase,
@@ -987,25 +1007,4 @@
       exceptions.clear();
     }
   }
-
-  /**
-   * Creates a JUnit rule to set a default handler for uncaught exceptions to run {@link
-   * BugReport#handleCrash(Crash, CrashContext)}.
-   */
-  private static TestRule createHandleCrashHandlerRule() {
-    return (base, description) ->
-        new Statement() {
-          @Override
-          public void evaluate() throws Throwable {
-            @Nullable UncaughtExceptionHandler old = Thread.getDefaultUncaughtExceptionHandler();
-            Thread.setDefaultUncaughtExceptionHandler(
-                (ignored, exception) ->
-                    BugReport.handleCrash(Crash.from(exception), CrashContext.keepAlive()));
-
-            base.evaluate();
-
-            Thread.setDefaultUncaughtExceptionHandler(old);
-          }
-        };
-  }
 }