Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/AsynchronousSocketChannel/DieBeforeComplete.java
41153 views
1
/*
2
* Copyright (c) 2008, 2009, 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 6842687
26
* @summary Unit test for AsynchronousSocketChannel/AsynchronousServerSocketChannel
27
*/
28
import java.nio.ByteBuffer;
29
import java.nio.channels.*;
30
import java.net.*;
31
import java.util.concurrent.*;
32
import java.util.concurrent.atomic.AtomicReference;
33
34
/**
35
* Initiates I/O operation on a thread that terminates before the I/O completes.
36
*/
37
38
public class DieBeforeComplete {
39
40
public static void main(String[] args) throws Exception {
41
final AsynchronousServerSocketChannel listener =
42
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));
43
44
InetAddress lh = InetAddress.getLocalHost();
45
int port = ((InetSocketAddress) (listener.getLocalAddress())).getPort();
46
final SocketAddress sa = new InetSocketAddress(lh, port);
47
48
// -- accept --
49
50
// initiate accept in a thread that dies before connection is established
51
Future<AsynchronousSocketChannel> r1 =
52
initiateAndDie(new Task<AsynchronousSocketChannel>() {
53
public Future<AsynchronousSocketChannel> run() {
54
return listener.accept();
55
}});
56
57
// establish and accept connection
58
SocketChannel peer = SocketChannel.open(sa);
59
final AsynchronousSocketChannel channel = r1.get();
60
61
// --- read --
62
63
// initiate read in a thread that dies befores bytes are available
64
final ByteBuffer dst = ByteBuffer.allocate(100);
65
Future<Integer> r2 = initiateAndDie(new Task<Integer>() {
66
public Future<Integer> run() {
67
return channel.read(dst);
68
}});
69
70
// send bytes
71
peer.write(ByteBuffer.wrap("hello".getBytes()));
72
int nread = r2.get();
73
if (nread <= 0)
74
throw new RuntimeException("Should have read at least one byte");
75
76
// -- write --
77
78
// initiate writes in threads that dies
79
boolean completedImmediately;
80
Future<Integer> r3;
81
do {
82
final ByteBuffer src = ByteBuffer.wrap(new byte[10000]);
83
r3 = initiateAndDie(new Task<Integer>() {
84
public Future<Integer> run() {
85
return channel.write(src);
86
}});
87
try {
88
int nsent = r3.get(5, TimeUnit.SECONDS);
89
if (nsent <= 0)
90
throw new RuntimeException("Should have wrote at least one byte");
91
completedImmediately = true;
92
} catch (TimeoutException x) {
93
completedImmediately = false;
94
}
95
} while (completedImmediately);
96
97
// drain connection
98
peer.configureBlocking(false);
99
ByteBuffer src = ByteBuffer.allocateDirect(10000);
100
do {
101
src.clear();
102
nread = peer.read(src);
103
if (nread == 0) {
104
Thread.sleep(100);
105
nread = peer.read(src);
106
}
107
} while (nread > 0);
108
109
// write should complete now
110
int nsent = r3.get();
111
if (nsent <= 0)
112
throw new RuntimeException("Should have wrote at least one byte");
113
}
114
115
static interface Task<T> {
116
Future<T> run();
117
}
118
119
static <T> Future<T> initiateAndDie(final Task<T> task) {
120
final AtomicReference<Future<T>> result = new AtomicReference<Future<T>>();
121
Runnable r = new Runnable() {
122
public void run() {
123
result.set(task.run());
124
}
125
};
126
Thread t = new Thread(r);
127
t.start();
128
while (t.isAlive()) {
129
try {
130
t.join();
131
} catch (InterruptedException x) {
132
}
133
}
134
return result.get();
135
}
136
}
137
138