33 * socket state and replies back to this class via an out-of-band |
33 * socket state and replies back to this class via an out-of-band |
34 * channel. |
34 * channel. |
35 */ |
35 */ |
36 import java.io.IOException; |
36 import java.io.IOException; |
37 import java.net.InetSocketAddress; |
37 import java.net.InetSocketAddress; |
|
38 import java.net.InetAddress; |
38 import java.nio.ByteBuffer; |
39 import java.nio.ByteBuffer; |
39 import java.nio.channels.DatagramChannel; |
40 import java.nio.channels.DatagramChannel; |
40 import java.nio.channels.SelectionKey; |
41 import java.nio.channels.SelectionKey; |
41 import java.nio.channels.Selector; |
42 import java.nio.channels.Selector; |
42 import java.nio.channels.ServerSocketChannel; |
43 import java.nio.channels.ServerSocketChannel; |
64 SocketChannel sc; |
65 SocketChannel sc; |
65 |
66 |
66 /* |
67 /* |
67 * Wait for service to connect |
68 * Wait for service to connect |
68 */ |
69 */ |
|
70 System.err.println("Waiting for the service to connect"); |
69 ssc.configureBlocking(false); |
71 ssc.configureBlocking(false); |
70 sk = ssc.register(sel, SelectionKey.OP_ACCEPT); |
72 sk = ssc.register(sel, SelectionKey.OP_ACCEPT); |
71 long to = Utils.adjustTimeout(15*1000); |
73 long to = Utils.adjustTimeout(15*1000); |
72 sc = null; |
74 sc = null; |
73 for (;;) { |
75 for (;;) { |
87 ssc.configureBlocking(false); |
89 ssc.configureBlocking(false); |
88 |
90 |
89 /* |
91 /* |
90 * Wait for service to report test result |
92 * Wait for service to report test result |
91 */ |
93 */ |
|
94 System.err.println("Waiting for the service to report test result"); |
92 sc.configureBlocking(false); |
95 sc.configureBlocking(false); |
93 sk = sc.register(sel, SelectionKey.OP_READ); |
96 sk = sc.register(sel, SelectionKey.OP_READ); |
94 to = Utils.adjustTimeout(5000); |
97 to = Utils.adjustTimeout(5000); |
95 ByteBuffer bb = ByteBuffer.allocateDirect(20); |
98 ByteBuffer bb = ByteBuffer.allocateDirect(20); |
96 for (;;) { |
99 for (;;) { |
109 to -= System.currentTimeMillis() - st; |
112 to -= System.currentTimeMillis() - st; |
110 if (to <= 0) { |
113 if (to <= 0) { |
111 throw new IOException("Timed out waiting for service to report test result"); |
114 throw new IOException("Timed out waiting for service to report test result"); |
112 } |
115 } |
113 } |
116 } |
|
117 System.err.println("Cleaning up"); |
114 sk.cancel(); |
118 sk.cancel(); |
115 sc.close(); |
119 sc.close(); |
116 sel.close(); |
120 sel.close(); |
117 |
121 |
118 /* |
122 /* |
119 * Examine the test result |
123 * Examine the test result |
120 */ |
124 */ |
|
125 System.err.println("Examine test result"); |
121 bb.flip(); |
126 bb.flip(); |
122 byte b = bb.get(); |
127 byte b = bb.get(); |
123 |
128 |
124 if (expectFail && b == 'P') { |
129 if (expectFail && b == 'P') { |
125 System.err.println("Test passed - test is expected to fail!!!"); |
130 System.err.println("Test passed - test is expected to fail!!!"); |
150 /* |
155 /* |
151 * Create the listener which will be used to read the test result |
156 * Create the listener which will be used to read the test result |
152 * from the service. |
157 * from the service. |
153 */ |
158 */ |
154 ServerSocketChannel ssc = ServerSocketChannel.open(); |
159 ServerSocketChannel ssc = ServerSocketChannel.open(); |
155 ssc.socket().bind(new InetSocketAddress(0)); |
160 ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); |
|
161 System.err.println("Listener bound to: " + ssc.socket().getLocalSocketAddress()); |
156 |
162 |
157 /* |
163 /* |
158 * The port is passed to the service as an argument. |
164 * The port is passed to the service as an argument. |
159 */ |
165 */ |
160 int port = ssc.socket().getLocalPort(); |
166 int port = ssc.socket().getLocalPort(); |
162 arg[0] = String.valueOf(port); |
168 arg[0] = String.valueOf(port); |
163 |
169 |
164 /* |
170 /* |
165 * Launch service with a SocketChannel (tcp nowait) |
171 * Launch service with a SocketChannel (tcp nowait) |
166 */ |
172 */ |
|
173 System.err.println("launchWithSocketChannel"); |
167 SocketChannel sc = Launcher.launchWithSocketChannel(TEST_SERVICE, options, arg); |
174 SocketChannel sc = Launcher.launchWithSocketChannel(TEST_SERVICE, options, arg); |
|
175 System.err.println("Waiting for test results"); |
168 waitForTestResult(ssc, expectFail); |
176 waitForTestResult(ssc, expectFail); |
169 sc.close(); |
177 sc.close(); |
170 |
178 |
171 /* |
179 /* |
172 * Launch service with a ServerSocketChannel (tcp wait) |
180 * Launch service with a ServerSocketChannel (tcp wait) |
173 * launchWithServerSocketChannel establishes a connection to the service |
181 * launchWithServerSocketChannel establishes a connection to the service |
174 * and the returned SocketChannel is connected to the service. |
182 * and the returned SocketChannel is connected to the service. |
175 */ |
183 */ |
|
184 System.err.println("launchWithServerSocketChannel"); |
176 sc = Launcher.launchWithServerSocketChannel(TEST_SERVICE, options, arg); |
185 sc = Launcher.launchWithServerSocketChannel(TEST_SERVICE, options, arg); |
177 waitForTestResult(ssc, expectFail); |
186 waitForTestResult(ssc, expectFail); |
178 sc.close(); |
187 sc.close(); |
179 |
188 |
180 /* |
189 /* |
181 * Launch service with a DatagramChannel (udp wait) |
190 * Launch service with a DatagramChannel (udp wait) |
182 */ |
191 */ |
|
192 System.err.println("launchWithDatagramChannel"); |
183 DatagramChannel dc = Launcher.launchWithDatagramChannel(TEST_SERVICE, options, arg); |
193 DatagramChannel dc = Launcher.launchWithDatagramChannel(TEST_SERVICE, options, arg); |
184 waitForTestResult(ssc, expectFail); |
194 waitForTestResult(ssc, expectFail); |
185 dc.close(); |
195 dc.close(); |
186 |
196 |
|
197 System.err.println("done"); |
187 if (failures > 0) { |
198 if (failures > 0) { |
188 throw new RuntimeException("Test failed - see log for details"); |
199 throw new RuntimeException("Test failed - see log for details"); |
189 } else { |
200 } else { |
190 System.out.println("All tests passed."); |
201 System.out.println("All tests passed."); |
191 } |
202 } |