001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.drlvm.tests.regression.h3341;
019:
020: import junit.framework.TestCase;
021:
022: /**
023: * Test case for StopThread() jvmti function applied to thread that hangs on
024: * Object.wait().
025: * Checks that StopThread() forces target thread to exit wait() and throw the
026: * requested exception immediately before the wait() timeout is over.
027: * Since we test thread synchronization methods behaviour the test doesn't use
028: * synchronization object's for it's own purposes. Thus It's execution is based
029: * on timeouts.
030: */
031: public class StopThreadInWait extends TestCase {
032:
033: static final int TIMEOUT_FOR_WAIT = 10000;
034: static final int TIMEOUT_BEFORE_STOPPING = 1000;
035: static final int TIMEOUT_FOR_EXCEPTION = 1000;
036:
037: static boolean tooLate = false;
038:
039: public static void main(String args[]) {
040: (new StopThreadInWait()).test();
041: }
042:
043: public void test() {
044: Thread waitingThread = new WaitingThread();
045: waitingThread.start();
046:
047: try {
048: Thread.sleep(TIMEOUT_BEFORE_STOPPING);
049: try {
050: System.err.println("[Java]: Throwing an exception");
051: // pass execution to the agent
052: throw new InvokeAgentException(waitingThread,
053: new ThreadDeath());
054: } catch (Exception e) {
055: System.err.println("[Java]: Exception caught");
056: }
057:
058: Thread.sleep(TIMEOUT_FOR_EXCEPTION);
059: tooLate = true;
060:
061: System.err.println("[Java]: joining test_thread...");
062: waitingThread.join();
063: } catch (InterruptedException exc) {
064: exc.printStackTrace();
065: }
066:
067: System.err.println("[Java]: test done");
068: assertTrue(Status.status);
069: }
070: }
071:
072: class WaitingThread extends Thread {
073:
074: public WaitingThread() {
075: super ();
076: }
077:
078: public void run() {
079: Object lock = new Object();
080:
081: synchronized (lock) {
082: try {
083: System.out
084: .println("[java]: test thread falling in wait");
085: lock.wait(StopThreadInWait.TIMEOUT_FOR_WAIT);
086: } catch (ThreadDeath exc) {
087: if (!StopThreadInWait.tooLate) {
088: Status.status = true;
089:
090: System.out
091: .println("[java]: test thread has caught "
092: + "ThreadDeath exception in time");
093: } else {
094: System.out
095: .println("[java]: test thread has caught "
096: + "ThreadDeath exception too late");
097: }
098: } catch (Throwable exc) {
099: exc.printStackTrace();
100: }
101: }
102: }
103: };
104:
105: class InvokeAgentException extends Exception {
106:
107: Thread thread;
108: Throwable stopException;
109:
110: InvokeAgentException(Thread thread, Throwable exception) {
111: this .thread = thread;
112: stopException = exception;
113: }
114: }
115:
116: class Status {
117: /** the field should be modified by jvmti agent to determine test result. */
118: public static boolean status = false;
119: }
|