author | iignatyev |
Mon, 30 Apr 2018 18:10:24 -0700 | |
changeset 49934 | 44839fbb20db |
permissions | -rw-r--r-- |
49934
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
1 |
/* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
4 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
8 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
13 |
* accompanied this code). |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
14 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
18 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
21 |
* questions. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
22 |
*/ |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
23 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
24 |
package nsk.share.jdi; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
25 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
26 |
import java.util.concurrent.locks.Lock; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
27 |
import java.util.concurrent.locks.ReentrantLock; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
28 |
import java.util.concurrent.locks.Condition; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
29 |
import java.util.concurrent.TimeUnit; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
30 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
31 |
import nsk.share.Failure; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
32 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
33 |
/** |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
34 |
* Functions to set and wait for states in threads. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
35 |
* Used to sync main thread and debuggee thread. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
36 |
*/ |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
37 |
public class ThreadState { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
38 |
private final Lock lock = new ReentrantLock(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
39 |
private final Condition cond = lock.newCondition(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
40 |
private volatile String currentState; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
41 |
private long timeoutMs; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
42 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
43 |
public ThreadState(String startState, long timeoutMs) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
44 |
currentState = startState; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
45 |
this.timeoutMs = timeoutMs; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
46 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
47 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
48 |
/** |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
49 |
* Set new state. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
50 |
*/ |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
51 |
public void setState(String newState) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
52 |
lock.lock(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
53 |
try { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
54 |
log(MSG_SET_STATE, newState); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
55 |
currentState = newState; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
56 |
cond.signalAll(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
57 |
} finally { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
58 |
lock.unlock(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
59 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
60 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
61 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
62 |
/** |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
63 |
* Wait for the specified state. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
64 |
* Throws Failure if timeout. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
65 |
*/ |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
66 |
public void waitForState(String waitState) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
67 |
lock.lock(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
68 |
try { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
69 |
log(MSG_WAIT_STATE, waitState); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
70 |
while (!currentState.equals(waitState)) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
71 |
if (!cond.await(timeoutMs, TimeUnit.MILLISECONDS)) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
72 |
throw new Failure(format(MSG_TIMEOUT, waitState)); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
73 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
74 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
75 |
log(MSG_GOT_STATE, waitState); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
76 |
} catch (InterruptedException e) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
77 |
e.printStackTrace(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
78 |
throw new Failure(e); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
79 |
} finally { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
80 |
lock.unlock(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
81 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
82 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
83 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
84 |
/** |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
85 |
* Simple helper that sets a new state and then wait for another state. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
86 |
*/ |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
87 |
public void setAndWait(String newState, String waitState) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
88 |
setState(newState); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
89 |
waitForState(waitState); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
90 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
91 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
92 |
private static final String MSG_TIMEOUT = "ThreadState(thread='%s', state='%s') timeout waiting for %s"; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
93 |
private static final String MSG_SET_STATE = "ThreadState(thread='%s', state='%s') set state to %s"; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
94 |
private static final String MSG_WAIT_STATE = "ThreadState(thread='%s', state='%s') waiting for state %s"; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
95 |
private static final String MSG_GOT_STATE = "ThreadState(thread='%s', state='%s') got state %s"; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
96 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
97 |
private String format(String pattern, String state) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
98 |
final String threadName = Thread.currentThread().getName(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
99 |
return String.format(pattern, threadName, currentState, state); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
100 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
101 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
102 |
private void log(String pattern, String state) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
103 |
System.out.println(format(pattern, state)); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
104 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
105 |
} |