1 // Tags: JDK1.4 |
|
2 |
|
3 // Copyright (C) 2005 Free Software Foundation, Inc. |
|
4 // Written by Guilhem Lavaux (guilhem@kaffe.org) |
|
5 |
|
6 // This file is part of Mauve. |
|
7 |
|
8 // Mauve is free software; you can redistribute it and/or modify |
|
9 // it under the terms of the GNU General Public License as published by |
|
10 // the Free Software Foundation; either version 2, or (at your option) |
|
11 // any later version. |
|
12 |
|
13 // Mauve is distributed in the hope that it will be useful, |
|
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 // GNU General Public License for more details. |
|
17 |
|
18 // You should have received a copy of the GNU General Public License |
|
19 // along with Mauve; see the file COPYING. If not, write to |
|
20 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
21 // Boston, MA 02111-1307, USA. */ |
|
22 package gnu.testlet.java.nio.channels.SocketChannel; |
|
23 |
|
24 import java.net.*; |
|
25 import java.util.Set; |
|
26 import java.nio.*; |
|
27 import java.nio.channels.*; |
|
28 import java.io.OutputStream; |
|
29 import java.io.InputStream; |
|
30 |
|
31 import gnu.testlet.Testlet; |
|
32 import gnu.testlet.TestHarness; |
|
33 |
|
34 public class select implements Testlet |
|
35 { |
|
36 static final int testPort = 3487; |
|
37 |
|
38 public void test (TestHarness harness) |
|
39 { |
|
40 final Thread parentThread = Thread.currentThread(); |
|
41 |
|
42 Thread t = new Thread() { |
|
43 public void run() |
|
44 { |
|
45 try |
|
46 { |
|
47 Thread.sleep(10000); |
|
48 parentThread.interrupt(); |
|
49 } |
|
50 catch (InterruptedException e) |
|
51 { |
|
52 } |
|
53 } |
|
54 }; |
|
55 |
|
56 t.start(); |
|
57 |
|
58 try |
|
59 { |
|
60 ServerSocketChannel ssc = ServerSocketChannel.open(); |
|
61 Selector sel = Selector.open(); |
|
62 |
|
63 ssc.socket().bind(new InetSocketAddress(testPort)); |
|
64 |
|
65 SelectionKey ssc_key; |
|
66 |
|
67 ssc.configureBlocking(true); |
|
68 try |
|
69 { |
|
70 ssc_key = ssc.register(sel, SelectionKey.OP_ACCEPT, null); |
|
71 harness.fail("Channel must be in blocking mode for being able to register"); |
|
72 } |
|
73 catch (IllegalBlockingModeException e) |
|
74 { |
|
75 harness.check(true); |
|
76 } |
|
77 ssc.configureBlocking(false); |
|
78 ssc_key = ssc.register(sel, SelectionKey.OP_ACCEPT, null); |
|
79 |
|
80 Thread client_thread = new Thread() { |
|
81 public void run() |
|
82 { |
|
83 try |
|
84 { |
|
85 Socket s = new Socket(InetAddress.getLocalHost(), testPort); |
|
86 OutputStream o = s.getOutputStream(); |
|
87 InputStream i = s.getInputStream(); |
|
88 int val; |
|
89 |
|
90 o.write(12345678); |
|
91 val = i.read(); |
|
92 } |
|
93 catch (Exception _) |
|
94 { |
|
95 } |
|
96 } |
|
97 }; |
|
98 |
|
99 client_thread.start(); |
|
100 |
|
101 if (sel.select(1000) == 0) |
|
102 { |
|
103 harness.fail("Select on accept has failed"); |
|
104 return; |
|
105 } |
|
106 else |
|
107 harness.check(true); |
|
108 |
|
109 Set keys = sel.selectedKeys(); |
|
110 |
|
111 if (!keys.contains(ssc_key)) |
|
112 { |
|
113 harness.fail("The set does not contain the expected key"); |
|
114 return; |
|
115 } |
|
116 else |
|
117 harness.check(true); |
|
118 |
|
119 SocketChannel sc = ssc.accept(); |
|
120 |
|
121 sc.configureBlocking(false); |
|
122 |
|
123 SelectionKey sk = sc.register(sel, SelectionKey.OP_READ, null); |
|
124 ByteBuffer bb = ByteBuffer.allocate(1); |
|
125 |
|
126 if (sel.select(1000) == 0) |
|
127 { |
|
128 harness.fail("Select on read has failed"); |
|
129 return; |
|
130 } |
|
131 else |
|
132 harness.check(true); |
|
133 |
|
134 sc.read(bb); |
|
135 |
|
136 if (sel.select(100) != 0) |
|
137 { |
|
138 harness.fail("Select on timed out read failed"); |
|
139 return; |
|
140 } |
|
141 else |
|
142 harness.check(true); |
|
143 |
|
144 sk.interestOps(SelectionKey.OP_WRITE); |
|
145 if (sel.select(1000) == 0) |
|
146 { |
|
147 harness.fail("Select on write has failed"); |
|
148 return; |
|
149 } |
|
150 else |
|
151 harness.check(true); |
|
152 |
|
153 bb.flip(); |
|
154 sc.write(bb); |
|
155 sc.close(); |
|
156 ssc.close(); |
|
157 } |
|
158 catch (Exception e) |
|
159 { |
|
160 harness.fail("Unexpected exception " + e); |
|
161 harness.debug(e); |
|
162 } |
|
163 |
|
164 t.interrupt(); |
|
165 } |
|
166 } |
|
167 |
|