Skip to content

Commit a683c41

Browse files
committed
Add option to debug ClipboardTest remote application
Added RemoteClipboard.DEBUG_REMOTE so that ClipboardTest can be launched as a standalone java application to make it easier to debug the remote end of the tests. Part of #2126
1 parent 10c8fb6 commit a683c41

File tree

3 files changed

+62
-44
lines changed

3 files changed

+62
-44
lines changed

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/RemoteClipboard.java

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,63 @@
3535
import clipboard.ClipboardCommands;
3636

3737
public class RemoteClipboard implements ClipboardCommands {
38+
/**
39+
* Set to true to manually launch the ClipboardTest. This is useful so you can
40+
* debug the Swing side.
41+
*/
42+
private static boolean DEBUG_REMOTE = false;
3843
private ClipboardCommands remote;
3944
private Process remoteClipboardProcess;
4045
private Path remoteClipboardTempDir;
4146

4247
public void start() throws Exception {
4348
assertNull(remote, "Create a new instance to restart");
49+
50+
int port = DEBUG_REMOTE ? ClipboardCommands.DEFAULT_PORT : launchRemote();
51+
try {
52+
Registry reg = LocateRegistry.getRegistry("127.0.0.1", port);
53+
long stopTime = System.currentTimeMillis() + 10000;
54+
do {
55+
try {
56+
remote = (ClipboardCommands) reg.lookup(ClipboardCommands.ID);
57+
break;
58+
} catch (NotBoundException e) {
59+
// try again because the remote app probably hasn't bound yet
60+
}
61+
} while (System.currentTimeMillis() < stopTime);
62+
} catch (RemoteException e) {
63+
64+
Integer exitValue = null;
65+
boolean waitFor = false;
66+
try {
67+
waitFor = remoteClipboardProcess.waitFor(5, TimeUnit.SECONDS);
68+
if (waitFor) {
69+
exitValue = remoteClipboardProcess.exitValue();
70+
}
71+
} catch (InterruptedException e1) {
72+
Thread.interrupted();
73+
}
74+
75+
String message = "Failed to get remote clipboards command, this seems to happen on macOS on I-build tests. Exception: "
76+
+ e.toString() + " waitFor: " + waitFor + " exitValue: " + exitValue;
77+
78+
// Give some diagnostic information to help track down why this fails on build
79+
// machine. We only hard error on Linux, for other platforms we allow test to
80+
// just be skipped until we track down what is causing
81+
// https://github.com/eclipse-platform/eclipse.platform.swt/issues/2553
82+
assumeTrue(SwtTestUtil.isGTK, message);
83+
throw new RuntimeException(message, e);
84+
}
85+
assertNotNull(remote);
86+
87+
// Run a no-op on the Swing event loop so that we know it is idle
88+
// and we can continue startup
89+
remote.waitUntilReady();
90+
remote.setFocus();
91+
remote.waitUntilReady();
92+
}
93+
94+
private int launchRemote() throws IOException {
4495
/*
4596
* The below copy using getPath may be redundant (i.e. it may be possible to run
4697
* the class files from where they currently reside in the bin folder or the
@@ -66,7 +117,7 @@ public void start() throws Exception {
66117
String javaExe = javaHome + "/bin/java" + (SwtTestUtil.isWindowsOS ? ".exe" : "");
67118
assertTrue(Files.exists(Path.of(javaExe)));
68119

69-
ProcessBuilder pb = new ProcessBuilder(javaExe, "clipboard.ClipboardTest")
120+
ProcessBuilder pb = new ProcessBuilder(javaExe, "clipboard.ClipboardTest", "-autoport")
70121
.directory(remoteClipboardTempDir.toFile());
71122
pb.inheritIO();
72123
pb.redirectOutput(Redirect.PIPE);
@@ -85,47 +136,7 @@ public void start() throws Exception {
85136
throw new RuntimeException("Failed to get port");
86137
});
87138
assertNotEquals(0, port);
88-
try {
89-
Registry reg = LocateRegistry.getRegistry("127.0.0.1", port);
90-
long stopTime = System.currentTimeMillis() + 10000;
91-
do {
92-
try {
93-
remote = (ClipboardCommands) reg.lookup(ClipboardCommands.ID);
94-
break;
95-
} catch (NotBoundException e) {
96-
// try again because the remote app probably hasn't bound yet
97-
}
98-
} while (System.currentTimeMillis() < stopTime);
99-
} catch (RemoteException e) {
100-
101-
Integer exitValue = null;
102-
boolean waitFor = false;
103-
try {
104-
waitFor = remoteClipboardProcess.waitFor(5, TimeUnit.SECONDS);
105-
if (waitFor) {
106-
exitValue = remoteClipboardProcess.exitValue();
107-
}
108-
} catch (InterruptedException e1) {
109-
Thread.interrupted();
110-
}
111-
112-
String message = "Failed to get remote clipboards command, this seems to happen on macOS on I-build tests. Exception: "
113-
+ e.toString() + " waitFor: " + waitFor + " exitValue: " + exitValue;
114-
115-
// Give some diagnostic information to help track down why this fails on build
116-
// machine. We only hard error on Linux, for other platforms we allow test to
117-
// just be skipped until we track down what is causing
118-
// https://github.com/eclipse-platform/eclipse.platform.swt/issues/2553
119-
assumeTrue(SwtTestUtil.isGTK, message);
120-
throw new RuntimeException(message, e);
121-
}
122-
assertNotNull(remote);
123-
124-
// Run a no-op on the Swing event loop so that we know it is idle
125-
// and we can continue startup
126-
remote.waitUntilReady();
127-
remote.setFocus();
128-
remote.waitUntilReady();
139+
return port;
129140
}
130141

131142
@Override
@@ -207,7 +218,7 @@ public void waitUntilReady() throws RemoteException {
207218
}
208219

209220
@Override
210-
public void waitForButtonPress() throws RemoteException {
221+
public void waitForButtonPress() throws RemoteException {
211222
remote.waitForButtonPress();
212223
}
213224
}

tests/org.eclipse.swt.tests/data/clipboard/ClipboardCommands.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public interface ClipboardCommands extends Remote {
2626
*/
2727
int SELECTION_CLIPBOARD = 1 << 1;
2828

29+
/**
30+
* The port the RMI will listen on by default
31+
*/
32+
int DEFAULT_PORT = 3456;
2933

3034
void stop() throws RemoteException;
3135

tests/org.eclipse.swt.tests/data/clipboard/ClipboardTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.rmi.registry.LocateRegistry;
2424
import java.rmi.registry.Registry;
2525
import java.rmi.server.RMISocketFactory;
26+
import java.util.Arrays;
2627

2728
import javax.swing.JButton;
2829
import javax.swing.JFrame;
@@ -117,9 +118,11 @@ public void log(String log) {
117118
public static void main(String[] args) throws IOException {
118119
System.setProperty("java.rmi.server.hostname", "127.0.0.1");
119120

121+
boolean autoPort = Arrays.asList(args).contains("-autoport");
122+
120123
// Make sure RMI is localhost only
121124
RMISocketFactory.setSocketFactory(new LocalHostOnlySocketFactory());
122-
int chosenPort = getAvailablePort();
125+
int chosenPort = autoPort ? getAvailablePort() : ClipboardCommands.DEFAULT_PORT;
123126
rmiRegistry = LocateRegistry.createRegistry(chosenPort);
124127
System.out.println(ClipboardCommands.PORT_MESSAGE + chosenPort);
125128

0 commit comments

Comments
 (0)