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) { private void addDebugListener(String testId, HashTree testPlan) {
MsDebugListener resultCollector = new MsDebugListener(); MsResultCollector resultCollector = new MsResultCollector();
resultCollector.setName(testId); 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.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ViewResultsFullVisualizer"));
resultCollector.setEnabled(true); resultCollector.setEnabled(true);
testPlan.add(testPlan.getArray()[0], resultCollector); 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.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.RequestResult; import io.metersphere.dto.RequestResult;
import io.metersphere.dto.ResultDTO;
import io.metersphere.jmeter.JMeterBase; import io.metersphere.jmeter.JMeterBase;
import io.metersphere.utils.JMeterVars; import io.metersphere.utils.JMeterVars;
import io.metersphere.utils.LoggerUtil; 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 io.metersphere.websocket.c.to.c.util.MsgDto;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; 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 { TestStateListener, Remoteable, NoThreadClone {
private static final String ERROR_LOGGING = "MsResultCollector.error_logging"; // $NON-NLS-1$ 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"; public static final String TEST_END = "MS_TEST_END";
private MsWebSocketClient client;
@Override @Override
public Object clone() { public Object clone() {
MsDebugListener clone = (MsDebugListener) super.clone(); MsResultCollector clone = (MsResultCollector) super.clone();
return clone; return clone;
} }
@ -100,28 +97,20 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
@Override @Override
public void testEnded(String host) { public void testEnded(String host) {
LoggerUtil.debug("TestEnded " + this.getName()); LoggerUtil.debug("TestEnded " + this.getName());
SampleResult result = new SampleResult(); MsgDto dto = new MsgDto();
result.setResponseCode(TEST_END); dto.setExecEnd(false);
ResultDTO dto = new ResultDTO(); dto.setContent(TEST_END);
dto.setReportId(this.getName()); dto.setReportId("send." + this.getName());
try { dto.setToReport(this.getName());
if (client != null) { LoggerUtil.debug("send. " + this.getName());
client.close(); WebSocketUtils.sendMessageSingle(dto);
} WebSocketUtils.onClose(this.getName());
} catch (Exception e) {
LogUtil.error(e);
}
} }
@Override @Override
public void testStarted(String host) { public void testStarted(String host) {
LogUtil.debug("TestStarted " + this.getName()); 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 @Override
@ -142,10 +131,9 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
dto.setReportId("send." + this.getName()); dto.setReportId("send." + this.getName());
dto.setToReport(this.getName()); dto.setToReport(this.getName());
LoggerUtil.debug("send. " + this.getName()); LoggerUtil.debug("send. " + this.getName());
if (client != null) { WebSocketUtils.sendMessageSingle(dto);
client.send(JSON.toJSONString(dto));
}
} catch (Exception ex) { } catch (Exception ex) {
LoggerUtil.error("消息推送失败:" + ex.getMessage());
} }
} }
@ -179,9 +167,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
dto.setReportId("send." + this.getName()); dto.setReportId("send." + this.getName());
dto.setToReport(this.getName()); dto.setToReport(this.getName());
LoggerUtil.debug("send. " + this.getName()); LoggerUtil.debug("send. " + this.getName());
if (client != null) { WebSocketUtils.sendMessageSingle(dto);
client.send(JSON.toJSONString(dto));
}
} }
} }
} }

View File

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

View File

@ -1,5 +1,6 @@
package io.metersphere.websocket.c.to.c; package io.metersphere.websocket.c.to.c;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.websocket.c.to.c.util.MsgDto; import io.metersphere.websocket.c.to.c.util.MsgDto;
import javax.websocket.RemoteEndpoint; import javax.websocket.RemoteEndpoint;
@ -12,9 +13,13 @@ public class WebSocketUtils {
// 单用户推送 // 单用户推送
public static void sendMessage(Session session, String message) { public static void sendMessage(Session session, String message) {
if (session == null) { return; } if (session == null) {
return;
}
RemoteEndpoint.Async async = session.getAsyncRemote(); RemoteEndpoint.Async async = session.getAsyncRemote();
if (async == null) { return; } if (async == null) {
return;
}
async.sendText(message); async.sendText(message);
} }
@ -30,4 +35,20 @@ public class WebSocketUtils {
sendMessage(session, message); 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) { if (e.data) {
this.runningEvaluation(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.getReport();
this.messageWebSocket.close(); this.messageWebSocket.close();
scenario.$emit('hide', this.scenarioId); scenario.$emit('hide', this.scenarioId);

View File

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