001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util;
038:
039: import junit.framework.*;
040:
041: /**
042: * Test case for CompletionMonitorTest
043: */
044: public class CompletionMonitorTest extends TestCase {
045:
046: private boolean _shouldInterrupt;
047:
048: public void testDegenerateSignal() {
049:
050: CompletionMonitor as = new CompletionMonitor(true);
051: assertTrue("Flag should start out as true", as.isFlag());
052: //This thread will interrupt the main thread if it hangs
053:
054: ThreadInterrupter interrupter = new ThreadInterrupter();
055: interrupter.start();
056:
057: assertTrue(
058: "WaitOne hung, and was interrupted by the failsafe.",
059: as.waitOne());
060: interrupter.targetCompleted();
061: }
062:
063: public void testRealSignal() {
064: final CompletionMonitor as = new CompletionMonitor(false);
065: Thread worker = new Thread() {
066: public void run() {
067: try {
068: Thread.sleep(50);
069: as.set();
070: } catch (InterruptedException e) {
071: }
072: }
073: };
074: worker.start();
075: assertTrue("WaitOne hung", as.waitOne());
076: assertTrue(as.waitOne());
077: as.reset();
078: assertFalse("Reset failed to do its job", as.isFlag());
079: }
080:
081: private static class ThreadInterrupter {
082:
083: private Object _lock = new Object();
084:
085: private int _timeout;
086: private boolean _targetComplete;
087:
088: private Thread _target;
089:
090: private Thread _interrupter = new Thread() {
091: public void run() {
092: synchronized (_lock) {
093: try {
094: // This is an 'if' rather than a 'while' because we do not want to wait beyond
095: // _timeout milliseconds if the target fails to complete
096:
097: if (!_targetComplete)
098: _lock.wait(_timeout);
099: } catch (InterruptedException e) {
100: // Do nothing if interrupted. simply go to the finally block
101: } finally {
102: // This code may be called due to the timeout on the wait
103: // or by an interruption by the target thread.
104: if (!_targetComplete)
105: _target.interrupt();
106: }
107: }
108: }
109: };
110:
111: public ThreadInterrupter() {
112: this (Thread.currentThread(), 50);
113: }
114:
115: public ThreadInterrupter(int timeout) {
116: this (Thread.currentThread(), timeout);
117: }
118:
119: public ThreadInterrupter(Thread target, int timeout) {
120: _target = target;
121: _timeout = timeout;
122: _targetComplete = false;
123: }
124:
125: public void start() {
126: _interrupter.start();
127: }
128:
129: public void targetCompleted() {
130: synchronized (_lock) {
131: _targetComplete = true;
132: _lock.notify();
133: }
134: }
135: }
136: }
|