author | chegar |
Sun, 05 Nov 2017 17:32:13 +0000 | |
branch | http-client-branch |
changeset 55763 | 634d8e14c172 |
parent 47216 | 71c04702a3d5 |
child 49431 | 5812849b5027 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21818
diff
changeset
|
2 |
* Copyright (c) 2003, 2013, 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 |
||
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
71 |
static class Other implements Runnable { |
2 | 72 |
int port; |
73 |
long delay; |
|
74 |
boolean connected = false; |
|
75 |
||
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
76 |
public Other(int port, long delay) { |
2 | 77 |
this.port = port; |
78 |
this.delay = delay; |
|
79 |
} |
|
80 |
||
81 |
public void run() { |
|
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
82 |
System.out.println ("Other starts: sleep " + delay); |
2 | 83 |
try { |
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
84 |
Thread.sleep(delay); |
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
85 |
System.out.println ("Other opening socket"); |
2 | 86 |
Socket s = new Socket("localhost", port); |
87 |
synchronized (this) { |
|
88 |
connected = true; |
|
89 |
} |
|
90 |
s.close(); |
|
91 |
} |
|
92 |
catch (Exception ioe) { |
|
93 |
ioe.printStackTrace(); |
|
94 |
} |
|
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
95 |
System.out.println ("Other ends"); |
2 | 96 |
} |
97 |
||
98 |
public synchronized boolean connected() { |
|
99 |
return connected; |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
public static void main(String args[]) throws Exception { |
|
104 |
ServerSocket ss = new ServerSocket(0); |
|
105 |
||
106 |
Socket s1 = new Socket("localhost", ss.getLocalPort()); |
|
107 |
Socket s2 = ss.accept(); |
|
108 |
||
109 |
// setup conditions for untransmitted data and lengthy |
|
110 |
// linger interval |
|
111 |
s1.setSendBufferSize(128*1024); |
|
112 |
s1.setSoLinger(true, 30); |
|
113 |
s2.setReceiveBufferSize(1*1024); |
|
114 |
||
115 |
// start sender |
|
116 |
Thread thr = new Thread(new Sender(s1)); |
|
117 |
thr.start(); |
|
118 |
||
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
119 |
// other thread that will connect after 5 seconds. |
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
120 |
Other other = new Other(ss.getLocalPort(), 5000); |
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
121 |
thr = new Thread(other); |
2 | 122 |
thr.start(); |
123 |
||
124 |
// give sender time to queue the data |
|
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
125 |
System.out.println ("Main sleep 1000"); |
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
126 |
Thread.sleep(1000); |
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
127 |
System.out.println ("Main continue"); |
2 | 128 |
|
129 |
// close the socket asynchronously |
|
130 |
(new Thread(new Closer(s1))).start(); |
|
131 |
||
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
132 |
System.out.println ("Main sleep 15000"); |
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
133 |
// give other time to run |
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
134 |
Thread.sleep(15000); |
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
135 |
System.out.println ("Main closing serversocket"); |
2 | 136 |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
137 |
ss.close(); |
21818
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
138 |
// check that other is done |
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
139 |
if (!other.connected()) { |
1f6c379e48f6
8028581: [TESTBUG] java/net/Socket/LingerTest.java failing
michaelm
parents:
7668
diff
changeset
|
140 |
throw new RuntimeException("Other thread is blocked"); |
2 | 141 |
} |
142 |
System.out.println ("Main ends"); |
|
143 |
} |
|
144 |
} |