feat (接口自动化): 完成用例和场景调试部分功能 #1001749

This commit is contained in:
fit2-zhao 2021-07-30 15:10:01 +08:00 committed by fit2-zhao
parent e6bffa7436
commit 87d6833400
9 changed files with 1388 additions and 0 deletions

View File

@ -429,6 +429,11 @@
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>

View File

@ -17,10 +17,13 @@
package io.metersphere.api.jmeter;
import com.alibaba.fastjson.JSON;
import io.metersphere.api.dto.RunningParamKeys;
import io.metersphere.api.service.MsResultService;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.websocket.c.to.c.MsWebSocketClient;
import io.metersphere.websocket.c.to.c.util.MsgDto;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.engine.util.NoThreadClone;
@ -49,6 +52,8 @@ public class MsResultCollector extends AbstractListenerElement implements Sample
private MsResultService msResultService;
private MsWebSocketClient client;
@Override
public Object clone() {
MsResultCollector clone = (MsResultCollector) super.clone();
@ -95,6 +100,11 @@ public class MsResultCollector extends AbstractListenerElement implements Sample
SampleResult result = new SampleResult();
result.setResponseCode(TEST_END);
msResultService.setCache(this.getName(), result);
try {
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
@ -104,6 +114,12 @@ public class MsResultCollector extends AbstractListenerElement implements Sample
if (msResultService == null) {
LogUtil.error("testResultService is required");
}
try {
client = new MsWebSocketClient("ws://127.0.0.1:8081/ws/" + "send." + this.getName());
client.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
@ -118,6 +134,16 @@ public class MsResultCollector extends AbstractListenerElement implements Sample
@Override
public void sampleStarted(SampleEvent e) {
System.out.println("start ====");
try {
MsgDto dto = new MsgDto();
dto.setContent(e.getThreadGroup());
dto.setReportId("send." + this.getName());
dto.setToReport(this.getName());
client.send(JSON.toJSONString(dto));
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override

View File

@ -57,6 +57,8 @@ public class ShiroUtils {
filterChainDefinitionMap.put("/v1/health/**", "anon");
//mock接口
filterChainDefinitionMap.put("/mock/**", "anon");
filterChainDefinitionMap.put("/ws/**", "anon");
}
public static void ignoreCsrfFilter(Map<String, String> filterChainDefinitionMap) {

View File

@ -0,0 +1,59 @@
package io.metersphere.websocket.c.to.c;
import com.alibaba.fastjson.JSON;
import io.metersphere.websocket.c.to.c.util.MsgDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
@Slf4j
@Component // 注册到spring
@ServerEndpoint("/ws/{reportId}") // 创建websocket服务
public class IndexWebSocket {
/**
* 连接成功响应
*/
@OnOpen
public void openSession(@PathParam("reportId") String reportId, Session session) {
WebSocketUtils.ONLINE_USER_SESSIONS.put(reportId, session);
log.info("客户端: [" + reportId + "] : 连接成功!");
WebSocketUtils.sendMessageAll("客户端: [" + reportId + "] : 连接成功!");
}
/**
* 收到消息响应
*/
@OnMessage
public void onMessage(@PathParam("reportId") String reportId, String message) {
log.info("服务器收到:[" + reportId + "] : " + message);
MsgDto dto = JSON.parseObject(message, MsgDto.class);
dto.setContent(dto.getContent());
WebSocketUtils.sendMessageSingle(dto);
}
/**
* 连接关闭响应
*/
@OnClose
public void onClose(@PathParam("reportId") String reportId, Session session) throws IOException {
//当前的Session 移除
WebSocketUtils.ONLINE_USER_SESSIONS.remove(reportId);
log.info("[" + reportId + "] : 断开连接!");
//并且通知其他人当前用户已经断开连接了
WebSocketUtils.sendMessageAll("[" + reportId + "] : 断开连接!");
session.close();
}
/**
* 连接异常响应
*/
@OnError
public void onError(Session session, Throwable throwable) throws IOException {
session.close();
}
}

View File

@ -0,0 +1,54 @@
package io.metersphere.websocket.c.to.c;
import com.alibaba.fastjson.JSON;
import io.metersphere.websocket.c.to.c.util.MsgDto;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
public class MsWebSocketClient extends WebSocketClient{
public MsWebSocketClient(String url) throws URISyntaxException {
super(new URI(url));
}
@Override
public void onOpen(ServerHandshake shake) {
System.out.println("握手...");
for(Iterator<String> it=shake.iterateHttpFields();it.hasNext();) {
String key = it.next();
System.out.println(key+":"+shake.getFieldValue(key));
}
}
@Override
public void onMessage(String paramString) {
System.out.println("接收到消息:"+paramString);
}
@Override
public void onClose(int paramInt, String paramString, boolean paramBoolean) {
System.out.println("关闭...");
}
@Override
public void onError(Exception e) {
System.out.println("异常"+e);
}
public static void main(String[] args) {
try {
MsWebSocketClient client = new MsWebSocketClient("ws://127.0.0.1:8081/ws/22222");
client.connect();
System.out.println("建立websocket连接");
MsgDto dto = new MsgDto();
dto.setContent("099991023123123");
dto.setReportId("123123123");
dto.setToReport("3933abd9");
client.send(JSON.toJSONString(dto));
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,33 @@
package io.metersphere.websocket.c.to.c;
import io.metersphere.websocket.c.to.c.util.MsgDto;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class WebSocketUtils {
public static final Map<String, Session> ONLINE_USER_SESSIONS = new ConcurrentHashMap<>();
// 单用户推送
public static void sendMessage(Session session, String message) {
if (session == null) { return; }
RemoteEndpoint.Async async = session.getAsyncRemote();
if (async == null) { return; }
async.sendText(message);
}
// 单用户推送
public static void sendMessageSingle(MsgDto dto) {
sendMessage(ONLINE_USER_SESSIONS.get(dto.getReportId()), dto.getContent());
sendMessage(ONLINE_USER_SESSIONS.get(dto.getToReport()), dto.getContent());
}
// 全用户推送
public static void sendMessageAll(String message) {
ONLINE_USER_SESSIONS.forEach((sessionId, session) -> {
sendMessage(session, message);
});
}
}

View File

@ -0,0 +1,18 @@
package io.metersphere.websocket.c.to.c.util;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*/
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class MsgDto {
private String reportId;
private String toReport;
private String content;
}

View File

@ -0,0 +1,19 @@
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;
}

File diff suppressed because it is too large Load Diff