Skip to content

Commit b4f6c89

Browse files
committed
[Wisp] Keep aligning Socket.isConnected() with the normal OpenJDK returning result
Summary: Socket.connect() in Wisp's implemention, uses nio SocketChannel's logic to prevent blocking when Socket is used. However, OpenJDK's isConnected() returns true after successfully connecting even if the socket gets closed. This will cause break accordance so we shall fix this. Test Plan: newly-added ServerSocketConnectionTest.java Reviewed-by: D-D-H Issue: #437
1 parent dadc45b commit b4f6c89

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

jdk/src/share/classes/java/net/Socket.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@ SocketImpl getImpl() throws SocketException {
597597
public void connect(SocketAddress endpoint) throws IOException {
598598
if (WispEngine.transparentWispSwitch()) {
599599
asyncImpl.connect(endpoint);
600+
// Getting here normally means we have succeeded in the connection
601+
connected = true;
600602
return;
601603
}
602604
connect(endpoint, 0);
@@ -637,6 +639,8 @@ public void connect(SocketAddress endpoint, int timeout) throws IOException {
637639

638640
if (WispEngine.transparentWispSwitch()) {
639641
asyncImpl.connect(endpoint, timeout);
642+
// Getting here normally means we have succeeded in the connection
643+
connected = true;
640644
return;
641645
}
642646

@@ -1773,8 +1777,9 @@ public String toString() {
17731777
* @since 1.4
17741778
*/
17751779
public boolean isConnected() {
1776-
if (WispEngine.transparentWispSwitch())
1777-
return asyncImpl.isConnected();
1780+
if (WispEngine.transparentWispSwitch()) {
1781+
return connected;
1782+
}
17781783

17791784
// Before 1.3 Sockets were always connected during creation
17801785
return connected || oldImpl;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2022 Alibaba Group Holding Limited. 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. Alibaba designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*/
21+
22+
/*
23+
* @test
24+
* @summary Test ServerSocket isConnected() method
25+
* @requires os.family == "linux"
26+
* @library /lib/testlibrary
27+
* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableCoroutine -XX:+UseWispMonitor -Dcom.alibaba.wisp.transparentWispSwitch=true ServerSocketConnectionTest
28+
*/
29+
30+
import java.io.IOException;
31+
import java.net.*;
32+
import static jdk.testlibrary.Asserts.assertTrue;
33+
import static jdk.testlibrary.Asserts.assertFalse;
34+
35+
36+
public class ServerSocketConnectionTest {
37+
38+
private static void testIsConnectedAfterClosing() throws Exception {
39+
ServerSocket ss = null;
40+
Socket s1 = null;
41+
InetAddress ia = InetAddress.getLocalHost();
42+
try {
43+
ss = new ServerSocket(0);
44+
ss.setSoTimeout(1000);
45+
46+
InetSocketAddress isa = new InetSocketAddress(ia, ss.getLocalPort());
47+
s1 = new Socket();
48+
s1.connect(isa);
49+
} catch (Exception e) {
50+
e.printStackTrace();
51+
throw new RuntimeException("Test Failed!");
52+
} finally {
53+
// close this socket
54+
if (s1 != null) {
55+
s1.close();
56+
// We should return true even after the socket is closed, to keep align with OpenJDK.
57+
assertTrue(s1.isConnected());
58+
}
59+
60+
if (ss != null) ss.close();
61+
}
62+
}
63+
64+
private static void testIsConnectedAfterConnectionFailure() throws Exception {
65+
ServerSocket ss = null;
66+
Socket s1 = null;
67+
InetAddress ia = InetAddress.getLocalHost();
68+
try {
69+
ss = new ServerSocket(0);
70+
ss.setSoTimeout(1000);
71+
72+
InetSocketAddress isa = new InetSocketAddress(ia, ss.getLocalPort());
73+
s1 = new Socket();
74+
s1.connect(isa, -1); // exception here
75+
} catch (IOException e) {
76+
e.printStackTrace();
77+
throw new RuntimeException("Test Failed!");
78+
} catch (IllegalArgumentException e) {
79+
// we shall go here.
80+
assertFalse(s1.isConnected());
81+
} finally {
82+
if (ss != null) ss.close();
83+
if (s1 != null) s1.close();
84+
}
85+
}
86+
87+
public static void main(String args[]) throws Exception {
88+
testIsConnectedAfterClosing();
89+
testIsConnectedAfterConnectionFailure();
90+
}
91+
}

0 commit comments

Comments
 (0)