Skip to content

Commit 8fa5b7f

Browse files
committed
Synchronization added and a better waiting for messages in a test.
1 parent e96759a commit 8fa5b7f

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

tools/src/com.oracle.truffle.tools.chromeinspector.test/src/com/oracle/truffle/tools/chromeinspector/test/InspectorMessageTransportTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ public void inspectorReconnectTest() throws IOException, InterruptedException {
134134
}
135135
// We will not get the last 4 messages as we do not do initial suspension on re-connect
136136
int expectedNumMessages = 2 * MESSAGES.length - 4;
137-
while (session.messages.size() < expectedNumMessages) {
138-
// The reply messages are sent asynchronously. We need to wait for them.
139-
Thread.sleep(100);
137+
synchronized (session.messages) {
138+
while (session.messages.size() < expectedNumMessages) {
139+
// The reply messages are sent asynchronously. We need to wait for them.
140+
session.messages.wait();
141+
}
140142
}
141143

142144
Assert.assertEquals(session.messages.toString(), expectedNumMessages, session.messages.size());
@@ -226,7 +228,10 @@ private static final class BasicRemote {
226228

227229
void sendText(String text) throws IOException {
228230
if (!text.startsWith("{\"method\":\"Debugger.scriptParsed\"")) {
229-
messages.add("toClient(" + text + ")");
231+
synchronized (messages) {
232+
messages.add("toClient(" + text + ")");
233+
messages.notifyAll();
234+
}
230235
}
231236
if (text.startsWith("{\"method\":\"Debugger.paused\"")) {
232237
handler.onMessage("{\"id\":100,\"method\":\"Debugger.resume\"}");

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/instrument/InspectorInstrument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,10 @@ private static final class Server {
420420
info.println(" " + address);
421421
info.flush();
422422
} else {
423+
restartServerEndpointOnClose(hostAndPort, env, wsuri, executionContext, connectionWatcher, iss, interceptor);
423424
interceptor.opened(serverEndpoint);
424425
wss = interceptor;
425426
wsURL = wsuri.toString();
426-
restartServerEndpointOnClose(hostAndPort, env, wsuri, executionContext, connectionWatcher, iss, interceptor);
427427
}
428428
}
429429
if (debugBreak || waitAttached) {

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/server/InspectServerSession.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public final class InspectServerSession implements MessageEndpoint {
6262
final InspectorExecutionContext context;
6363
private volatile MessageEndpoint messageEndpoint;
6464
private volatile JSONMessageListener jsonMessageListener;
65-
private CommandProcessThread processThread;
65+
private volatile CommandProcessThread processThread;
6666
private Runnable onClose;
6767

6868
private InspectServerSession(RuntimeDomain runtime, DebuggerDomain debugger, ProfilerDomain profiler,
@@ -86,15 +86,19 @@ public void onClose(Runnable onCloseTask) {
8686

8787
@Override
8888
public void sendClose() {
89-
runtime.disable();
90-
debugger.disable();
91-
profiler.disable();
92-
context.reset();
93-
messageEndpoint = null;
94-
processThread.dispose();
95-
processThread = null;
96-
if (onClose != null) {
97-
onClose.run();
89+
Runnable onCloseRunnable = null;
90+
synchronized (this) {
91+
runtime.disable();
92+
debugger.disable();
93+
profiler.disable();
94+
context.reset();
95+
messageEndpoint = null;
96+
processThread.dispose();
97+
processThread = null;
98+
onCloseRunnable = onClose;
99+
}
100+
if (onCloseRunnable != null) {
101+
onCloseRunnable.run();
98102
}
99103
}
100104

@@ -103,7 +107,7 @@ public DebuggerDomain getDebugger() {
103107
return debugger;
104108
}
105109

106-
public void setMessageListener(MessageEndpoint messageListener) {
110+
public synchronized void setMessageListener(MessageEndpoint messageListener) {
107111
this.messageEndpoint = messageListener;
108112
if (messageListener != null && processThread == null) {
109113
EventHandler eh = new EventHandlerImpl();
@@ -115,7 +119,7 @@ public void setMessageListener(MessageEndpoint messageListener) {
115119
}
116120
}
117121

118-
public void setJSONMessageListener(JSONMessageListener messageListener) {
122+
public synchronized void setJSONMessageListener(JSONMessageListener messageListener) {
119123
this.jsonMessageListener = messageListener;
120124
if (messageListener != null && processThread == null) {
121125
EventHandler eh = new EventHandlerImpl();
@@ -139,14 +143,20 @@ public void sendText(String message) {
139143
}
140144
return;
141145
}
142-
processThread.push(cmd);
146+
CommandProcessThread pt = processThread;
147+
if (pt != null) {
148+
pt.push(cmd);
149+
}
143150
}
144151

145152
public void sendCommand(Command cmd) {
146153
if (context.isSynchronous()) {
147154
sendCommandSync(cmd);
148155
} else {
149-
processThread.push(cmd);
156+
CommandProcessThread pt = processThread;
157+
if (pt != null) {
158+
pt.push(cmd);
159+
}
150160
}
151161
}
152162

0 commit comments

Comments
 (0)