Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/LingerTest.java
38812 views
1
/*
2
* Copyright (c) 2003, 2013, 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 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 Other implements Runnable {
72
int port;
73
long delay;
74
boolean connected = false;
75
76
public Other(int port, long delay) {
77
this.port = port;
78
this.delay = delay;
79
}
80
81
public void run() {
82
System.out.println ("Other starts: sleep " + delay);
83
try {
84
Thread.sleep(delay);
85
System.out.println ("Other opening socket");
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
}
95
System.out.println ("Other ends");
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
119
// other thread that will connect after 5 seconds.
120
Other other = new Other(ss.getLocalPort(), 5000);
121
thr = new Thread(other);
122
thr.start();
123
124
// give sender time to queue the data
125
System.out.println ("Main sleep 1000");
126
Thread.sleep(1000);
127
System.out.println ("Main continue");
128
129
// close the socket asynchronously
130
(new Thread(new Closer(s1))).start();
131
132
System.out.println ("Main sleep 15000");
133
// give other time to run
134
Thread.sleep(15000);
135
System.out.println ("Main closing serversocket");
136
137
ss.close();
138
// check that other is done
139
if (!other.connected()) {
140
throw new RuntimeException("Other thread is blocked");
141
}
142
System.out.println ("Main ends");
143
}
144
}
145
146