equal
deleted
inserted
replaced
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
21 * have any questions. |
21 * have any questions. |
22 */ |
22 */ |
23 |
23 |
24 /* |
24 /* |
25 * @test |
25 * Test run from script, AcceptCauseFileDescriptorLeak.sh |
26 * @summary configuring unconnected Socket before passing to implAccept can cause fd leak |
26 * author Edward Wang |
27 * @bug 6368984 |
|
28 * @author Edward Wang |
|
29 */ |
27 */ |
30 |
28 |
31 import java.io.*; |
29 import java.io.IOException; |
32 import java.net.*; |
30 import java.net.ServerSocket; |
|
31 import java.net.Socket; |
33 |
32 |
34 public class AcceptCauseFileDescriptorLeak { |
33 public class AcceptCauseFileDescriptorLeak { |
35 private static final int REPS = 1000; |
34 private static final int REPS = 2048; |
36 |
35 |
37 public static void main(String[] args) throws Exception { |
36 public static void main(String[] args) throws Exception { |
38 final ServerSocket ss = new ServerSocket(0) { |
37 final ServerSocket ss = new ServerSocket(0) { |
39 public Socket accept() throws IOException { |
38 public Socket accept() throws IOException { |
40 Socket s = new Socket() { }; |
39 Socket s = new Socket() { }; |
58 for (int i = 0; i < REPS; i++) { |
57 for (int i = 0; i < REPS; i++) { |
59 ss.accept().close(); |
58 ss.accept().close(); |
60 } |
59 } |
61 ss.close(); |
60 ss.close(); |
62 t.join(); |
61 t.join(); |
63 |
|
64 // |
|
65 // The threshold 20 below is a little arbitrary. The point here is that |
|
66 // the remaining open file descriptors should be constant independent |
|
67 // of REPS. |
|
68 // |
|
69 if (countOpenFD() > 20) { |
|
70 throw new RuntimeException("File descriptor leak detected."); |
|
71 } |
|
72 } |
|
73 |
|
74 |
|
75 /* |
|
76 * Actually, this approach to count open file descriptors only |
|
77 * works for Solaris/Linux. On Windows platform, this method |
|
78 * will simply return zero. So the test will always be passed |
|
79 * on Windows, too. |
|
80 */ |
|
81 private static int countOpenFD() { |
|
82 File dirOfFD = new File("/proc/self/fd"); |
|
83 File[] fds = dirOfFD.listFiles(); |
|
84 |
|
85 if (fds != null) |
|
86 return fds.length; |
|
87 else |
|
88 return 0; |
|
89 } |
62 } |
90 } |
63 } |