Make 129-ThreadGetId more robust.
Make sure we get the heap task daemon, to avoid a race with the runtime
creating the stack trace, and then updating the peer.
test: 129-ThreadGetId
bug: 28261069
Change-Id: I739ab6cd0180e2be07b7cecac6ad8a905a7c9cd0
diff --git a/test/129-ThreadGetId/src/Main.java b/test/129-ThreadGetId/src/Main.java
index 5aefd17..6ba01ff 100644
--- a/test/129-ThreadGetId/src/Main.java
+++ b/test/129-ThreadGetId/src/Main.java
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+import java.lang.reflect.Field;
import java.util.Map;
public class Main implements Runnable {
@@ -33,14 +34,38 @@
System.out.println("Finishing");
}
- static void test_getStackTraces() {
+ static Thread getHeapTaskDaemon() throws Exception {
+ Field f = ThreadGroup.class.getDeclaredField("systemThreadGroup");
+ f.setAccessible(true);
+ ThreadGroup systemThreadGroup = (ThreadGroup) f.get(null);
+
+ while (true) {
+ int activeCount = systemThreadGroup.activeCount();
+ Thread[] array = new Thread[activeCount];
+ systemThreadGroup.enumerate(array);
+ for (Thread thread : array) {
+ if (thread.getName().equals("HeapTaskDaemon") &&
+ thread.getState() != Thread.State.NEW) {
+ return thread;
+ }
+ }
+ // Yield to eventually get the daemon started.
+ Thread.sleep(10);
+ }
+ }
+
+ static void test_getStackTraces() throws Exception {
+ Thread heapDaemon = getHeapTaskDaemon();
+
+ // Force a GC to ensure the daemon truly started.
+ Runtime.getRuntime().gc();
// Check all the current threads for positive IDs.
Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
for (Map.Entry<Thread, StackTraceElement[]> pair : map.entrySet()) {
Thread thread = pair.getKey();
// Expect empty stack trace since we do not support suspending the GC thread for
// obtaining stack traces. See b/28261069.
- if (thread.getName().equals("HeapTaskDaemon")) {
+ if (thread == heapDaemon) {
System.out.println(thread.getName() + " depth " + pair.getValue().length);
}
}