Skip to content

Commit

Permalink
[Wisp] improve NIO testcase for wisp
Browse files Browse the repository at this point in the history
Summary: as title

Testing: test/jdk/com/alibaba/wisp/TestIo.java

Reviewers: yulei.lx, lusou.zq

Issue: #925
  • Loading branch information
jia-wei-tang committed Jan 22, 2025
1 parent db5e1b1 commit 36a8a63
Showing 1 changed file with 64 additions and 22 deletions.
86 changes: 64 additions & 22 deletions test/jdk/com/alibaba/wisp/TestIo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/


Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 36a8a63

Please sign in to comment.