20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /* @test |
24 /* @test |
25 * @bug 4607272 |
25 * @bug 4607272 6999915 |
26 * @summary Unit test for AsynchronousSocketChannel |
26 * @summary Unit test for AsynchronousSocketChannel |
27 * @run main/othervm -XX:+DisableExplicitGC -mx64m Leaky |
27 * @run main/othervm -XX:+DisableExplicitGC -XX:MaxDirectMemorySize=64m Leaky |
28 */ |
28 */ |
29 |
29 |
30 import java.nio.ByteBuffer; |
30 import java.nio.ByteBuffer; |
|
31 import java.nio.BufferPoolMXBean; |
31 import java.nio.channels.*; |
32 import java.nio.channels.*; |
32 import java.net.*; |
33 import java.net.*; |
|
34 import java.util.List; |
33 import java.util.concurrent.Future; |
35 import java.util.concurrent.Future; |
|
36 import java.util.concurrent.ThreadFactory; |
|
37 import java.lang.management.ManagementFactory; |
34 |
38 |
35 /** |
39 /** |
36 * Heap buffers must be substituted with direct buffers when doing I/O. This |
40 * Heap buffers must be substituted with direct buffers when doing I/O. This |
37 * test creates a scenario on Windows that challenges the per-thread buffer |
41 * test creates a scenario on Windows that challenges the per-thread buffer |
38 * cache and quickly leads to an OutOfMemoryError if temporary buffers are |
42 * cache and quickly leads to an OutOfMemoryError if temporary buffers are |
47 private final AsynchronousSocketChannel client; |
51 private final AsynchronousSocketChannel client; |
48 private final SocketChannel peer; |
52 private final SocketChannel peer; |
49 private final ByteBuffer dst; |
53 private final ByteBuffer dst; |
50 private Future<Integer> readResult; |
54 private Future<Integer> readResult; |
51 |
55 |
52 Connection() throws Exception { |
56 Connection(AsynchronousChannelGroup group) throws Exception { |
53 ServerSocketChannel ssc = |
57 ServerSocketChannel ssc = |
54 ServerSocketChannel.open().bind(new InetSocketAddress(0)); |
58 ServerSocketChannel.open().bind(new InetSocketAddress(0)); |
55 InetAddress lh = InetAddress.getLocalHost(); |
59 InetAddress lh = InetAddress.getLocalHost(); |
56 int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort(); |
60 int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort(); |
57 SocketAddress remote = new InetSocketAddress(lh, port); |
61 SocketAddress remote = new InetSocketAddress(lh, port); |
58 client = AsynchronousSocketChannel.open(); |
62 client = AsynchronousSocketChannel.open(group); |
59 client.connect(remote).get(); |
63 client.connect(remote).get(); |
60 peer = ssc.accept(); |
64 peer = ssc.accept(); |
61 ssc.close(); |
65 ssc.close(); |
62 dst = ByteBuffer.allocate(K*K); |
66 dst = ByteBuffer.allocate(K*K); |
63 } |
67 } |
75 readResult.get(); |
79 readResult.get(); |
76 } |
80 } |
77 } |
81 } |
78 |
82 |
79 public static void main(String[] args) throws Exception { |
83 public static void main(String[] args) throws Exception { |
|
84 ThreadFactory threadFactory = new ThreadFactory() { |
|
85 @Override |
|
86 public Thread newThread(Runnable r) { |
|
87 Thread t = new Thread(r); |
|
88 t.setDaemon(true); |
|
89 return t; |
|
90 } |
|
91 }; |
|
92 AsynchronousChannelGroup group = |
|
93 AsynchronousChannelGroup.withFixedThreadPool(4, threadFactory); |
80 |
94 |
81 final int CONNECTION_COUNT = 10; |
95 final int CONNECTION_COUNT = 10; |
82 Connection[] connections = new Connection[CONNECTION_COUNT]; |
96 Connection[] connections = new Connection[CONNECTION_COUNT]; |
83 for (int i=0; i<CONNECTION_COUNT; i++) { |
97 for (int i=0; i<CONNECTION_COUNT; i++) { |
84 connections[i] = new Connection(); |
98 connections[i] = new Connection(group); |
85 } |
99 } |
86 |
100 |
87 for (int i=0; i<1024; i++) { |
101 for (int i=0; i<1024; i++) { |
88 // initiate reads |
102 // initiate reads |
89 for (Connection conn: connections) { |
103 for (Connection conn: connections) { |
98 // complete read |
112 // complete read |
99 for (Connection conn: connections) { |
113 for (Connection conn: connections) { |
100 conn.finishRead(); |
114 conn.finishRead(); |
101 } |
115 } |
102 } |
116 } |
|
117 |
|
118 // print summary of buffer pool usage |
|
119 List<BufferPoolMXBean> pools = |
|
120 ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); |
|
121 for (BufferPoolMXBean pool: pools) |
|
122 System.out.format(" %8s ", pool.getName()); |
|
123 System.out.println(); |
|
124 for (int i=0; i<pools.size(); i++) |
|
125 System.out.format("%6s %10s %10s ", "Count", "Capacity", "Memory"); |
|
126 System.out.println(); |
|
127 for (BufferPoolMXBean pool: pools) { |
|
128 System.out.format("%6d %10d %10d ", |
|
129 pool.getCount(), pool.getTotalCapacity(), pool.getMemoryUsed()); |
|
130 } |
|
131 System.out.println(); |
103 } |
132 } |
104 } |
133 } |