Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/SocketChannel/LingerOnClose.java
41154 views
1
/*
2
* Copyright (c) 2018, 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 8203059
26
* @summary Test SocketChannel.close with SO_LINGER enabled
27
*/
28
29
import java.io.IOException;
30
import java.net.InetAddress;
31
import java.net.InetSocketAddress;
32
import java.net.StandardSocketOptions;
33
import java.nio.ByteBuffer;
34
import java.nio.channels.SelectionKey;
35
import java.nio.channels.Selector;
36
import java.nio.channels.ServerSocketChannel;
37
import java.nio.channels.SocketChannel;
38
39
public class LingerOnClose {
40
41
private enum TestMode {
42
BLOCKING,
43
NON_BLOCKING,
44
NON_BLOCKING_AND_REGISTERED;
45
}
46
47
public static void main(String[] args) throws IOException {
48
// blocking mode
49
test(TestMode.BLOCKING, -1);
50
test(TestMode.BLOCKING, 0);
51
test(TestMode.BLOCKING, 1);
52
53
// non-blocking mode
54
test(TestMode.NON_BLOCKING, -1);
55
test(TestMode.NON_BLOCKING, 0);
56
test(TestMode.NON_BLOCKING, 1);
57
58
// non-blocking mode, close while registered with Selector
59
test(TestMode.NON_BLOCKING_AND_REGISTERED, -1);
60
test(TestMode.NON_BLOCKING_AND_REGISTERED, 0);
61
test(TestMode.NON_BLOCKING_AND_REGISTERED, 1);
62
}
63
64
/**
65
* Test closing a SocketChannel with SO_LINGER set to the given linger
66
* interval. If the linger interval is 0, it checks that the peer observes
67
* a connection reset (TCP RST).
68
*/
69
static void test(TestMode mode, int interval) throws IOException {
70
SocketChannel sc = null;
71
SocketChannel peer = null;
72
Selector sel = null;
73
74
try (ServerSocketChannel ssc = ServerSocketChannel.open()) {
75
ssc.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
76
77
// establish loopback connection
78
sc = SocketChannel.open(ssc.getLocalAddress());
79
peer = ssc.accept();
80
81
// configured blocking mode and register with Selector if needed
82
if (mode != TestMode.BLOCKING)
83
sc.configureBlocking(false);
84
if (mode == TestMode.NON_BLOCKING_AND_REGISTERED) {
85
sel = Selector.open();
86
sc.register(sel, SelectionKey.OP_READ);
87
sel.selectNow();
88
}
89
90
// enable or disable SO_LINGER
91
sc.setOption(StandardSocketOptions.SO_LINGER, interval);
92
93
// close channel and flush Selector if needed
94
sc.close();
95
if (mode == TestMode.NON_BLOCKING_AND_REGISTERED)
96
sel.selectNow();
97
98
// read other end of connection, expect EOF or RST
99
ByteBuffer bb = ByteBuffer.allocate(100);
100
try {
101
int n = peer.read(bb);
102
if (interval == 0) {
103
throw new RuntimeException("RST expected");
104
} else if (n != -1) {
105
throw new RuntimeException("EOF expected");
106
}
107
} catch (IOException ioe) {
108
if (interval != 0) {
109
// exception not expected
110
throw ioe;
111
}
112
}
113
} finally {
114
if (sc != null) sc.close();
115
if (peer != null) peer.close();
116
if (sel != null) sel.close();
117
}
118
}
119
}
120
121