Add unit tests for memory pressure events.
PiperOrigin-RevId: 415110987
diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD
index 7c1d276..98f9877 100644
--- a/src/test/java/com/google/devtools/build/lib/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/BUILD
@@ -40,6 +40,7 @@
"//src/test/java/com/google/devtools/build/lib/packages/metrics:srcs",
"//src/test/java/com/google/devtools/build/lib/packages/semantics:srcs",
"//src/test/java/com/google/devtools/build/lib/platform:srcs",
+ "//src/test/java/com/google/devtools/build/lib/platform/darwin:srcs",
"//src/test/java/com/google/devtools/build/lib/profiler:srcs",
"//src/test/java/com/google/devtools/build/lib/profiler/callcounts:srcs",
"//src/test/java/com/google/devtools/build/lib/profiler/memory:srcs",
diff --git a/src/test/java/com/google/devtools/build/lib/platform/BUILD b/src/test/java/com/google/devtools/build/lib/platform/BUILD
index 58a49ad..9d711f2 100644
--- a/src/test/java/com/google/devtools/build/lib/platform/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/platform/BUILD
@@ -39,3 +39,27 @@
"//third_party:truth",
],
)
+
+java_test(
+ name = "SystemMemoryPressureEventTest",
+ timeout = "short",
+ srcs = ["SystemMemoryPressureEventTest.java"],
+ data = select({
+ "//src/conditions:darwin": [
+ "//src/test/java/com/google/devtools/build/lib/platform/darwin:notifier",
+ ],
+ "//conditions:default": [
+ ],
+ }),
+ deps = [
+ "//src/main/java/com/google/devtools/build/lib:runtime",
+ "//src/main/java/com/google/devtools/build/lib/platform:system_memory_pressure_module",
+ "//src/main/java/com/google/devtools/build/lib/platform:system_memory_pressure_monitor",
+ "//src/main/java/com/google/devtools/build/lib/util:os",
+ "//src/test/java/com/google/devtools/build/lib/buildtool/util",
+ "//third_party:guava",
+ "//third_party:junit4",
+ "//third_party:truth",
+ "@bazel_tools//tools/java/runfiles",
+ ],
+)
diff --git a/src/test/java/com/google/devtools/build/lib/platform/SystemMemoryPressureEventTest.java b/src/test/java/com/google/devtools/build/lib/platform/SystemMemoryPressureEventTest.java
new file mode 100644
index 0000000..12c23d6
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/platform/SystemMemoryPressureEventTest.java
@@ -0,0 +1,92 @@
+// Copyright 2021 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.platform;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.eventbus.Subscribe;
+import com.google.devtools.build.lib.buildtool.util.BuildIntegrationTestCase;
+import com.google.devtools.build.lib.runtime.BlazeModule;
+import com.google.devtools.build.lib.runtime.BlazeRuntime;
+import com.google.devtools.build.lib.runtime.CommandEnvironment;
+import com.google.devtools.build.lib.util.OS;
+import com.google.devtools.build.runfiles.Runfiles;
+import org.junit.Assume;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests {@link SystemMemoryPressureEvent} by sending fake notifications. */
+@RunWith(JUnit4.class)
+public final class SystemMemoryPressureEventTest extends BuildIntegrationTestCase {
+ static class SystemMemoryPressureEventListener extends BlazeModule {
+ public int memoryPressureWarningEventCount = 0;
+ public int memoryPressureCriticalEventCount = 0;
+
+ @Override
+ public void beforeCommand(CommandEnvironment env) {
+ env.getEventBus().register(this);
+ }
+
+ @Subscribe
+ public void memoryPressureEvent(SystemMemoryPressureEvent event) {
+ switch (event.level()) {
+ case WARNING:
+ ++memoryPressureWarningEventCount;
+ assertThat(event.logString()).isEqualTo("SystemMemoryPressureEvent: Warning");
+ break;
+
+ case CRITICAL:
+ ++memoryPressureCriticalEventCount;
+ assertThat(event.logString()).isEqualTo("SystemMemoryPressureEvent: Critical");
+ break;
+ }
+ }
+ }
+
+ private final SystemMemoryPressureEventListener eventListener =
+ new SystemMemoryPressureEventListener();
+
+ @Override
+ protected BlazeRuntime.Builder getRuntimeBuilder() throws Exception {
+ return super.getRuntimeBuilder()
+ .addBlazeModule(eventListener)
+ .addBlazeModule(new SystemMemoryPressureModule());
+ }
+
+ @Test
+ public void testMemoryPressure() throws Exception {
+ Assume.assumeTrue(OS.getCurrent() == OS.DARWIN);
+ Runfiles runfiles = Runfiles.create();
+ String notifierFilePath =
+ runfiles.rlocation(
+ "io_bazel/src/test/java/com/google/devtools/build/lib/platform/darwin/notifier");
+ write(
+ "system_suspension_event/BUILD",
+ "genrule(",
+ " name = 'fire_memory_pressure_notifications',",
+ " outs = ['fire_memory_pressure_notifications.out'],",
+ " cmd = '"
+ + notifierFilePath
+ + " com.google.bazel.test.memorypressurelevel.warning 0 > $@; ' + ",
+ " '"
+ + notifierFilePath
+ + " com.google.bazel.test.memorypressurelevel.critical 0 >> $@',",
+ ")");
+ buildTarget("//system_suspension_event:fire_memory_pressure_notifications");
+ assertThat(eventListener.memoryPressureWarningEventCount).isGreaterThan(0);
+ assertThat(eventListener.memoryPressureCriticalEventCount).isGreaterThan(0);
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/platform/darwin/BUILD b/src/test/java/com/google/devtools/build/lib/platform/darwin/BUILD
index 7cc3d36..c676fbc 100644
--- a/src/test/java/com/google/devtools/build/lib/platform/darwin/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/platform/darwin/BUILD
@@ -7,6 +7,13 @@
licenses(["notice"])
+filegroup(
+ name = "srcs",
+ testonly = 0,
+ srcs = glob(["**"]),
+ visibility = ["//src:__subpackages__"],
+)
+
# Utility for sending notifications. It expects one arg which is the
# notification to send using the notify_post (3) api.
cc_binary(