1 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 ; 11 import java.net.DatagramSocket ; 12 import java.net.InetAddress ; 13 14 15 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 name) { 29 super(name); 30 } 31 32 33 String 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 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 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 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 ex) { 109 System.err.println("InterruptTest.sleep(): " + ex); 110 } 111 } 112 113 114 115 116 117 class SleeperThread extends Thread { 118 int mode; 119 DatagramSocket 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 ex) { 157 System.err.println("InterruptTest.SleeperThread.runSleep(): " + ex); 158 } 159 } 160 161 void runWait() { 162 Object mutex=new Object (); 163 synchronized(mutex) { 164 try { 165 mutex.wait(); 166 } 167 catch(InterruptedException 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 ex) { 178 System.err.println("InterruptTest.SleeperThread.runRead(): " + ex); 179 } 180 } 181 182 void runSocketRead() { 183 byte[] buf=new byte[2]; 184 DatagramPacket packet; 185 186 try { 187 sock=new DatagramSocket (12345, InetAddress.getLocalHost()); 188 packet=new DatagramPacket (buf, buf.length); 190 sock.receive(packet); 192 } 194 catch(Exception e) { 195 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 [] args) { 208 junit.textui.TestRunner.run(suite()); 209 } 210 } 211 212 213 | Popular Tags |