Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wisp] Fix initialization problem between JavaNetSocketAccess and its usage #443

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jdk/src/linux/classes/com/alibaba/wisp/engine/WispEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.dyn.CoroutineExitException;
import java.dyn.CoroutineSupport;
import java.io.IOException;
import java.net.Socket;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.util.*;
Expand Down Expand Up @@ -148,6 +149,7 @@ private static void initializeClasses() {
Class.forName(ShutdownEngine.class.getName());
Class.forName(AbstractShutdownTask.class.getName());
Class.forName(ShutdownControlGroup.class.getName());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some comments to explain why we need this.

Class.forName(Socket.class.getName());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaNetSocketAccess is only used by ServerSocket, I suggest moving setJavaNetSocketAccess from Socket.<clinit> to ServerSocket.<clinit>. Thus, we don't need to preload Socket.class.

if (WispConfiguration.WISP_PROFILE) {
Class.forName(WispPerfCounterMonitor.class.getName());
}
Expand Down
37 changes: 32 additions & 5 deletions jdk/test/com/alibaba/wisp/io/ServerSocketConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,43 @@
* @summary Test ServerSocket isConnected() method
* @requires os.family == "linux"
* @library /lib/testlibrary
* @build SimpleClient
* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableCoroutine -XX:+UseWispMonitor -Dcom.alibaba.wisp.transparentWispSwitch=true ServerSocketConnectionTest
*/

import java.io.IOException;
import java.net.*;
import static jdk.testlibrary.Asserts.assertTrue;
import static jdk.testlibrary.Asserts.assertFalse;

import static jdk.testlibrary.Asserts.*;


public class ServerSocketConnectionTest {

private static final String CLIENT = "SimpleClient";
private static final String PORT = "4039";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, using a random port makes the test more stable.

Copy link
Collaborator

@D-D-H D-D-H Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use 0

port – the port number, or 0 to use a port number that is automatically allocated.


public static void testJavaNetSocketAccessInitialization() throws Exception {
ServerSocket ss = null;
try {
ss = new ServerSocket(Integer.parseInt(PORT));
// We should avoid using the Socket class here to reproduce this issue.
// So we use another process here.
ProcessBuilder pb = jdk.testlibrary.ProcessTools.createJavaProcessBuilder(
CLIENT,
PORT // port
);
pb.start();
ss.accept();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept after started? is this intentional?

} catch (Exception e) {
e.printStackTrace();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Asserts.fail() for better readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

fail("Test Failed!");
} finally {
if (ss != null) {
ss.close();
}
}
}

private static void testIsConnectedAfterClosing() throws Exception {
ServerSocket ss = null;
Socket s1 = null;
Expand All @@ -48,7 +74,7 @@ private static void testIsConnectedAfterClosing() throws Exception {
s1.connect(isa);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Test Failed!");
fail("Test Failed!");
} finally {
// close this socket
if (s1 != null) {
Expand All @@ -74,7 +100,7 @@ private static void testIsConnectedAfterConnectionFailure() throws Exception {
s1.connect(isa, -1); // exception here
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Test Failed!");
fail("Test Failed!");
} catch (IllegalArgumentException e) {
// we shall go here.
assertFalse(s1.isConnected());
Expand All @@ -93,7 +119,7 @@ private static void testIsConnectedAfterAcception() throws Exception {
s1 = ss.accept();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Test Failed!");
fail("Test Failed!");
} finally {
if (s1 != null) {
assertTrue(s1.isConnected());
Expand All @@ -105,6 +131,7 @@ private static void testIsConnectedAfterAcception() throws Exception {
}

public static void main(String args[]) throws Exception {
testJavaNetSocketAccessInitialization();
testIsConnectedAfterClosing();
testIsConnectedAfterConnectionFailure();
testIsConnectedAfterAcception();
Expand Down
14 changes: 14 additions & 0 deletions jdk/test/com/alibaba/wisp/io/SimpleClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.*;
import java.net.Socket;

public class SimpleClient {

public static void main(String[] args) throws Exception {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), Integer.parseInt(args[0])));
System.out.println(socket.getRemoteSocketAddress());
}

}