Skip to content

Commit

Permalink
Merge pull request #39109 from yrodiere/devui-test-assertion-improvement
Browse files Browse the repository at this point in the history
Correctly forward websocket errors in DevUIJsonRPCTest
  • Loading branch information
phillip-kruger authored Mar 3, 2024
2 parents 1b72a2e + e386b14 commit 9a710df
Showing 1 changed file with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ private JsonNode objectResultFromJsonRPC(int id) throws InterruptedException, Js
}

private JsonNode objectResultFromJsonRPC(int id, int loopCount) throws InterruptedException, JsonProcessingException {
if (MESSAGES.containsKey(id)) {
String response = MESSAGES.remove(id);
if (RESPONSES.containsKey(id)) {
WebSocketResponse response = RESPONSES.remove(id);
if (response != null) {
ObjectNode json = (ObjectNode) new ObjectMapper().readTree(response);
ObjectNode json = (ObjectNode) new ObjectMapper().readTree(response.message());
JsonNode result = json.get("result");
if (result != null) {
return result.get("object");
Expand Down Expand Up @@ -212,25 +212,48 @@ private int sendRequest(String methodName, Map<String, Object> params) throws IO
socket.frameHandler((e) -> {
Buffer b = accumulatedBuffer.appendBuffer(e.binaryData());
if (e.isFinal()) {
MESSAGES.put(id, b.toString());
RESPONSES.put(id, new WebSocketResponse(b.toString()));
}
});

socket.writeTextMessage(request);

socket.exceptionHandler((e) -> {
e.printStackTrace();
RESPONSES.put(id, new WebSocketResponse(e));
vertx.close();
});
socket.closeHandler(v -> {
vertx.close();
});
} else {
RESPONSES.put(id, new WebSocketResponse(ar.cause()));
vertx.close();
}
});
return id;
}

private static final ConcurrentHashMap<Integer, String> MESSAGES = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<Integer, WebSocketResponse> RESPONSES = new ConcurrentHashMap<>();

private static class WebSocketResponse {
private final String message;
private final Throwable throwable;

public WebSocketResponse(String message) {
this.message = message;
this.throwable = null;
}

public WebSocketResponse(Throwable throwable) {
this.message = null;
this.throwable = throwable;
}

String message() {
if (throwable != null) {
throw new IllegalStateException("Request failed: " + throwable.getMessage(), throwable);
}
return message;
}
}
}

0 comments on commit 9a710df

Please sign in to comment.