39 import java.lang.management.OperatingSystemMXBean; |
41 import java.lang.management.OperatingSystemMXBean; |
40 import java.lang.ref.Reference; |
42 import java.lang.ref.Reference; |
41 import java.lang.ref.ReferenceQueue; |
43 import java.lang.ref.ReferenceQueue; |
42 import java.lang.ref.WeakReference; |
44 import java.lang.ref.WeakReference; |
43 import java.lang.reflect.Field; |
45 import java.lang.reflect.Field; |
44 import java.nio.file.Files; |
|
45 import java.nio.file.Path; |
46 import java.nio.file.Path; |
46 import java.nio.file.Paths; |
|
47 import java.util.ArrayDeque; |
47 import java.util.ArrayDeque; |
48 import java.util.HashSet; |
48 import java.util.HashSet; |
49 import java.util.List; |
|
50 import java.util.Optional; |
|
51 import java.util.concurrent.TimeUnit; |
|
52 import java.util.concurrent.atomic.AtomicInteger; |
49 import java.util.concurrent.atomic.AtomicInteger; |
53 |
50 |
54 import com.sun.management.UnixOperatingSystemMXBean; |
51 import com.sun.management.UnixOperatingSystemMXBean; |
|
52 |
|
53 import jdk.test.lib.util.FileUtils; |
55 |
54 |
56 /** |
55 /** |
57 * Tests for FIS unreferenced. |
56 * Tests for FIS unreferenced. |
58 * - Not subclassed - cleaner cleanup |
57 * - Not subclassed - cleaner cleanup |
59 * - Subclassed no finalize or close - cleaner cleanup |
58 * - Subclassed no finalize or close - cleaner cleanup |
144 inFile.createNewFile(); |
143 inFile.createNewFile(); |
145 inFile.deleteOnExit(); |
144 inFile.deleteOnExit(); |
146 |
145 |
147 String name = inFile.getPath(); |
146 String name = inFile.getPath(); |
148 |
147 |
|
148 FileUtils.listFileDescriptors(System.out); |
149 long fdCount0 = getFdCount(); |
149 long fdCount0 = getFdCount(); |
150 System.out.printf("initial count of open file descriptors: %d%n", fdCount0); |
|
151 |
150 |
152 int failCount = 0; |
151 int failCount = 0; |
153 failCount += test(new FileInputStream(name), CleanupType.CLEANER); |
152 failCount += test(new FileInputStream(name), CleanupType.CLEANER); |
154 |
153 |
155 failCount += test(new StreamOverrides(name), CleanupType.CLEANER); |
154 failCount += test(new StreamOverrides(name), CleanupType.CLEANER); |
164 throw new AssertionError("Failed test count: " + failCount); |
163 throw new AssertionError("Failed test count: " + failCount); |
165 } |
164 } |
166 |
165 |
167 // Check the final count of open file descriptors |
166 // Check the final count of open file descriptors |
168 long fdCount = getFdCount(); |
167 long fdCount = getFdCount(); |
169 System.out.printf("final count of open file descriptors: %d%n", fdCount); |
|
170 if (fdCount != fdCount0) { |
168 if (fdCount != fdCount0) { |
171 listProcFD(); |
169 System.out.printf("initial count of open file descriptors: %d%n", fdCount0); |
172 throw new AssertionError("raw fd count wrong: expected: " + fdCount0 |
170 System.out.printf("final count of open file descriptors: %d%n", fdCount); |
173 + ", actual: " + fdCount); |
171 FileUtils.listFileDescriptors(System.out); |
174 } |
172 } |
175 } |
173 } |
176 |
174 |
177 // Get the count of open file descriptors, or -1 if not available |
175 // Get the count of open file descriptors, or -1 if not available |
178 private static long getFdCount() { |
176 private static long getFdCount() { |
272 ex.printStackTrace(System.out); |
270 ex.printStackTrace(System.out); |
273 return 1; |
271 return 1; |
274 } |
272 } |
275 return 0; |
273 return 0; |
276 } |
274 } |
277 |
|
278 /** |
|
279 * Method to list the open file descriptors (if supported by the 'lsof' command). |
|
280 */ |
|
281 static void listProcFD() { |
|
282 List<String> lsofDirs = List.of("/usr/bin", "/usr/sbin"); |
|
283 Optional<Path> lsof = lsofDirs.stream() |
|
284 .map(s -> Paths.get(s, "lsof")) |
|
285 .filter(f -> Files.isExecutable(f)) |
|
286 .findFirst(); |
|
287 lsof.ifPresent(exe -> { |
|
288 try { |
|
289 System.out.printf("Open File Descriptors:%n"); |
|
290 long pid = ProcessHandle.current().pid(); |
|
291 ProcessBuilder pb = new ProcessBuilder(exe.toString(), "-p", Integer.toString((int) pid)); |
|
292 pb.inheritIO(); |
|
293 Process p = pb.start(); |
|
294 p.waitFor(10, TimeUnit.SECONDS); |
|
295 } catch (IOException | InterruptedException ie) { |
|
296 ie.printStackTrace(); |
|
297 } |
|
298 }); |
|
299 } |
|
300 } |
275 } |