diff --git a/test/jdk/com/alibaba/wisp/TestIo.java b/test/jdk/com/alibaba/wisp/TestIo.java index 578bbb8db6c..53d3449df24 100644 --- a/test/jdk/com/alibaba/wisp/TestIo.java +++ b/test/jdk/com/alibaba/wisp/TestIo.java @@ -4,8 +4,7 @@ * @modules java.base/jdk.internal.misc * @requires os.family == "linux" * @requires os.arch != "riscv64" - * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableCoroutine -Dcom.alibaba.wisp.transparentWispSwitch=true TestIo - * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableCoroutine -Dcom.alibaba.wisp.transparentWispSwitch=true TestIo + * @run main/othervm/timeout=10 -XX:+UnlockExperimentalVMOptions -XX:+EnableCoroutine -Dcom.alibaba.wisp.transparentWispSwitch=true -Dcom.alibaba.wisp.threadAsWisp.black=name:Local-Server TestIo */ @@ -20,25 +19,25 @@ import java.nio.channels.SocketChannel; import java.util.Properties; -public class TestIo { +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.ServerSocket; +import java.net.Socket; - static Properties p; - static String socketAddr; - static { - p = java.security.AccessController.doPrivileged( - new java.security.PrivilegedAction<Properties>() { - public Properties run() { - return System.getProperties(); - } - } - ); - socketAddr = (String)p.get("test.wisp.socketAddress"); - if (socketAddr == null) { - socketAddr = "www.example.com"; - } - } +public class TestIo { + static int port = 28888; + static String socketAddr = "127.0.0.1"; + static ServerSocket ss; public static void main(String[] args) { + try { + runServerThread(); + } catch (Exception e) { + e.printStackTrace(); + throw new Error("cannot create a server socket"); + } String result = http(); System.out.println(result); @@ -49,16 +48,59 @@ public static void main(String[] args) { throw new Error("protocol error"); } + + /** An Example of Http Response + * HTTP/1.0 200 OK + * Content-Encoding: gzip + * Accept-Ranges: bytes + * Age: 482198 + * Cache-Control: max-age=604800 + * Content-Type: text/html; charset=UTF-8 + * Date: Wed, 31 Jul 2024 08:38:52 GMT + * Etag: "3147526947" + * Expires: Wed, 07 Aug 2024 08:38:52 GMT + * Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT + * Server: ECAcc (sac/2533) + * X-Cache: HIT + * Content-Length: 648 + * Connection: close + */ + private static void runServerThread() throws Exception { + ss = new ServerSocket(port, 0, null); + Thread serverThread = new Thread(()->{ + try { + while (true) { + System.out.println("waiting......"); + Socket s = ss.accept(); + BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); + String mess = br.readLine(); + assert mess.contains("HEAD / HTTP/1.0"); + String response = "HTTP/1.0 200 OK\r\n" + + "Content-type:text/html; charset=UTF-8\r\n" + + "Server: ECAcc (sac/2533)\r\n" + "\r\n"; + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + bw.write(response); + bw.flush(); + s.close(); + ss.close(); + break; + } + } catch (Exception e) { + e.printStackTrace(); + } + }, "Local-Server"); + serverThread.start(); + } + static String http() { - String host = socketAddr; try { SocketChannel ch = SocketChannel.open(); ch.configureBlocking(false); - connect(ch, InetAddress.getByName(host).getHostAddress()); + connect(ch, socketAddr); ByteBuffer bb = ByteBuffer.allocate(1000); - String request = "HEAD / HTTP/1.0\r\nHost:" + host + "\r\n\r\n"; + String request = "HEAD / HTTP/1.0\r\nHost:" + socketAddr + "\r\n\r\n"; bb.put(request.getBytes()); bb.flip(); write(ch, bb); @@ -94,7 +136,7 @@ static int write(SocketChannel ch, ByteBuffer bb) throws IOException { } static void connect(SocketChannel ch, String ip) throws IOException { - ch.connect(new InetSocketAddress(ip, 80)); + ch.connect(new InetSocketAddress(ip, port)); int retry = 0; while (!ch.finishConnect()) { if (retry++ > 3) throw new Error("busy loop");