GNU header update.
[cacao.git] / tests / kaffe / SoInterrupt.java
1 /**
2  * Test interruptable I/O.
3  *
4  * Note that this test will not run under Sun's JDK1.2 on Solaris, see
5  * http://developer.java.sun.com/developer/bugParade/bugs/4178050.html
6  *
7  * It's unlikely to work on Windows either, see
8  * http://developer.java.sun.com/developer/bugParade/bugs/4154947.html
9  *
10  * It it supported by Kaffe's jthreads, though it may not work under other
11  * threading systems either.  XXX
12  *
13  * @author Godmar Back <gback@cs.utah.edu>
14  */
15 import java.net.*;
16 import java.io.*;
17
18 public class SoInterrupt {
19     public static void main(String av[]) throws Exception {
20         final boolean verbose = av.length > 0;
21         final String foo = "foo";
22         final Thread main = Thread.currentThread();
23
24         int tryport = 45054;
25         ServerSocket server;
26         for(;;++tryport) {
27             try {
28                 server = new ServerSocket(tryport);
29                 break;
30             } catch (IOException _) {}
31         }
32         final int port = tryport;
33         Thread watchdog = new Thread() {
34             public void run() {
35                 try {
36                     Thread.sleep(10 * 1000);
37                 } catch (InterruptedException _) { }
38                 System.out.println("Watchdog Failure: Time out.");
39                 System.exit(1);
40             }
41         };
42         watchdog.start();
43
44         Thread t = new Thread() {
45             public void run() {
46                 try {
47                     // let main thread wait a while
48                     try {
49                         Thread.sleep(1000);
50                     } catch (InterruptedException e) {
51                         System.out.println("Failure " + e);
52                     }
53                     if (verbose)
54                         System.out.println("interrupting " + main);
55                     // interrupt it
56                     main.interrupt();
57                     // give it some more time
58                     try {
59                         Thread.sleep(1000);
60                     } catch (InterruptedException e) {
61                         System.out.println("Failure " + e);
62                     }
63
64                     // now connect
65                     if (verbose)
66                         System.out.println("connecting...");
67                     Socket s = new Socket(InetAddress.getByName(null), port);
68                     // wait some
69                     try {
70                         Thread.sleep(1000);
71                     } catch (InterruptedException e) {
72                         System.out.println("Failure " + e);
73                     }
74
75                     if (verbose)
76                         System.out.println("interrupting again " + main);
77                     // interrupt it again
78                     main.interrupt();
79                     // wait some more
80                     try {
81                         Thread.sleep(1000);
82                     } catch (InterruptedException e) {
83                         System.out.println("Failure " + e);
84                     }
85
86                     // now finish up
87                     PrintWriter p = new PrintWriter(s.getOutputStream());
88                     p.println(foo);
89                     p.close();
90                 } catch (Exception e) {
91                     System.out.println("Failure " + e);
92                 }
93             }
94         };
95         t.start();
96         Socket rsocket = null;
97         try {
98             if (verbose)
99                 System.out.println("waiting for client...");
100             rsocket = server.accept(); 
101             if (verbose)
102                 System.out.println("accepted..." + rsocket);
103         } catch (InterruptedIOException e) {
104             if (verbose)
105                 System.out.println(e);
106             System.out.println("Success 1.");
107         }
108         if (verbose)
109             System.out.println("waiting for client again...");
110         rsocket = server.accept(); 
111         System.out.println("Success 2.");
112         InputStream is = rsocket.getInputStream();
113         LineNumberReader r = new LineNumberReader(new InputStreamReader(is));
114         byte []b = null;
115         try {
116             r.readLine();
117         } catch (InterruptedIOException e) {
118             // System.out.println(e);
119             System.out.println("Success 3.");
120         }
121         String s = r.readLine();
122         if (s.equals(foo)) {
123             System.out.println("Success 4.");
124         } else {
125             System.out.println("Failure: `" + s + "'");
126         }
127         System.exit(0);
128     }
129 }
130
131 /* Expected Output:
132 Success 1.
133 Success 2.
134 Success 3.
135 Success 4.
136 */