author | mfang |
Wed, 17 Aug 2011 14:18:26 -0700 | |
changeset 10294 | 8fcdae2a7ec7 |
parent 7668 | d4a77089c587 |
child 21818 | 1f6c379e48f6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 4796166 |
|
27 |
* @summary Linger interval delays usage of released file descriptor |
|
28 |
*/ |
|
29 |
||
30 |
import java.net.*; |
|
31 |
import java.io.*; |
|
32 |
||
33 |
public class LingerTest { |
|
34 |
||
35 |
static class Sender implements Runnable { |
|
36 |
Socket s; |
|
37 |
||
38 |
public Sender(Socket s) { |
|
39 |
this.s = s; |
|
40 |
} |
|
41 |
||
42 |
public void run() { |
|
43 |
System.out.println ("Sender starts"); |
|
44 |
try { |
|
45 |
s.getOutputStream().write(new byte[128*1024]); |
|
46 |
} |
|
47 |
catch (IOException ioe) { |
|
48 |
} |
|
49 |
System.out.println ("Sender ends"); |
|
50 |
} |
|
51 |
} |
|
52 |
||
53 |
static class Closer implements Runnable { |
|
54 |
Socket s; |
|
55 |
||
56 |
public Closer(Socket s) { |
|
57 |
this.s = s; |
|
58 |
} |
|
59 |
||
60 |
public void run() { |
|
61 |
System.out.println ("Closer starts"); |
|
62 |
try { |
|
63 |
s.close(); |
|
64 |
} |
|
65 |
catch (IOException ioe) { |
|
66 |
} |
|
67 |
System.out.println ("Closer ends"); |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
static class Another implements Runnable { |
|
72 |
int port; |
|
73 |
long delay; |
|
74 |
boolean connected = false; |
|
75 |
||
76 |
public Another(int port, long delay) { |
|
77 |
this.port = port; |
|
78 |
this.delay = delay; |
|
79 |
} |
|
80 |
||
81 |
public void run() { |
|
82 |
System.out.println ("Another starts"); |
|
83 |
try { |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
84 |
Thread.sleep(delay); |
2 | 85 |
Socket s = new Socket("localhost", port); |
86 |
synchronized (this) { |
|
87 |
connected = true; |
|
88 |
} |
|
89 |
s.close(); |
|
90 |
} |
|
91 |
catch (Exception ioe) { |
|
92 |
ioe.printStackTrace(); |
|
93 |
} |
|
94 |
System.out.println ("Another ends"); |
|
95 |
} |
|
96 |
||
97 |
public synchronized boolean connected() { |
|
98 |
return connected; |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
public static void main(String args[]) throws Exception { |
|
103 |
ServerSocket ss = new ServerSocket(0); |
|
104 |
||
105 |
Socket s1 = new Socket("localhost", ss.getLocalPort()); |
|
106 |
Socket s2 = ss.accept(); |
|
107 |
||
108 |
// setup conditions for untransmitted data and lengthy |
|
109 |
// linger interval |
|
110 |
s1.setSendBufferSize(128*1024); |
|
111 |
s1.setSoLinger(true, 30); |
|
112 |
s2.setReceiveBufferSize(1*1024); |
|
113 |
||
114 |
// start sender |
|
115 |
Thread thr = new Thread(new Sender(s1)); |
|
116 |
thr.start(); |
|
117 |
||
118 |
// another thread that will connect after 5 seconds. |
|
119 |
Another another = new Another(ss.getLocalPort(), 5000); |
|
120 |
thr = new Thread(another); |
|
121 |
thr.start(); |
|
122 |
||
123 |
// give sender time to queue the data |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
124 |
Thread.sleep(1000); |
2 | 125 |
|
126 |
// close the socket asynchronously |
|
127 |
(new Thread(new Closer(s1))).start(); |
|
128 |
||
129 |
// give another time to run |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
130 |
Thread.sleep(10000); |
2 | 131 |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
132 |
ss.close(); |
2 | 133 |
// check that another is done |
134 |
if (!another.connected()) { |
|
135 |
throw new RuntimeException("Another thread is blocked"); |
|
136 |
} |
|
137 |
System.out.println ("Main ends"); |
|
138 |
||
139 |
} |
|
140 |
||
141 |
} |