Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/asyncClose/Race.java
41153 views
1
/*
2
* Copyright (c) 2013, 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 8006395 8012244
27
* @summary Tests racing code that reads and closes a Socket
28
*/
29
30
import java.io.InputStream;
31
import java.net.InetAddress;
32
import java.net.InetSocketAddress;
33
import java.net.ServerSocket;
34
import java.net.Socket;
35
import java.net.ConnectException;
36
import java.net.SocketException;
37
import java.util.concurrent.Phaser;
38
39
// Racey test, will not always fail, but if it does then we have a problem.
40
41
public class Race {
42
final static int THREADS = 100;
43
44
public static void main(String[] args) throws Exception {
45
try (ServerSocket ss = new ServerSocket()) {
46
InetAddress loopback = InetAddress.getLoopbackAddress();
47
ss.bind(new InetSocketAddress(loopback, 0));
48
final int port = ss.getLocalPort();
49
final Phaser phaser = new Phaser(THREADS + 1);
50
for (int i=0; i<100; i++) {
51
try {
52
final Socket s = new Socket(loopback, port);
53
s.setSoLinger(false, 0);
54
try (Socket sa = ss.accept()) {
55
sa.setSoLinger(false, 0);
56
final InputStream is = s.getInputStream();
57
Thread[] threads = new Thread[THREADS];
58
for (int j=0; j<THREADS; j++) {
59
threads[j] = new Thread() {
60
public void run() {
61
try {
62
phaser.arriveAndAwaitAdvance();
63
while (is.read() != -1)
64
Thread.sleep(50);
65
} catch (Exception x) {
66
if (!(x instanceof SocketException
67
&& x.getMessage().equalsIgnoreCase("socket closed")))
68
x.printStackTrace();
69
// ok, expect Socket closed
70
}
71
}};
72
}
73
for (int j=0; j<100; j++)
74
threads[j].start();
75
phaser.arriveAndAwaitAdvance();
76
s.close();
77
for (int j=0; j<100; j++)
78
threads[j].join();
79
}
80
} catch (ConnectException e) {
81
System.err.println("Exception " + e + " Port: " + port);
82
}
83
}
84
}
85
}
86
}
87
88