Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/LingerTest.java
41152 views
1
/*
2
* Copyright (c) 2003, 2019, 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
* @library /test/lib
28
* @summary Linger interval delays usage of released file descriptor
29
* @run main LingerTest
30
* @run main/othervm -Djava.net.preferIPv4Stack=true LingerTest
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
import jdk.test.lib.net.IPSupport;
36
37
public class LingerTest {
38
39
static class Sender implements Runnable {
40
Socket s;
41
42
public Sender(Socket s) {
43
this.s = s;
44
}
45
46
public void run() {
47
System.out.println ("Sender starts");
48
try {
49
s.getOutputStream().write(new byte[128*1024]);
50
}
51
catch (IOException ioe) {
52
}
53
System.out.println ("Sender ends");
54
}
55
}
56
57
static class Closer implements Runnable {
58
Socket s;
59
60
public Closer(Socket s) {
61
this.s = s;
62
}
63
64
public void run() {
65
System.out.println ("Closer starts");
66
try {
67
s.close();
68
}
69
catch (IOException ioe) {
70
}
71
System.out.println ("Closer ends");
72
}
73
}
74
75
static class Other implements Runnable {
76
final InetAddress address;
77
final int port;
78
final long delay;
79
boolean connected = false;
80
81
public Other(InetAddress address, int port, long delay) {
82
this.address = address;
83
this.port = port;
84
this.delay = delay;
85
}
86
87
public void run() {
88
System.out.println ("Other starts: sleep " + delay);
89
try {
90
Thread.sleep(delay);
91
System.out.println ("Other opening socket");
92
Socket s = new Socket(address, port);
93
synchronized (this) {
94
connected = true;
95
}
96
s.close();
97
}
98
catch (Exception ioe) {
99
ioe.printStackTrace();
100
}
101
System.out.println ("Other ends");
102
}
103
104
public synchronized boolean connected() {
105
return connected;
106
}
107
}
108
109
public static void main(String args[]) throws Exception {
110
IPSupport.throwSkippedExceptionIfNonOperational();
111
112
InetAddress loopback = InetAddress.getLoopbackAddress();
113
ServerSocket ss = new ServerSocket(0, 50, loopback);
114
115
Socket s1 = new Socket(loopback, ss.getLocalPort());
116
Socket s2 = ss.accept();
117
118
// setup conditions for untransmitted data and lengthy
119
// linger interval
120
s1.setSendBufferSize(128*1024);
121
s1.setSoLinger(true, 30);
122
s2.setReceiveBufferSize(1*1024);
123
124
// start sender
125
Thread senderThread = new Thread(new Sender(s1));
126
senderThread.start();
127
128
// other thread that will connect after 5 seconds.
129
Other other = new Other(loopback, ss.getLocalPort(), 5000);
130
Thread otherThread = new Thread(other);
131
otherThread.start();
132
133
// give sender time to queue the data
134
System.out.println ("Main sleep 1000");
135
Thread.sleep(1000);
136
System.out.println ("Main continue");
137
138
// close the socket asynchronously
139
Thread closerThread = new Thread(new Closer(s1));
140
closerThread.start();
141
142
System.out.println ("Main sleep 15000");
143
// give other time to run
144
Thread.sleep(15000);
145
System.out.println ("Main closing serversocket");
146
147
ss.close();
148
// check that other is done
149
if (!other.connected()) {
150
throw new RuntimeException("Other thread is blocked");
151
}
152
153
// await termination of all test related threads
154
senderThread.join(60_000);
155
otherThread.join(60_000);
156
closerThread.join(60_000);
157
158
System.out.println ("Main ends");
159
}
160
}
161
162