Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,23 @@ public void onMessage(final String result) {
if (LOG.isDebugEnabled()) {
LOG.debug("onMessage server[{}] result({})", this.getURI().toString(), result);
}

Map<String, Object> jsonToMap = JsonUtils.jsonToMap(result);
Object eventType = jsonToMap.get(RunningModeConstants.EVENT_TYPE);
if (Objects.equals(DataEventTypeEnum.RUNNING_MODE.name(), eventType)) {
LOG.info("server[{}] handle running mode result({})", this.getURI().toString(), result);
this.runningMode = String.valueOf(jsonToMap.get(RunningModeConstants.RUNNING_MODE));
if (Objects.equals(RunningModeEnum.STANDALONE.name(), runningMode)) {
return;

try {
Map<String, Object> jsonToMap = JsonUtils.jsonToMap(result);
Object eventType = jsonToMap.get(RunningModeConstants.EVENT_TYPE);
if (Objects.equals(DataEventTypeEnum.RUNNING_MODE.name(), eventType)) {
LOG.info("server[{}] handle running mode result({})", this.getURI().toString(), result);
this.runningMode = String.valueOf(jsonToMap.get(RunningModeConstants.RUNNING_MODE));
if (Objects.equals(RunningModeEnum.STANDALONE.name(), runningMode)) {
return;
}
this.masterUrl = String.valueOf(jsonToMap.get(RunningModeConstants.MASTER_URL));
this.isConnectedToMaster = Boolean.TRUE.equals(jsonToMap.get(RunningModeConstants.IS_MASTER));
} else {
handleResult(result);
}
this.masterUrl = String.valueOf(jsonToMap.get(RunningModeConstants.MASTER_URL));
this.isConnectedToMaster = Boolean.TRUE.equals(jsonToMap.get(RunningModeConstants.IS_MASTER));
} else {
handleResult(result);
} catch (RuntimeException ex) {
LOG.warn("Failed to handle websocket message from server[{}], the message will be ignored", this.getURI(), ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,32 @@ public void testOnMessage() {
verify(pluginDataSubscriber).onSubscribe(any());
}

@Test
public void testOnMessageShouldIgnoreMalformedJsonAndHandleNextMessage() {
Assertions.assertDoesNotThrow(() -> shenyuWebsocketClient.onMessage("{invalid json"));

doNothing().when(pluginDataSubscriber).onSubscribe(any());
String json = GsonUtils.getInstance().toJson(websocketData);
shenyuWebsocketClient.onMessage(json);
verify(pluginDataSubscriber).onSubscribe(any());
}

@Test
public void testOnMessageShouldIgnoreUnknownGroupType() {
websocketData.setGroupType("UNKNOWN_GROUP");
String json = GsonUtils.getInstance().toJson(websocketData);

Assertions.assertDoesNotThrow(() -> shenyuWebsocketClient.onMessage(json));
}

@Test
public void testOnMessageShouldIgnoreUnknownEventType() {
websocketData.setEventType("UNKNOWN_EVENT");
String json = GsonUtils.getInstance().toJson(websocketData);

Assertions.assertDoesNotThrow(() -> shenyuWebsocketClient.onMessage(json));
}

@Test
public void testOnClose() {
shenyuWebsocketClient = spy(shenyuWebsocketClient);
Expand Down
Loading