Suppress test 1931 flakes caused by spurious wakeups
This is the same as the fix for test 1932 from commit
7aee63e49dc1421c7840de091bba04f5cf1a87e5 but for test 1931.
Spurious 'wait' wakeups can cause locks to appear contended when they
otherwise wouldn't be. This could make test 1931 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: I8583b5f8a0665556d1562f62bd69728eccf9412b
diff --git a/test/1931-monitor-events/src/art/Test1931.java b/test/1931-monitor-events/src/art/Test1931.java
index f549789..b1fb841 100644
--- a/test/1931-monitor-events/src/art/Test1931.java
+++ b/test/1931-monitor-events/src/art/Test1931.java
@@ -29,6 +29,18 @@
import java.util.function.Function;
public class Test1931 {
+ private static Monitors.LockController CONTENTION_SUPPRESSED = null;
+
+ 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;
+ };
+ }
+
public static void printStackTrace(Throwable t) {
System.out.println("Caught exception: " + t);
for (Throwable c = t.getCause(); c != null; c = c.getCause()) {
@@ -39,10 +51,16 @@
}
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);
}
public static void handleMonitorEntered(Thread thd, Object lock) {
+ if (CONTENTION_SUPPRESSED != null && CONTENTION_SUPPRESSED.IsWorkerThread(thd)) {
+ return;
+ }
System.out.println(thd.getName() + " LOCKED " + lock);
}
public static void handleMonitorWait(Thread thd, Object lock, long timeout) {
@@ -170,10 +188,14 @@
controller1.waitForLockToBeHeld();
controller1.DoWait();
controller1.waitForNotifySleep();
- controller2.DoLock();
- controller2.waitForLockToBeHeld();
- controller2.DoNotifyAll();
- controller2.DoUnlock();
+ try (AutoCloseable suppress = SuppressContention(controller2)) {
+ // If controller1 has a spurious wakeup we could see contention here. Suppress it so it won't
+ // cause the test to fail.
+ controller2.DoLock();
+ controller2.waitForLockToBeHeld();
+ controller2.DoNotifyAll();
+ controller2.DoUnlock();
+ }
controller1.waitForLockToBeHeld();
controller1.DoUnlock();
}
@@ -187,10 +209,14 @@
controller1.waitForLockToBeHeld();
controller1.DoTimedWait();
controller1.waitForNotifySleep();
- controller2.DoLock();
- controller2.waitForLockToBeHeld();
- controller2.DoNotifyAll();
- controller2.DoUnlock();
+ try (AutoCloseable suppress = SuppressContention(controller2)) {
+ // If controller1 has a spurious wakeup we could see contention here. Suppress it so it won't
+ // cause the test to fail.
+ controller2.DoLock();
+ controller2.waitForLockToBeHeld();
+ controller2.DoNotifyAll();
+ controller2.DoUnlock();
+ }
controller1.waitForLockToBeHeld();
controller1.DoUnlock();
}