Suppress test 1932 flakes caused by spurious wakeups

Spurious 'wait' wakeups can cause locks to appear contended when they
otherwise wouldn't be. This could make test 1932 somewhat flaky since
it could see contention in these cases. To prevent this flakiness this
suppresses output when these operations take place. This should not
affect the tests usefulness since those portions are mainly about
testing waiting behavior.

Test: ./test.py --host
Bug: 149308087
Change-Id: Ia6263a641429e47aa29c05a14f841c7935892de2
diff --git a/test/1932-monitor-events-misc/src/art/Test1932.java b/test/1932-monitor-events-misc/src/art/Test1932.java
index aee811a..84f2059 100644
--- a/test/1932-monitor-events-misc/src/art/Test1932.java
+++ b/test/1932-monitor-events-misc/src/art/Test1932.java
@@ -219,10 +219,12 @@
     controller1.waitForLockToBeHeld();
     controller1.DoWait();
     controller1.waitForNotifySleep();
-    controller2.DoLock();
-    controller2.waitForLockToBeHeld();
-    controller2.DoNotifyAll();
-    // See comment in testWaitMonitorEnter.
+    try (AutoCloseable suppress = SuppressContention(controller2)) {
+      controller2.DoLock();
+      controller2.waitForLockToBeHeld();
+      controller2.DoNotifyAll();
+      // See comment in testWaitMonitorEnter.
+    }
     controller2.DoUnlock();
     controller1.waitForLockToBeHeld();
     controller1.DoUnlock();
@@ -247,13 +249,15 @@
     controller1.waitForLockToBeHeld();
     controller1.DoWait();
     controller1.waitForNotifySleep();
-    controller2.DoLock();
-    controller2.waitForLockToBeHeld();
-    controller2.DoNotifyAll();
     // We've seen extra messages about contended locking here. They're OK if e.g. controller1
     // wakes up spuriously before the notification, which is allowed. But in the normal case,
     // we should not see contended locking here. And the extra messages seem rare enough that
     // we can tolerate the spurious failures to catch more bugs. See b/149308087.
+    try (AutoCloseable suppress = SuppressContention(controller2)) {
+      controller2.DoLock();
+      controller2.waitForLockToBeHeld();
+      controller2.DoNotifyAll();
+    }
     controller2.DoUnlock();
     controller1.waitForLockToBeHeld();
     controller1.DoUnlock();
@@ -277,10 +281,12 @@
     controller2.waitForLockToBeHeld();
     controller2.DoWait();
     controller2.waitForNotifySleep();
-    controller1.DoLock();
-    controller1.waitForLockToBeHeld();
-    controller1.DoNotifyAll();
-    // See comment in testWaitMonitorEnter.
+    try (AutoCloseable suppress = SuppressContention(controller1)) {
+      controller1.DoLock();
+      controller1.waitForLockToBeHeld();
+      controller1.DoNotifyAll();
+      // See comment in testWaitMonitorEnter.
+    }
     controller2.waitForLockToBeHeld();
     controller2.DoUnlock();
   }
@@ -603,7 +609,22 @@
     }
   }
 
+  public static AutoCloseable SuppressContention(Monitors.LockController controller) {
+    if (CONTENTION_SUPPRESSED != null) {
+      throw new IllegalStateException("Only one contention suppression is possible at a time.");
+    }
+    CONTENTION_SUPPRESSED = controller;
+    return () -> {
+      CONTENTION_SUPPRESSED = null;
+    };
+  }
+
+  private static Monitors.LockController CONTENTION_SUPPRESSED = null;
+
   public static void handleMonitorEnter(Thread thd, Object lock) {
+    if (CONTENTION_SUPPRESSED != null && CONTENTION_SUPPRESSED.IsWorkerThread(thd)) {
+      return;
+    }
     System.out.println(thd.getName() + " contended-LOCKING " + lock);
     if (HANDLER != null) {
       HANDLER.handleMonitorEnter(thd, lock);
@@ -611,6 +632,9 @@
   }
 
   public static void handleMonitorEntered(Thread thd, Object lock) {
+    if (CONTENTION_SUPPRESSED != null && CONTENTION_SUPPRESSED.IsWorkerThread(thd)) {
+      return;
+    }
     System.out.println(thd.getName() + " LOCKED " + lock);
     if (HANDLER != null) {
       HANDLER.handleMonitorEntered(thd, lock);