Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/asyncClose/ServerSocket_accept.java
41153 views
1
/*
2
* Copyright (c) 2001, 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
* Tests that a thread blocked in ServerSocket.accept
26
* throws a SocketException if the socket is asynchronously closed.
27
*/
28
import java.io.IOException;
29
import java.net.InetAddress;
30
import java.net.ServerSocket;
31
import java.net.Socket;
32
import java.net.SocketException;
33
import java.util.concurrent.CountDownLatch;
34
import java.util.concurrent.atomic.AtomicBoolean;
35
36
public class ServerSocket_accept extends AsyncCloseTest implements Runnable {
37
private final ServerSocket ss;
38
private final int timeout;
39
private final CountDownLatch latch;
40
private final AtomicBoolean readyToClose = new AtomicBoolean(false);
41
42
public ServerSocket_accept() throws IOException {
43
this(0);
44
}
45
46
public ServerSocket_accept(int timeout) throws IOException {
47
this.timeout = timeout;
48
latch = new CountDownLatch(1);
49
ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
50
}
51
52
public String description() {
53
String s = "ServerSocket.accept()";
54
if (timeout > 0) {
55
s += " (with timeout)";
56
}
57
return s;
58
}
59
60
public void run() {
61
try {
62
latch.countDown();
63
Socket s;
64
// if readyToClose is still false it means some other
65
// process on the system attempted to connect: just
66
// ignore it, and go back to accept again.
67
do {
68
s = ss.accept();
69
} while (!readyToClose.get());
70
failed("ServerSocket.accept() returned unexpectedly!!" + " - " + s);
71
} catch (SocketException se) {
72
closed();
73
} catch (Exception e) {
74
failed(e.getMessage());
75
}
76
}
77
78
public AsyncCloseTest go(){
79
try {
80
Thread thr = new Thread(this);
81
thr.start();
82
latch.await();
83
Thread.sleep(5000); //sleep, so ServerSocket.accept() can block
84
readyToClose.set(true);
85
ss.close();
86
thr.join();
87
88
if (isClosed()) {
89
return passed();
90
} else {
91
return failed("ServerSocket.accept() wasn't preempted");
92
}
93
} catch (Exception x) {
94
failed(x.getMessage());
95
throw new RuntimeException(x);
96
}
97
}
98
}
99
100