58808
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 8212132
|
|
27 |
* @summary Test that DatagramChannel does not leak file descriptors
|
|
28 |
* @requires os.family != "windows"
|
|
29 |
* @modules jdk.management
|
|
30 |
* @library /test/lib
|
|
31 |
* @run main/othervm Unref
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.io.IOException;
|
|
35 |
import java.lang.management.ManagementFactory;
|
|
36 |
import java.net.InetSocketAddress;
|
|
37 |
import java.net.StandardProtocolFamily;
|
|
38 |
import java.nio.channels.DatagramChannel;
|
|
39 |
import java.nio.channels.SelectionKey;
|
|
40 |
import java.nio.channels.Selector;
|
|
41 |
|
|
42 |
import com.sun.management.UnixOperatingSystemMXBean;
|
|
43 |
|
|
44 |
import jtreg.SkippedException;
|
|
45 |
import jdk.test.lib.net.IPSupport;
|
|
46 |
|
|
47 |
public class Unref {
|
|
48 |
|
|
49 |
interface DatagramChannelSupplier {
|
|
50 |
DatagramChannel get() throws IOException;
|
|
51 |
}
|
|
52 |
|
|
53 |
public static void main(String[] args) throws Exception {
|
|
54 |
if (unixOperatingSystemMXBean() == null)
|
|
55 |
throw new SkippedException("This test requires UnixOperatingSystemMXBean");
|
|
56 |
|
|
57 |
test(DatagramChannel::open);
|
|
58 |
if (IPSupport.hasIPv4())
|
|
59 |
test(() -> DatagramChannel.open(StandardProtocolFamily.INET));
|
|
60 |
if (IPSupport.hasIPv6())
|
|
61 |
test(() -> DatagramChannel.open(StandardProtocolFamily.INET6));
|
|
62 |
}
|
|
63 |
|
|
64 |
static void test(DatagramChannelSupplier supplier) throws Exception {
|
|
65 |
openAndClose(supplier); // warm-up
|
|
66 |
|
|
67 |
try (Selector sel = Selector.open()) {
|
|
68 |
long count = fileDescriptorCount();
|
|
69 |
|
|
70 |
// open+close
|
|
71 |
openAndClose(supplier);
|
|
72 |
assertEquals(fileDescriptorCount(), count);
|
|
73 |
|
|
74 |
// open+unref, file descriptor should be closed by cleaner
|
|
75 |
openAndUnref(supplier);
|
|
76 |
assertEquals(waitForFileDescriptorCount(count), count);
|
|
77 |
|
|
78 |
// open+register+close+flush
|
|
79 |
openRegisterAndClose(supplier, sel);
|
|
80 |
assertEquals(fileDescriptorCount(), count);
|
|
81 |
|
|
82 |
// open+register+flush, file descriptor should be closed by cleaner
|
|
83 |
openRegisterAndUnref(supplier, sel);
|
|
84 |
assertEquals(waitForFileDescriptorCount(count), count);
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Create a DatagramChannel and closes it.
|
|
90 |
*/
|
|
91 |
static void openAndClose(DatagramChannelSupplier supplier) throws IOException {
|
|
92 |
System.out.println("openAndClose ...");
|
|
93 |
DatagramChannel dc = supplier.get();
|
|
94 |
dc.close();
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Create a DatagramChannel and exits without closing the channel.
|
|
99 |
*/
|
|
100 |
static void openAndUnref(DatagramChannelSupplier supplier) throws IOException {
|
|
101 |
System.out.println("openAndUnref ...");
|
|
102 |
DatagramChannel dc = supplier.get();
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Create a DatagramChannel, register it with a Selector, close the channel
|
|
107 |
* while register, and then finally flush the channel from the Selector.
|
|
108 |
*/
|
|
109 |
static void openRegisterAndClose(DatagramChannelSupplier supplier, Selector sel)
|
|
110 |
throws IOException
|
|
111 |
{
|
|
112 |
System.out.println("openRegisterAndClose ...");
|
|
113 |
try (DatagramChannel dc = supplier.get()) {
|
|
114 |
dc.bind(new InetSocketAddress(0));
|
|
115 |
dc.configureBlocking(false);
|
|
116 |
dc.register(sel, SelectionKey.OP_READ);
|
|
117 |
sel.selectNow();
|
|
118 |
}
|
|
119 |
|
|
120 |
// flush, should close channel
|
|
121 |
sel.selectNow();
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Creates a DatagramChannel, registers with a Selector, cancels the key
|
|
126 |
* and flushes the channel from the Selector. This method exits without
|
|
127 |
* closing the channel.
|
|
128 |
*/
|
|
129 |
static void openRegisterAndUnref(DatagramChannelSupplier supplier, Selector sel)
|
|
130 |
throws IOException
|
|
131 |
{
|
|
132 |
System.out.println("openRegisterAndUnref ...");
|
|
133 |
DatagramChannel dc = supplier.get();
|
|
134 |
dc.bind(new InetSocketAddress(0));
|
|
135 |
dc.configureBlocking(false);
|
|
136 |
SelectionKey key = dc.register(sel, SelectionKey.OP_READ);
|
|
137 |
sel.selectNow();
|
|
138 |
key.cancel();
|
|
139 |
sel.selectNow();
|
|
140 |
}
|
|
141 |
|
|
142 |
/**
|
|
143 |
* If the file descriptor count is higher than the given count then invoke
|
|
144 |
* System.gc() and wait for the file descriptor count to drop.
|
|
145 |
*/
|
|
146 |
static long waitForFileDescriptorCount(long target) throws InterruptedException {
|
|
147 |
long actual = fileDescriptorCount();
|
|
148 |
if (actual > target) {
|
|
149 |
System.gc();
|
|
150 |
while ((actual = fileDescriptorCount()) > target) {
|
|
151 |
Thread.sleep(10);
|
|
152 |
}
|
|
153 |
}
|
|
154 |
return actual;
|
|
155 |
}
|
|
156 |
|
|
157 |
static UnixOperatingSystemMXBean unixOperatingSystemMXBean() {
|
|
158 |
return ManagementFactory.getPlatformMXBean(UnixOperatingSystemMXBean.class);
|
|
159 |
}
|
|
160 |
|
|
161 |
static long fileDescriptorCount() {
|
|
162 |
return unixOperatingSystemMXBean().getOpenFileDescriptorCount();
|
|
163 |
}
|
|
164 |
|
|
165 |
static void assertEquals(long actual, long expected) {
|
|
166 |
if (actual != expected)
|
|
167 |
throw new RuntimeException("actual=" + actual + ", expected=" + expected);
|
|
168 |
}
|
|
169 |
}
|
|
170 |
|