Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/SoTimeout.java
41149 views
1
/*
2
* Copyright (c) 1998, 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
/* @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();
48
serverSocket.bind(new InetSocketAddress(addr, 0));
49
port = serverSocket.getLocalPort();
50
51
byte[] b = new byte[12];
52
Thread t = new Thread(new SoTimeout());
53
t.start();
54
55
Socket s = serverSocket.accept();
56
serverSocket.close();
57
58
// set a 5 second timeout on the socket
59
s.setSoTimeout(5000);
60
61
s.getInputStream().read(b, 0, b.length);
62
s.close();
63
64
long waited = System.currentTimeMillis() - timeWritten;
65
66
// this sequence should complete fairly quickly and if it
67
// takes something resembling the the SoTimeout value then
68
// we are probably incorrectly blocking and not waking up
69
if (waited > 2000) {
70
throw new Exception("shouldn't take " + waited + " to complete");
71
}
72
}
73
74
public void run() {
75
try {
76
byte[] b = new byte[12];
77
Socket s = new Socket(addr, port);
78
79
Thread.yield();
80
timeWritten = System.currentTimeMillis();
81
s.getOutputStream().write(b, 0, 12);
82
s.close();
83
} catch (Exception e) {
84
e.printStackTrace();
85
}
86
}
87
88
}
89
90