diff options
author | 2017-07-20 11:42:12 -0700 | |
---|---|---|
committer | 2017-07-20 22:04:36 +0000 | |
commit | ed3a357e6b28dbc00a60b72af8bb846775348172 (patch) | |
tree | ee94b390cabe28db2bc13f5e6d900a666e39d8a4 | |
parent | e8f48da635c4d07bbe431e5819da8e1fad91a8ef (diff) |
Make test 924 less flaky.
GetThreadState on threads blocked on monitors can be affected by GC
and other internals. This is because internal suspension of threads
marks them as Runnable. To make this test not observe this as much we
make it call GetThreadState multiple times and use the most common
result as the threads current state.
Bug: 62117833
Bug: 63903050
Test: stress --cpu 64 &
while ./test/run-test --host 924; do ; done
Change-Id: Iaa38cb58386416a72a6bb3c26ed90f4b8b623d1b
-rw-r--r-- | test/924-threads/src/art/Test924.java | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/test/924-threads/src/art/Test924.java b/test/924-threads/src/art/Test924.java index 84b7c62264..b73eb30888 100644 --- a/test/924-threads/src/art/Test924.java +++ b/test/924-threads/src/art/Test924.java @@ -164,8 +164,10 @@ public class Test924 { do { Thread.yield(); } while (t.getState() != Thread.State.BLOCKED); - Thread.sleep(10); - printThreadState(t); + // Since internal thread suspension (For GC or other cases) can happen at any time and changes + // the thread state we just have it print the majority thread state across 11 calls over 55 + // milliseconds. + printMajorityThreadState(t, 11, 5); } // Sleeping. @@ -357,10 +359,32 @@ public class Test924 { STATE_KEYS.addAll(STATE_NAMES.keySet()); Collections.sort(STATE_KEYS); } - + + // Call getThreadState 'votes' times waiting 'wait' millis between calls and print the most common + // result. + private static void printMajorityThreadState(Thread t, int votes, int wait) throws Exception { + Map<Integer, Integer> states = new HashMap<>(); + for (int i = 0; i < votes; i++) { + int cur_state = getThreadState(t); + states.put(cur_state, states.getOrDefault(cur_state, 0) + 1); + Thread.sleep(wait); // Wait a little bit. + } + int best_state = -1; + int highest_count = 0; + for (Map.Entry<Integer, Integer> e : states.entrySet()) { + if (e.getValue() > highest_count) { + highest_count = e.getValue(); + best_state = e.getKey(); + } + } + printThreadState(best_state); + } + private static void printThreadState(Thread t) { - int state = getThreadState(t); + printThreadState(getThreadState(t)); + } + private static void printThreadState(int state) { StringBuilder sb = new StringBuilder(); for (Integer i : STATE_KEYS) { |