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/SoTimeout.java
38812 views
1
/*
2
* Copyright (c) 1998, 2010, 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
/* @test
25
@bug 4156625
26
@summary Socket.setSoTimeout(T) can cause incorrect delay of T
27
under green threads
28
@author Tom Rodriguez
29
*/
30
31
/*
32
* This program depends a bit on the particular behaviour of the green
33
* threads scheduler to produce the problem, but given that the underlying
34
* bug a green threads bug, I think that's OK.
35
*/
36
37
import java.net.*;
38
39
public class SoTimeout implements Runnable {
40
static ServerSocket serverSocket;
41
static long timeWritten;
42
static InetAddress addr;
43
static int port;
44
45
public static void main(String[] args) throws Exception {
46
addr = InetAddress.getLocalHost();
47
serverSocket = new ServerSocket(0);
48
port = serverSocket.getLocalPort();
49
50
byte[] b = new byte[12];
51
Thread t = new Thread(new SoTimeout());
52
t.start();
53
54
Socket s = serverSocket.accept();
55
serverSocket.close();
56
57
// set a 5 second timeout on the socket
58
s.setSoTimeout(5000);
59
60
s.getInputStream().read(b, 0, b.length);
61
s.close();
62
63
long waited = System.currentTimeMillis() - timeWritten;
64
65
// this sequence should complete fairly quickly and if it
66
// takes something resembling the the SoTimeout value then
67
// we are probably incorrectly blocking and not waking up
68
if (waited > 2000) {
69
throw new Exception("shouldn't take " + waited + " to complete");
70
}
71
}
72
73
public void run() {
74
try {
75
byte[] b = new byte[12];
76
Socket s = new Socket(addr, port);
77
78
Thread.yield();
79
timeWritten = System.currentTimeMillis();
80
s.getOutputStream().write(b, 0, 12);
81
s.close();
82
} catch (Exception e) {
83
e.printStackTrace();
84
}
85
}
86
87
}
88
89