|
1 /* |
|
2 * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* @test |
|
25 * @bug 4607272 |
|
26 * @summary Unit test for AsynchronousServerSocketChannel |
|
27 * @run main/timeout=180 Basic |
|
28 */ |
|
29 |
|
30 import java.nio.channels.*; |
|
31 import java.net.*; |
|
32 import java.io.IOException; |
|
33 import java.util.concurrent.ExecutionException; |
|
34 import java.util.concurrent.Future; |
|
35 import java.util.concurrent.atomic.AtomicReference; |
|
36 |
|
37 public class Basic { |
|
38 |
|
39 public static void main(String[] args) throws Exception { |
|
40 testBind(); |
|
41 testAccept(); |
|
42 } |
|
43 |
|
44 static void testBind() throws Exception { |
|
45 System.out.println("-- bind --"); |
|
46 |
|
47 AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel.open(); |
|
48 if (ch.getLocalAddress() != null) |
|
49 throw new RuntimeException("Local address should be 'null'"); |
|
50 ch.bind(new InetSocketAddress(0), 20); |
|
51 |
|
52 // check local address after binding |
|
53 InetSocketAddress local = (InetSocketAddress)ch.getLocalAddress(); |
|
54 if (local.getPort() == 0) |
|
55 throw new RuntimeException("Unexpected port"); |
|
56 if (!local.getAddress().isAnyLocalAddress()) |
|
57 throw new RuntimeException("Not bound to a wildcard address"); |
|
58 |
|
59 // try to re-bind |
|
60 try { |
|
61 ch.bind(new InetSocketAddress(0)); |
|
62 throw new RuntimeException("AlreadyBoundException expected"); |
|
63 } catch (AlreadyBoundException x) { |
|
64 } |
|
65 ch.close(); |
|
66 |
|
67 // check ClosedChannelException |
|
68 ch = AsynchronousServerSocketChannel.open(); |
|
69 ch.close(); |
|
70 try { |
|
71 ch.bind(new InetSocketAddress(0)); |
|
72 throw new RuntimeException("ClosedChannelException expected"); |
|
73 } catch (ClosedChannelException x) { |
|
74 } |
|
75 } |
|
76 |
|
77 static void testAccept() throws Exception { |
|
78 System.out.println("-- accept --"); |
|
79 |
|
80 final AsynchronousServerSocketChannel listener = |
|
81 AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0)); |
|
82 |
|
83 InetAddress lh = InetAddress.getLocalHost(); |
|
84 int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort(); |
|
85 final InetSocketAddress isa = new InetSocketAddress(lh, port); |
|
86 |
|
87 // establish a few loopback connections |
|
88 for (int i=0; i<100; i++) { |
|
89 SocketChannel sc = SocketChannel.open(isa); |
|
90 AsynchronousSocketChannel ch = listener.accept().get(); |
|
91 sc.close(); |
|
92 ch.close(); |
|
93 } |
|
94 |
|
95 final AtomicReference<Throwable> exception = new AtomicReference<Throwable>(); |
|
96 |
|
97 // start accepting |
|
98 listener.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() { |
|
99 public void completed(AsynchronousSocketChannel ch, Void att) { |
|
100 try { |
|
101 ch.close(); |
|
102 } catch (IOException ignore) { } |
|
103 } |
|
104 public void failed(Throwable exc, Void att) { |
|
105 exception.set(exc); |
|
106 } |
|
107 public void cancelled(Void att) { |
|
108 } |
|
109 }); |
|
110 |
|
111 // check AcceptPendingException |
|
112 try { |
|
113 listener.accept(); |
|
114 throw new RuntimeException("AcceptPendingException expected"); |
|
115 } catch (AcceptPendingException x) { |
|
116 } |
|
117 |
|
118 // asynchronous close |
|
119 listener.close(); |
|
120 while (exception.get() == null) |
|
121 Thread.sleep(100); |
|
122 if (!(exception.get() instanceof AsynchronousCloseException)) |
|
123 throw new RuntimeException("AsynchronousCloseException expected"); |
|
124 |
|
125 // once closed when a further attemt should throw ClosedChannelException |
|
126 try { |
|
127 listener.accept().get(); |
|
128 throw new RuntimeException("ExecutionException expected"); |
|
129 } catch (ExecutionException x) { |
|
130 if (!(x.getCause() instanceof ClosedChannelException)) |
|
131 throw new RuntimeException("Cause of ClosedChannelException expected"); |
|
132 } catch (InterruptedException x) { |
|
133 } |
|
134 |
|
135 } |
|
136 } |