5080203: TEST_BUG: ThreadStateTest fails intermittently
Retry a few times to check thread status before reporting failure Reviewed-by: swamyv
This commit is contained in:
parent
bb698a2b6f
commit
354731edb5
@ -24,14 +24,13 @@
|
|||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @bug 4967283 5080203
|
* @bug 4967283 5080203
|
||||||
* @ignore Due to 5080203, cannot rely on this test always passing.
|
|
||||||
* @summary Basic unit test of thread states returned by
|
* @summary Basic unit test of thread states returned by
|
||||||
* ThreadMXBean.getThreadInfo.getThreadState().
|
* ThreadMXBean.getThreadInfo.getThreadState().
|
||||||
* It also tests lock information returned by ThreadInfo.
|
* It also tests lock information returned by ThreadInfo.
|
||||||
*
|
*
|
||||||
* @author Mandy Chung
|
* @author Mandy Chung
|
||||||
*
|
*
|
||||||
* @build ThreadExecutionSynchronizer
|
* @build ThreadExecutionSynchronizer Utils
|
||||||
* @run main ThreadStateTest
|
* @run main ThreadStateTest
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -43,7 +42,6 @@ import java.util.concurrent.locks.LockSupport;
|
|||||||
|
|
||||||
public class ThreadStateTest {
|
public class ThreadStateTest {
|
||||||
private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean();
|
private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean();
|
||||||
private static boolean testFailed = false;
|
|
||||||
|
|
||||||
static class Lock {
|
static class Lock {
|
||||||
private String name;
|
private String name;
|
||||||
@ -64,32 +62,32 @@ public class ThreadStateTest {
|
|||||||
MyThread myThread = new MyThread("MyThread");
|
MyThread myThread = new MyThread("MyThread");
|
||||||
|
|
||||||
// before myThread starts
|
// before myThread starts
|
||||||
// checkThreadState(myThread, Thread.State.NEW);
|
// Utils.checkThreadState(myThread, Thread.State.NEW);
|
||||||
|
|
||||||
myThread.start();
|
myThread.start();
|
||||||
myThread.waitUntilStarted();
|
myThread.waitUntilStarted();
|
||||||
checkThreadState(myThread, Thread.State.RUNNABLE);
|
Utils.checkThreadState(myThread, Thread.State.RUNNABLE);
|
||||||
checkLockInfo(myThread, Thread.State.RUNNABLE, null, null);
|
checkLockInfo(myThread, Thread.State.RUNNABLE, null, null);
|
||||||
|
|
||||||
myThread.suspend();
|
myThread.suspend();
|
||||||
goSleep(10);
|
Utils.goSleep(10);
|
||||||
checkSuspendedThreadState(myThread, Thread.State.RUNNABLE);
|
checkSuspendedThreadState(myThread, Thread.State.RUNNABLE);
|
||||||
myThread.resume();
|
myThread.resume();
|
||||||
|
|
||||||
synchronized (globalLock) {
|
synchronized (globalLock) {
|
||||||
myThread.goBlocked();
|
myThread.goBlocked();
|
||||||
checkThreadState(myThread, Thread.State.BLOCKED);
|
Utils.checkThreadState(myThread, Thread.State.BLOCKED);
|
||||||
checkLockInfo(myThread, Thread.State.BLOCKED,
|
checkLockInfo(myThread, Thread.State.BLOCKED,
|
||||||
globalLock, Thread.currentThread());
|
globalLock, Thread.currentThread());
|
||||||
}
|
}
|
||||||
|
|
||||||
myThread.goWaiting();
|
myThread.goWaiting();
|
||||||
checkThreadState(myThread, Thread.State.WAITING);
|
Utils.checkThreadState(myThread, Thread.State.WAITING);
|
||||||
checkLockInfo(myThread, Thread.State.WAITING,
|
checkLockInfo(myThread, Thread.State.WAITING,
|
||||||
globalLock, null);
|
globalLock, null);
|
||||||
|
|
||||||
myThread.goTimedWaiting();
|
myThread.goTimedWaiting();
|
||||||
checkThreadState(myThread, Thread.State.TIMED_WAITING);
|
Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING);
|
||||||
checkLockInfo(myThread, Thread.State.TIMED_WAITING,
|
checkLockInfo(myThread, Thread.State.TIMED_WAITING,
|
||||||
globalLock, null);
|
globalLock, null);
|
||||||
|
|
||||||
@ -102,32 +100,30 @@ public class ThreadStateTest {
|
|||||||
Bug ID : 5062095
|
Bug ID : 5062095
|
||||||
***********************************************
|
***********************************************
|
||||||
myThread.goParked();
|
myThread.goParked();
|
||||||
checkThreadState(myThread, Thread.State.WAITING);
|
Utils.checkThreadState(myThread, Thread.State.WAITING);
|
||||||
checkLockInfo(myThread, Thread.State.WAITING, null, null);
|
checkLockInfo(myThread, Thread.State.WAITING, null, null);
|
||||||
|
|
||||||
myThread.goTimedParked();
|
myThread.goTimedParked();
|
||||||
checkThreadState(myThread, Thread.State.TIMED_WAITING);
|
Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING);
|
||||||
checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null);
|
checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null);
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
myThread.goSleeping();
|
myThread.goSleeping();
|
||||||
checkThreadState(myThread, Thread.State.TIMED_WAITING);
|
Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING);
|
||||||
checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null);
|
checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null);
|
||||||
|
|
||||||
|
|
||||||
myThread.terminate();
|
myThread.terminate();
|
||||||
// checkThreadState(myThread, ThreadState.TERMINATED);
|
// Utils.checkThreadState(myThread, ThreadState.TERMINATED);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
myThread.join();
|
myThread.join();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.out.println("Unexpected exception.");
|
System.out.println("TEST FAILED: Unexpected exception.");
|
||||||
testFailed = true;
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
if (testFailed)
|
|
||||||
throw new RuntimeException("TEST FAILED.");
|
|
||||||
System.out.println("Test passed.");
|
System.out.println("Test passed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,32 +144,7 @@ public class ThreadStateTest {
|
|||||||
throw new RuntimeException(t.getName() + " expected to be suspended " +
|
throw new RuntimeException(t.getName() + " expected to be suspended " +
|
||||||
" but isSuspended() returns " + info.isSuspended());
|
" but isSuspended() returns " + info.isSuspended());
|
||||||
}
|
}
|
||||||
checkThreadState(t, state);
|
Utils.checkThreadState(t, state);
|
||||||
}
|
|
||||||
|
|
||||||
private static void checkThreadState(Thread t, Thread.State expected) {
|
|
||||||
ThreadInfo ti = tm.getThreadInfo(t.getId());
|
|
||||||
Thread.State state = ti.getThreadState();
|
|
||||||
if (state == null) {
|
|
||||||
throw new RuntimeException(t.getName() + " expected to have " +
|
|
||||||
expected + " but got null.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state != expected) {
|
|
||||||
if (expected == Thread.State.BLOCKED) {
|
|
||||||
int retryCount=0;
|
|
||||||
while (ti.getThreadState() != expected) {
|
|
||||||
if (retryCount >= 500) {
|
|
||||||
throw new RuntimeException(t.getName() +
|
|
||||||
" expected to have " + expected + " but got " + state);
|
|
||||||
}
|
|
||||||
goSleep(100);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new RuntimeException(t.getName() + " expected to have " +
|
|
||||||
expected + " but got " + state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getLockName(Object lock) {
|
private static String getLockName(Object lock) {
|
||||||
@ -250,16 +221,6 @@ public class ThreadStateTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void goSleep(long ms) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(ms);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
System.out.println("Unexpected exception.");
|
|
||||||
testFailed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class MyThread extends Thread {
|
static class MyThread extends Thread {
|
||||||
private ThreadExecutionSynchronizer thrsync = new ThreadExecutionSynchronizer();
|
private ThreadExecutionSynchronizer thrsync = new ThreadExecutionSynchronizer();
|
||||||
|
|
||||||
@ -335,7 +296,7 @@ public class ThreadStateTest {
|
|||||||
LockSupport.park();
|
LockSupport.park();
|
||||||
// give a chance for the main thread to block
|
// give a chance for the main thread to block
|
||||||
System.out.println(" myThread is going to park.");
|
System.out.println(" myThread is going to park.");
|
||||||
goSleep(10);
|
Utils.goSleep(10);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TIMED_PARKED: {
|
case TIMED_PARKED: {
|
||||||
@ -346,7 +307,7 @@ public class ThreadStateTest {
|
|||||||
LockSupport.parkUntil(deadline);
|
LockSupport.parkUntil(deadline);
|
||||||
|
|
||||||
// give a chance for the main thread to block
|
// give a chance for the main thread to block
|
||||||
goSleep(10);
|
Utils.goSleep(10);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SLEEPING: {
|
case SLEEPING: {
|
||||||
@ -375,7 +336,7 @@ public class ThreadStateTest {
|
|||||||
public void waitUntilStarted() {
|
public void waitUntilStarted() {
|
||||||
// wait for MyThread.
|
// wait for MyThread.
|
||||||
thrsync.waitForSignal();
|
thrsync.waitForSignal();
|
||||||
goSleep(10);
|
Utils.goSleep(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void goBlocked() {
|
public void goBlocked() {
|
||||||
@ -383,7 +344,7 @@ public class ThreadStateTest {
|
|||||||
setState(BLOCKED);
|
setState(BLOCKED);
|
||||||
// wait for MyThread to get blocked
|
// wait for MyThread to get blocked
|
||||||
thrsync.waitForSignal();
|
thrsync.waitForSignal();
|
||||||
goSleep(20);
|
Utils.goSleep(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void goWaiting() {
|
public void goWaiting() {
|
||||||
@ -391,28 +352,28 @@ public class ThreadStateTest {
|
|||||||
setState(WAITING);
|
setState(WAITING);
|
||||||
// wait for MyThread to wait on object.
|
// wait for MyThread to wait on object.
|
||||||
thrsync.waitForSignal();
|
thrsync.waitForSignal();
|
||||||
goSleep(20);
|
Utils.goSleep(20);
|
||||||
}
|
}
|
||||||
public void goTimedWaiting() {
|
public void goTimedWaiting() {
|
||||||
System.out.println("Waiting myThread to go timed waiting.");
|
System.out.println("Waiting myThread to go timed waiting.");
|
||||||
setState(TIMED_WAITING);
|
setState(TIMED_WAITING);
|
||||||
// wait for MyThread timed wait call.
|
// wait for MyThread timed wait call.
|
||||||
thrsync.waitForSignal();
|
thrsync.waitForSignal();
|
||||||
goSleep(20);
|
Utils.goSleep(20);
|
||||||
}
|
}
|
||||||
public void goParked() {
|
public void goParked() {
|
||||||
System.out.println("Waiting myThread to go parked.");
|
System.out.println("Waiting myThread to go parked.");
|
||||||
setState(PARKED);
|
setState(PARKED);
|
||||||
// wait for MyThread state change to PARKED.
|
// wait for MyThread state change to PARKED.
|
||||||
thrsync.waitForSignal();
|
thrsync.waitForSignal();
|
||||||
goSleep(20);
|
Utils.goSleep(20);
|
||||||
}
|
}
|
||||||
public void goTimedParked() {
|
public void goTimedParked() {
|
||||||
System.out.println("Waiting myThread to go timed parked.");
|
System.out.println("Waiting myThread to go timed parked.");
|
||||||
setState(TIMED_PARKED);
|
setState(TIMED_PARKED);
|
||||||
// wait for MyThread.
|
// wait for MyThread.
|
||||||
thrsync.waitForSignal();
|
thrsync.waitForSignal();
|
||||||
goSleep(20);
|
Utils.goSleep(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void goSleeping() {
|
public void goSleeping() {
|
||||||
@ -420,21 +381,21 @@ public class ThreadStateTest {
|
|||||||
setState(SLEEPING);
|
setState(SLEEPING);
|
||||||
// wait for MyThread.
|
// wait for MyThread.
|
||||||
thrsync.waitForSignal();
|
thrsync.waitForSignal();
|
||||||
goSleep(20);
|
Utils.goSleep(20);
|
||||||
}
|
}
|
||||||
public void terminate() {
|
public void terminate() {
|
||||||
System.out.println("Waiting myThread to terminate.");
|
System.out.println("Waiting myThread to terminate.");
|
||||||
setState(TERMINATE);
|
setState(TERMINATE);
|
||||||
// wait for MyThread.
|
// wait for MyThread.
|
||||||
thrsync.waitForSignal();
|
thrsync.waitForSignal();
|
||||||
goSleep(20);
|
Utils.goSleep(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setState(int newState) {
|
private void setState(int newState) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case BLOCKED:
|
case BLOCKED:
|
||||||
while (state == BLOCKED) {
|
while (state == BLOCKED) {
|
||||||
goSleep(20);
|
Utils.goSleep(20);
|
||||||
}
|
}
|
||||||
state = newState;
|
state = newState;
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user