|
1 | 1 | /* |
2 | | - * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
34 | 34 | import com.sun.jdi.event.EventIterator; |
35 | 35 | import com.sun.jdi.event.EventQueue; |
36 | 36 | import com.sun.jdi.event.EventSet; |
| 37 | +import com.sun.jdi.event.ThreadDeathEvent; |
| 38 | +import com.sun.jdi.event.ThreadStartEvent; |
37 | 39 | import com.sun.jdi.request.BreakpointRequest; |
38 | 40 | import com.sun.jdi.request.EventRequest; |
39 | 41 | import com.sun.jdi.request.EventRequestManager; |
@@ -150,6 +152,37 @@ protected final void getEventSet() throws JDITestRuntimeException { |
150 | 152 | } |
151 | 153 | } |
152 | 154 |
|
| 155 | + // Special version of getEventSet for ThreadStartEvent/ThreadDeathEvent. |
| 156 | + // When ThreadStartRequest and/or ThreadDeathRequest are enabled, |
| 157 | + // we can get the events from system threads unexpected for tests. |
| 158 | + // The method skips ThreadStartEvent/ThreadDeathEvent events |
| 159 | + // for all threads except the expected one. |
| 160 | + protected void getEventSetForThreadStartDeath(String threadName) throws JDITestRuntimeException { |
| 161 | + boolean gotDesiredEvent = false; |
| 162 | + while (!gotDesiredEvent) { |
| 163 | + getEventSet(); |
| 164 | + Event event = eventIterator.nextEvent(); |
| 165 | + if (event instanceof ThreadStartEvent evt) { |
| 166 | + if (evt.thread().name().equals(threadName)) { |
| 167 | + gotDesiredEvent = true; |
| 168 | + } else { |
| 169 | + log2("Got ThreadStartEvent for wrong thread: " + event); |
| 170 | + } |
| 171 | + } else if (event instanceof ThreadDeathEvent evt) { |
| 172 | + if (evt.thread().name().equals(threadName)) { |
| 173 | + gotDesiredEvent = true; |
| 174 | + } else { |
| 175 | + log2("Got ThreadDeathEvent for wrong thread: " + event); |
| 176 | + } |
| 177 | + } else { |
| 178 | + // not ThreadStartEvent nor ThreadDeathEvent |
| 179 | + gotDesiredEvent = true; |
| 180 | + } |
| 181 | + } |
| 182 | + // reset the iterator before return |
| 183 | + eventIterator = eventSet.eventIterator(); |
| 184 | + } |
| 185 | + |
153 | 186 | protected void breakpointForCommunication() throws JDITestRuntimeException { |
154 | 187 |
|
155 | 188 | log2("breakpointForCommunication"); |
|
0 commit comments