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