Skip to content

Commit

Permalink
Showing 3 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -87,9 +87,19 @@ public void inspectorEndpointExplicitPathTest() {
inspectorEndpointTest("/some/complex/path");
}

@Test
public void inspectorEndpointRaceTest() {
RaceControl rc = new RaceControl();
inspectorEndpointTest(null, rc);
}

private static void inspectorEndpointTest(String path) {
Session session = new Session();
DebuggerEndpoint endpoint = new DebuggerEndpoint(path);
inspectorEndpointTest(path, null);
}

private static void inspectorEndpointTest(String path, RaceControl rc) {
Session session = new Session(rc);
DebuggerEndpoint endpoint = new DebuggerEndpoint(path, rc);
Engine engine = endpoint.onOpen(session);

Context context = Context.newBuilder().engine(engine).build();
@@ -124,10 +134,15 @@ public MessageEndpoint open(URI uri, MessageEndpoint peerEndpoint) throws IOExce

private static final class Session {

private final RaceControl rc;
final List<String> messages = new ArrayList<>(MESSAGES.length);
private final BasicRemote remote = new BasicRemote(messages);
private boolean opened = true;

Session(RaceControl rc) {
this.rc = rc;
}

BasicRemote getBasicRemote() {
return remote;
}
@@ -139,6 +154,9 @@ public boolean isOpen() {
void addMessageHandler(MsgHandler handler) throws IOException {
remote.handler = handler;
sendInitialMessages(handler);
if (rc != null) {
rc.clientMessagesSent();
}
}

private static void sendInitialMessages(final MsgHandler handler) throws IOException {
@@ -179,9 +197,11 @@ void sendText(String text) throws IOException {
private static final class DebuggerEndpoint {

private final String path;
private final RaceControl rc;

DebuggerEndpoint(String path) {
DebuggerEndpoint(String path, RaceControl rc) {
this.path = path;
this.rc = rc;
}

public Engine onOpen(final Session session) {
@@ -198,7 +218,11 @@ public MessageEndpoint open(URI requestURI, MessageEndpoint peerEndpoint) throws
String absolutePath = path.startsWith("/") ? path : "/" + path;
Assert.assertTrue(uriStr, uriStr.endsWith(":" + PORT + absolutePath));
}
return new ChromeDebuggingProtocolMessageHandler(session, requestURI, peerEndpoint);
MessageEndpoint ourEndpoint = new ChromeDebuggingProtocolMessageHandler(session, requestURI, peerEndpoint);
if (rc != null) {
rc.waitTillClientDataAreSent();
}
return ourEndpoint;
}
}).option("inspect", PORT);
if (path != null) {
@@ -260,4 +284,29 @@ public void sendClose() throws IOException {

}

// Test a possible race between data sent and transport open.
// The WSInterceptorServer needs to be able to deal with sendText() being called before opened()
private static final class RaceControl {

private boolean clientSent = false;

private synchronized void clientMessagesSent() {
clientSent = true;
notifyAll();
}

private synchronized void waitTillClientDataAreSent() {
try {
while (!clientSent) {
wait();
}
// The data were sent, but it takes a while to process them till
// WSInterceptorServer#sendText gets called.
Thread.sleep(100);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -261,7 +261,7 @@ private static final class Server {
throw new IOException(ex);
}
InspectServerSession iss = InspectServerSession.create(executionContext, debugBreak, connectionWatcher);
WSInterceptorServer interceptor = new WSInterceptorServer(wsuri, iss);
WSInterceptorServer interceptor = new WSInterceptorServer(wsuri, iss, connectionWatcher);
MessageEndpoint serverEndpoint;
try {
serverEndpoint = env.startServer(wsuri, iss);
@@ -277,7 +277,7 @@ private static final class Server {
info.println(" " + address);
info.flush();
} else {
interceptor.opened(serverEndpoint, connectionWatcher);
interceptor.opened(serverEndpoint);
wss = interceptor;
}
}
Original file line number Diff line number Diff line change
@@ -39,18 +39,18 @@
public final class WSInterceptorServer implements InspectorWSConnection, MessageEndpoint {

private final URI uri;
private final ConnectionWatcher connectionWatcher;
private MessageEndpoint inspectEndpoint;
private ConnectionWatcher connectionWatcher;

public WSInterceptorServer(URI uri, InspectServerSession iss) {
public WSInterceptorServer(URI uri, InspectServerSession iss, ConnectionWatcher connectionWatcher) {
this.uri = uri;
this.connectionWatcher = connectionWatcher;
iss.setMessageListener(this);
}

public void opened(MessageEndpoint endpoint, ConnectionWatcher cw) {
public void opened(MessageEndpoint endpoint) {
this.inspectEndpoint = endpoint;
this.connectionWatcher = cw;
cw.notifyOpen();
this.connectionWatcher.notifyOpen();
}

@Override

0 comments on commit 634dc88

Please sign in to comment.