001: // $Id: InterruptTest.java,v 1.6 2006/10/25 07:05:02 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008:
009: import java.net.DatagramPacket;
010: import java.net.DatagramSocket;
011: import java.net.InetAddress;
012:
013: /**
014: * Tests Thread.interrupt() against InputStream.read(), Object.wait() and Thread.sleep()
015: *
016: * @author Bela Ban Oct 5 2001
017: */
018: public class InterruptTest extends TestCase {
019: static final long TIMEOUT = 3000;
020: static final int SLEEP = 1;
021: static final int WAIT = 2;
022: static final int READ = 3;
023: static final int SOCKET_READ = 4;
024:
025: public InterruptTest(String name) {
026: super (name);
027: }
028:
029: String modeToString(int m) {
030: switch (m) {
031: case SLEEP:
032: return "SLEEP";
033: case WAIT:
034: return "WAIT";
035: case READ:
036: return "READ";
037: case SOCKET_READ:
038: return "SOCKET_READ";
039: default:
040: return "<unknown>";
041: }
042: }
043:
044: /**
045: * Starts the Interruptible and interrupts after TIMEOUT milliseconds. Then joins thread
046: * (waiting for TIMEOUT msecs). PASS if thread dead, FAIL if still alive
047: */
048: public void testSleepInterrupt() {
049: SleeperThread thread = new SleeperThread(SLEEP);
050: runTest(thread);
051: }
052:
053: public void testWaitInterrupt() {
054: SleeperThread thread = new SleeperThread(WAIT);
055: runTest(thread);
056: }
057:
058: /* public void testSocketReadInterrupt() {
059: SleeperThread thread=new SleeperThread(SOCKET_READ);
060: runTest(thread);
061: }
062:
063:
064: public void testReadInterrupt() {
065: SleeperThread thread=new SleeperThread(READ);
066: runTest(thread);
067: }*/
068:
069: void runTest(SleeperThread thread) {
070: System.out.println();
071: System.out.println("InterruptTest.runTest("
072: + modeToString(thread.getMode())
073: + "): starting other thread");
074: thread.start();
075: System.out.println("InterruptTest.runTest("
076: + modeToString(thread.getMode())
077: + "): starting other thread -- done");
078:
079: System.out.println("InterruptTest.runTest("
080: + modeToString(thread.getMode()) + "): sleeping for "
081: + TIMEOUT + " msecs");
082: sleep(TIMEOUT);
083: System.out.println("InterruptTest.runTest("
084: + modeToString(thread.getMode())
085: + "): sleeping -- done");
086:
087: System.out.println("InterruptTest.runTest("
088: + modeToString(thread.getMode())
089: + "): interrupting other thread");
090: thread.interrupt();
091: System.out.println("InterruptTest.runTest("
092: + modeToString(thread.getMode())
093: + "): interrupting other thread -- done");
094:
095: System.out.println("InterruptTest.runTest("
096: + modeToString(thread.getMode())
097: + "): joining other thread (timeout=" + TIMEOUT
098: + " msecs");
099: try {
100: thread.join(TIMEOUT);
101: } catch (InterruptedException e) {
102: e.printStackTrace();
103: }
104: System.out.println("InterruptTest.runTest("
105: + modeToString(thread.getMode())
106: + "): joining other thread -- done");
107:
108: System.out.println("InterruptTest.runTest("
109: + modeToString(thread.getMode())
110: + "): thread.isAlive()=" + thread.isAlive());
111: assertTrue(!thread.isAlive());
112: }
113:
114: void sleep(long msecs) {
115: try {
116: Thread.sleep(msecs);
117: } catch (Exception ex) {
118: System.err.println("InterruptTest.sleep(): " + ex);
119: }
120: }
121:
122: class SleeperThread extends Thread {
123: int mode;
124: DatagramSocket sock = null;
125:
126: SleeperThread(int mode) {
127: this .mode = mode;
128: }
129:
130: public int getMode() {
131: return mode;
132: }
133:
134: public void run() {
135: switch (mode) {
136: case SLEEP:
137: runSleep();
138: break;
139: case WAIT:
140: runWait();
141: break;
142: case READ:
143: runRead();
144: break;
145: case SOCKET_READ:
146: runSocketRead();
147: break;
148: default:
149: break;
150: }
151: }
152:
153: void runSleep() {
154: try {
155: Thread.sleep(TIMEOUT);
156: } catch (InterruptedException ex) {
157: System.err
158: .println("InterruptTest.SleeperThread.runSleep(): "
159: + ex);
160: }
161: }
162:
163: void runWait() {
164: Object mutex = new Object();
165: synchronized (mutex) {
166: try {
167: mutex.wait();
168: } catch (InterruptedException ex) {
169: System.err
170: .println("InterruptTest.SleeperThread.runWait(): "
171: + ex);
172: }
173: }
174: }
175:
176: void runRead() {
177: try {
178: System.in.read();
179: } catch (Exception ex) {
180: System.err
181: .println("InterruptTest.SleeperThread.runRead(): "
182: + ex);
183: }
184: }
185:
186: void runSocketRead() {
187: byte[] buf = new byte[2];
188: DatagramPacket packet;
189:
190: try {
191: sock = new DatagramSocket(12345, InetAddress
192: .getLocalHost());
193: // System.out.println("** mcast_sock=" + mcast_sock.getLocalAddress() + ":" + mcast_sock.getLocalPort());
194: packet = new DatagramPacket(buf, buf.length);
195: //System.out.println("** receive(): start");
196: sock.receive(packet);
197: //System.out.println("** receive(): done");
198: } catch (Exception e) {
199: //System.out.println("** receive(): done, exception=" + e);
200: System.err.println(e);
201: }
202: }
203: }
204:
205: public static Test suite() {
206: TestSuite s = new TestSuite(InterruptTest.class);
207: return s;
208: }
209:
210: public static void main(String[] args) {
211: junit.textui.TestRunner.run(suite());
212: }
213: }
|