KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > InterruptTest


1 // $Id: InterruptTest.java,v 1.1 2007/07/04 07:29:33 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9
10 import java.net.DatagramPacket JavaDoc;
11 import java.net.DatagramSocket JavaDoc;
12 import java.net.InetAddress JavaDoc;
13
14
15 /**
16  * Tests Thread.interrupt() against InputStream.read(), Object.wait() and Thread.sleep()
17  *
18  * @author Bela Ban Oct 5 2001
19  */

20 public class InterruptTest extends TestCase {
21     static final long TIMEOUT=3000;
22     static final int SLEEP=1;
23     static final int WAIT=2;
24     static final int READ=3;
25     static final int SOCKET_READ=4;
26
27
28     public InterruptTest(String JavaDoc name) {
29         super(name);
30     }
31
32
33     String JavaDoc modeToString(int m) {
34         switch(m) {
35         case SLEEP:
36             return "SLEEP";
37         case WAIT:
38             return "WAIT";
39         case READ:
40             return "READ";
41         case SOCKET_READ:
42             return "SOCKET_READ";
43         default:
44             return "<unknown>";
45         }
46     }
47
48
49     /**
50      * Starts the Interruptible and interrupts after TIMEOUT milliseconds. Then joins thread
51      * (waiting for TIMEOUT msecs). PASS if thread dead, FAIL if still alive
52      */

53     public void testSleepInterrupt() {
54         SleeperThread thread=new SleeperThread(SLEEP);
55         runTest(thread);
56     }
57
58
59     public void testWaitInterrupt() {
60         SleeperThread thread=new SleeperThread(WAIT);
61         runTest(thread);
62     }
63
64 /* public void testSocketReadInterrupt() {
65         SleeperThread thread=new SleeperThread(SOCKET_READ);
66         runTest(thread);
67     }
68
69
70     public void testReadInterrupt() {
71         SleeperThread thread=new SleeperThread(READ);
72         runTest(thread);
73     }*/

74
75
76     void runTest(SleeperThread thread) {
77         System.out.println();
78         System.out.println("InterruptTest.runTest(" + modeToString(thread.getMode()) + "): starting other thread");
79         thread.start();
80         System.out.println("InterruptTest.runTest(" + modeToString(thread.getMode()) + "): starting other thread -- done");
81
82         System.out.println("InterruptTest.runTest(" + modeToString(thread.getMode()) + "): sleeping for " + TIMEOUT + " msecs");
83         sleep(TIMEOUT);
84         System.out.println("InterruptTest.runTest(" + modeToString(thread.getMode()) + "): sleeping -- done");
85
86         System.out.println("InterruptTest.runTest(" + modeToString(thread.getMode()) + "): interrupting other thread");
87         thread.interrupt();
88         System.out.println("InterruptTest.runTest(" + modeToString(thread.getMode()) + "): interrupting other thread -- done");
89
90         System.out.println("InterruptTest.runTest(" + modeToString(thread.getMode()) + "): joining other thread (timeout=" + TIMEOUT + " msecs");
91         try {
92             thread.join(TIMEOUT);
93         }
94         catch(InterruptedException JavaDoc e) {
95             e.printStackTrace();
96         }
97         System.out.println("InterruptTest.runTest(" + modeToString(thread.getMode()) + "): joining other thread -- done");
98
99         System.out.println("InterruptTest.runTest(" + modeToString(thread.getMode()) + "): thread.isAlive()=" + thread.isAlive());
100         assertTrue(!thread.isAlive());
101     }
102
103
104     void sleep(long msecs) {
105         try {
106             Thread.sleep(msecs);
107         }
108         catch(Exception JavaDoc ex) {
109             System.err.println("InterruptTest.sleep(): " + ex);
110         }
111     }
112
113
114
115
116
117     class SleeperThread extends Thread JavaDoc {
118         int mode;
119         DatagramSocket JavaDoc sock=null;
120
121
122         SleeperThread(int mode) {
123             this.mode=mode;
124         }
125
126
127         public int getMode() {
128             return mode;
129         }
130
131
132         public void run() {
133             switch(mode) {
134             case SLEEP:
135                 runSleep();
136                 break;
137             case WAIT:
138                 runWait();
139                 break;
140             case READ:
141                 runRead();
142                 break;
143             case SOCKET_READ:
144                 runSocketRead();
145                 break;
146             default:
147                 break;
148             }
149         }
150
151
152         void runSleep() {
153             try {
154                 Thread.sleep(TIMEOUT);
155             }
156             catch(InterruptedException JavaDoc ex) {
157                 System.err.println("InterruptTest.SleeperThread.runSleep(): " + ex);
158             }
159         }
160
161         void runWait() {
162             Object JavaDoc mutex=new Object JavaDoc();
163             synchronized(mutex) {
164                 try {
165                     mutex.wait();
166                 }
167                 catch(InterruptedException JavaDoc ex) {
168                     System.err.println("InterruptTest.SleeperThread.runWait(): " + ex);
169                 }
170             }
171         }
172
173         void runRead() {
174             try {
175                 System.in.read();
176             }
177             catch(Exception JavaDoc ex) {
178                 System.err.println("InterruptTest.SleeperThread.runRead(): " + ex);
179             }
180         }
181
182         void runSocketRead() {
183             byte[] buf=new byte[2];
184             DatagramPacket JavaDoc packet;
185
186             try {
187                 sock=new DatagramSocket JavaDoc(12345, InetAddress.getLocalHost());
188                 // System.out.println("** mcast_sock=" + mcast_sock.getLocalAddress() + ":" + mcast_sock.getLocalPort());
189
packet=new DatagramPacket JavaDoc(buf, buf.length);
190                 //System.out.println("** receive(): start");
191
sock.receive(packet);
192                 //System.out.println("** receive(): done");
193
}
194             catch(Exception JavaDoc e) {
195                 //System.out.println("** receive(): done, exception=" + e);
196
System.err.println(e);
197             }
198         }
199     }
200
201
202     public static Test suite() {
203         TestSuite s=new TestSuite(InterruptTest.class);
204         return s;
205     }
206
207     public static void main(String JavaDoc[] args) {
208         junit.textui.TestRunner.run(suite());
209     }
210 }
211
212
213
Popular Tags