fix(接口测试): 修复调试时websocket 同步到消息内容问题

This commit is contained in:
fit2-zhao 2021-12-22 11:32:08 +08:00 committed by fit2-zhao
parent af124898fa
commit 3c4058042f
7 changed files with 44 additions and 56 deletions

View File

@ -68,9 +68,9 @@ public class JMeterService {
}
private void addDebugListener(String testId, HashTree testPlan) {
MsDebugListener resultCollector = new MsDebugListener();
MsResultCollector resultCollector = new MsResultCollector();
resultCollector.setName(testId);
resultCollector.setProperty(TestElement.TEST_CLASS, MsDebugListener.class.getName());
resultCollector.setProperty(TestElement.TEST_CLASS, MsResultCollector.class.getName());
resultCollector.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ViewResultsFullVisualizer"));
resultCollector.setEnabled(true);
testPlan.add(testPlan.getArray()[0], resultCollector);

View File

@ -23,11 +23,10 @@ import io.metersphere.api.service.MsResultService;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.RequestResult;
import io.metersphere.dto.ResultDTO;
import io.metersphere.jmeter.JMeterBase;
import io.metersphere.utils.JMeterVars;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.websocket.c.to.c.MsWebSocketClient;
import io.metersphere.websocket.c.to.c.WebSocketUtils;
import io.metersphere.websocket.c.to.c.util.MsgDto;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -44,7 +43,7 @@ import java.util.Map;
/**
* 实时结果监听
*/
public class MsDebugListener extends AbstractListenerElement implements SampleListener, Clearable, Serializable,
public class MsResultCollector extends AbstractListenerElement implements SampleListener, Clearable, Serializable,
TestStateListener, Remoteable, NoThreadClone {
private static final String ERROR_LOGGING = "MsResultCollector.error_logging"; // $NON-NLS-1$
@ -55,11 +54,9 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
public static final String TEST_END = "MS_TEST_END";
private MsWebSocketClient client;
@Override
public Object clone() {
MsDebugListener clone = (MsDebugListener) super.clone();
MsResultCollector clone = (MsResultCollector) super.clone();
return clone;
}
@ -100,28 +97,20 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
@Override
public void testEnded(String host) {
LoggerUtil.debug("TestEnded " + this.getName());
SampleResult result = new SampleResult();
result.setResponseCode(TEST_END);
ResultDTO dto = new ResultDTO();
dto.setReportId(this.getName());
try {
if (client != null) {
client.close();
}
} catch (Exception e) {
LogUtil.error(e);
}
MsgDto dto = new MsgDto();
dto.setExecEnd(false);
dto.setContent(TEST_END);
dto.setReportId("send." + this.getName());
dto.setToReport(this.getName());
LoggerUtil.debug("send. " + this.getName());
WebSocketUtils.sendMessageSingle(dto);
WebSocketUtils.onClose(this.getName());
}
@Override
public void testStarted(String host) {
LogUtil.debug("TestStarted " + this.getName());
try {
client = new MsWebSocketClient("ws://127.0.0.1:8081/ws/" + "send." + this.getName());
client.connect();
} catch (Exception e) {
LogUtil.error(e);
}
}
@Override
@ -142,10 +131,9 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
dto.setReportId("send." + this.getName());
dto.setToReport(this.getName());
LoggerUtil.debug("send. " + this.getName());
if (client != null) {
client.send(JSON.toJSONString(dto));
}
WebSocketUtils.sendMessageSingle(dto);
} catch (Exception ex) {
LoggerUtil.error("消息推送失败:" + ex.getMessage());
}
}
@ -179,9 +167,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
dto.setReportId("send." + this.getName());
dto.setToReport(this.getName());
LoggerUtil.debug("send. " + this.getName());
if (client != null) {
client.send(JSON.toJSONString(dto));
}
WebSocketUtils.sendMessageSingle(dto);
}
}
}

View File

@ -22,7 +22,7 @@ public class IndexWebSocket {
public void openSession(@PathParam("reportId") String reportId, Session session) {
WebSocketUtils.ONLINE_USER_SESSIONS.put(reportId, session);
log.info("客户端: [" + reportId + "] : 连接成功!");
WebSocketUtils.sendMessageAll("客户端: [" + reportId + "] : 连接成功!");
//WebSocketUtils.sendMessageAll("客户端: [" + reportId + "] : 连接成功!");
}
/**
@ -45,7 +45,7 @@ public class IndexWebSocket {
WebSocketUtils.ONLINE_USER_SESSIONS.remove(reportId);
log.info("[" + reportId + "] : 断开连接!");
//并且通知其他人当前用户已经断开连接了
WebSocketUtils.sendMessageAll("[" + reportId + "] : 断开连接!");
//WebSocketUtils.sendMessageAll("[" + reportId + "] : 断开连接!");
session.close();
}

View File

@ -1,5 +1,6 @@
package io.metersphere.websocket.c.to.c;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.websocket.c.to.c.util.MsgDto;
import javax.websocket.RemoteEndpoint;
@ -12,9 +13,13 @@ public class WebSocketUtils {
// 单用户推送
public static void sendMessage(Session session, String message) {
if (session == null) { return; }
if (session == null) {
return;
}
RemoteEndpoint.Async async = session.getAsyncRemote();
if (async == null) { return; }
if (async == null) {
return;
}
async.sendText(message);
}
@ -30,4 +35,20 @@ public class WebSocketUtils {
sendMessage(session, message);
});
}
//当前的Session 移除
public static void onClose(String reportId) {
try {
if (WebSocketUtils.ONLINE_USER_SESSIONS.containsKey(reportId)) {
WebSocketUtils.ONLINE_USER_SESSIONS.get(reportId).close();
WebSocketUtils.ONLINE_USER_SESSIONS.remove(reportId);
}
if (WebSocketUtils.ONLINE_USER_SESSIONS.containsKey(("send." + reportId))) {
WebSocketUtils.ONLINE_USER_SESSIONS.get(("send." + reportId)).close();
WebSocketUtils.ONLINE_USER_SESSIONS.remove(("send." + reportId));
}
} catch (Exception e) {
LoggerUtil.error("关闭socket失败" + e.getMessage());
}
}
}

View File

@ -1,19 +0,0 @@
package io.metersphere.websocket.c.to.c.util;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author: jason
* @Date: 2020-12-23
*/
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class SocketClient {
private Integer userId;
private String username;
}

View File

@ -356,7 +356,7 @@ export default {
if (e.data) {
this.runningEvaluation(e.data);
}
if (e.data && e.data.indexOf("断开连接") !== -1) {
if (e.data && e.data.indexOf("MS_TEST_END") !== -1) {
this.getReport();
this.messageWebSocket.close();
scenario.$emit('hide', this.scenarioId);

View File

@ -789,7 +789,7 @@ export default {
}
this.runningEvaluation(e.data);
this.message = getUUID();
if (e.data && e.data.indexOf("断开连接") !== -1) {
if (e.data && e.data.indexOf("MS_TEST_END") !== -1) {
this.runScenario = undefined;
this.debugLoading = false;
this.message = "stop";