|
| 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