fix(测试跟踪): 修复批量执行测试计划会偶发执行人错误的问题

--bug=1026019 --user=宋天阳 【测试跟踪】测试计划批量执行-报告执行人非当前执行人-任务中心不显示执行记录
https://www.tapd.cn/55049933/s/1369590
This commit is contained in:
song-tianyang 2023-05-08 11:33:57 +08:00 committed by 刘瑞斌
parent c266e9522a
commit 9139471fc8
8 changed files with 140 additions and 40 deletions

View File

@ -1,8 +1,7 @@
package io.metersphere.base.domain; package io.metersphere.base.domain;
import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
import lombok.Data;
@Data @Data
public class TestPlanExecutionQueue implements Serializable { public class TestPlanExecutionQueue implements Serializable {
@ -20,5 +19,7 @@ public class TestPlanExecutionQueue implements Serializable {
private Integer num; private Integer num;
private String executeUser;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
} }

View File

@ -573,6 +573,76 @@ public class TestPlanExecutionQueueExample {
addCriterion("num not between", value1, value2, "num"); addCriterion("num not between", value1, value2, "num");
return (Criteria) this; return (Criteria) this;
} }
public Criteria andExecuteUserIsNull() {
addCriterion("execute_user is null");
return (Criteria) this;
}
public Criteria andExecuteUserIsNotNull() {
addCriterion("execute_user is not null");
return (Criteria) this;
}
public Criteria andExecuteUserEqualTo(String value) {
addCriterion("execute_user =", value, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserNotEqualTo(String value) {
addCriterion("execute_user <>", value, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserGreaterThan(String value) {
addCriterion("execute_user >", value, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserGreaterThanOrEqualTo(String value) {
addCriterion("execute_user >=", value, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserLessThan(String value) {
addCriterion("execute_user <", value, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserLessThanOrEqualTo(String value) {
addCriterion("execute_user <=", value, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserLike(String value) {
addCriterion("execute_user like", value, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserNotLike(String value) {
addCriterion("execute_user not like", value, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserIn(List<String> values) {
addCriterion("execute_user in", values, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserNotIn(List<String> values) {
addCriterion("execute_user not in", values, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserBetween(String value1, String value2) {
addCriterion("execute_user between", value1, value2, "executeUser");
return (Criteria) this;
}
public Criteria andExecuteUserNotBetween(String value1, String value2) {
addCriterion("execute_user not between", value1, value2, "executeUser");
return (Criteria) this;
}
} }
public static class Criteria extends GeneratedCriteria { public static class Criteria extends GeneratedCriteria {

View File

@ -28,5 +28,4 @@ public interface TestPlanExecutionQueueMapper {
int updateByPrimaryKeySelective(TestPlanExecutionQueue record); int updateByPrimaryKeySelective(TestPlanExecutionQueue record);
int updateByPrimaryKey(TestPlanExecutionQueue record); int updateByPrimaryKey(TestPlanExecutionQueue record);
} }

View File

@ -9,6 +9,7 @@
<result column="test_plan_id" jdbcType="VARCHAR" property="testPlanId" /> <result column="test_plan_id" jdbcType="VARCHAR" property="testPlanId" />
<result column="resource_id" jdbcType="VARCHAR" property="resourceId" /> <result column="resource_id" jdbcType="VARCHAR" property="resourceId" />
<result column="num" jdbcType="INTEGER" property="num" /> <result column="num" jdbcType="INTEGER" property="num" />
<result column="execute_user" jdbcType="VARCHAR" property="executeUser" />
</resultMap> </resultMap>
<sql id="Example_Where_Clause"> <sql id="Example_Where_Clause">
<where> <where>
@ -69,7 +70,7 @@
</where> </where>
</sql> </sql>
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id, report_id, run_mode, create_time, test_plan_id, resource_id, num id, report_id, run_mode, create_time, test_plan_id, resource_id, num, execute_user
</sql> </sql>
<select id="selectByExample" parameterType="io.metersphere.base.domain.TestPlanExecutionQueueExample" resultMap="BaseResultMap"> <select id="selectByExample" parameterType="io.metersphere.base.domain.TestPlanExecutionQueueExample" resultMap="BaseResultMap">
select select
@ -104,10 +105,10 @@
<insert id="insert" parameterType="io.metersphere.base.domain.TestPlanExecutionQueue"> <insert id="insert" parameterType="io.metersphere.base.domain.TestPlanExecutionQueue">
insert into test_plan_execution_queue (id, report_id, run_mode, insert into test_plan_execution_queue (id, report_id, run_mode,
create_time, test_plan_id, resource_id, create_time, test_plan_id, resource_id,
num) num, execute_user)
values (#{id,jdbcType=VARCHAR}, #{reportId,jdbcType=VARCHAR}, #{runMode,jdbcType=VARCHAR}, values (#{id,jdbcType=VARCHAR}, #{reportId,jdbcType=VARCHAR}, #{runMode,jdbcType=VARCHAR},
#{createTime,jdbcType=BIGINT}, #{testPlanId,jdbcType=VARCHAR}, #{resourceId,jdbcType=VARCHAR}, #{createTime,jdbcType=BIGINT}, #{testPlanId,jdbcType=VARCHAR}, #{resourceId,jdbcType=VARCHAR},
#{num,jdbcType=INTEGER}) #{num,jdbcType=INTEGER}, #{executeUser,jdbcType=VARCHAR})
</insert> </insert>
<insert id="insertSelective" parameterType="io.metersphere.base.domain.TestPlanExecutionQueue"> <insert id="insertSelective" parameterType="io.metersphere.base.domain.TestPlanExecutionQueue">
insert into test_plan_execution_queue insert into test_plan_execution_queue
@ -133,6 +134,9 @@
<if test="num != null"> <if test="num != null">
num, num,
</if> </if>
<if test="executeUser != null">
execute_user,
</if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null"> <if test="id != null">
@ -156,6 +160,9 @@
<if test="num != null"> <if test="num != null">
#{num,jdbcType=INTEGER}, #{num,jdbcType=INTEGER},
</if> </if>
<if test="executeUser != null">
#{executeUser,jdbcType=VARCHAR},
</if>
</trim> </trim>
</insert> </insert>
<select id="countByExample" parameterType="io.metersphere.base.domain.TestPlanExecutionQueueExample" resultType="java.lang.Long"> <select id="countByExample" parameterType="io.metersphere.base.domain.TestPlanExecutionQueueExample" resultType="java.lang.Long">
@ -188,6 +195,9 @@
<if test="record.num != null"> <if test="record.num != null">
num = #{record.num,jdbcType=INTEGER}, num = #{record.num,jdbcType=INTEGER},
</if> </if>
<if test="record.executeUser != null">
execute_user = #{record.executeUser,jdbcType=VARCHAR},
</if>
</set> </set>
<if test="_parameter != null"> <if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" /> <include refid="Update_By_Example_Where_Clause" />
@ -201,7 +211,8 @@
create_time = #{record.createTime,jdbcType=BIGINT}, create_time = #{record.createTime,jdbcType=BIGINT},
test_plan_id = #{record.testPlanId,jdbcType=VARCHAR}, test_plan_id = #{record.testPlanId,jdbcType=VARCHAR},
resource_id = #{record.resourceId,jdbcType=VARCHAR}, resource_id = #{record.resourceId,jdbcType=VARCHAR},
num = #{record.num,jdbcType=INTEGER} num = #{record.num,jdbcType=INTEGER},
execute_user = #{record.executeUser,jdbcType=VARCHAR}
<if test="_parameter != null"> <if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" /> <include refid="Update_By_Example_Where_Clause" />
</if> </if>
@ -227,6 +238,9 @@
<if test="num != null"> <if test="num != null">
num = #{num,jdbcType=INTEGER}, num = #{num,jdbcType=INTEGER},
</if> </if>
<if test="executeUser != null">
execute_user = #{executeUser,jdbcType=VARCHAR},
</if>
</set> </set>
where id = #{id,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR}
</update> </update>
@ -237,7 +251,8 @@
create_time = #{createTime,jdbcType=BIGINT}, create_time = #{createTime,jdbcType=BIGINT},
test_plan_id = #{testPlanId,jdbcType=VARCHAR}, test_plan_id = #{testPlanId,jdbcType=VARCHAR},
resource_id = #{resourceId,jdbcType=VARCHAR}, resource_id = #{resourceId,jdbcType=VARCHAR},
num = #{num,jdbcType=INTEGER} num = #{num,jdbcType=INTEGER},
execute_user = #{executeUser,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR}
</update> </update>
</mapper> </mapper>

View File

@ -50,15 +50,12 @@ public class TestPlanMessageService {
@Async @Async
public void checkTestPlanStatusAndSendMessage(TestPlanReport report, TestPlanReportContentWithBLOBs testPlanReportContent, boolean sendMessage) { public void checkTestPlanStatusAndSendMessage(TestPlanReport report, TestPlanReportContentWithBLOBs testPlanReportContent, boolean sendMessage) {
if (report == null) { if (report != null && testPlanReportContent != null) {
return;
}
if (testPlanReportContent != null) {
report = testPlanReportService.checkTestPlanReportHasErrorCase(report, testPlanReportContent);
}
if (!report.getIsApiCaseExecuting() && !report.getIsPerformanceExecuting() && !report.getIsScenarioExecuting() && !report.getIsUiScenarioExecuting()) {
// 异步发送通知需要指定调用其他服务的user // 异步发送通知需要指定调用其他服务的user
HttpHeaderUtils.runAsUser(report.getCreator()); HttpHeaderUtils.runAsUser(report.getCreator());
try {
report = testPlanReportService.checkTestPlanReportHasErrorCase(report, testPlanReportContent);
if (!report.getIsApiCaseExecuting() && !report.getIsPerformanceExecuting() && !report.getIsScenarioExecuting() && !report.getIsUiScenarioExecuting()) {
//更新TestPlan状态为完成 //更新TestPlan状态为完成
TestPlanWithBLOBs testPlan = testPlanMapper.selectByPrimaryKey(report.getTestPlanId()); TestPlanWithBLOBs testPlan = testPlanMapper.selectByPrimaryKey(report.getTestPlanId());
if (testPlan != null if (testPlan != null
@ -67,7 +64,6 @@ public class TestPlanMessageService {
testPlan.setStatus(calcTestPlanStatusWithPassRate(testPlan)); testPlan.setStatus(calcTestPlanStatusWithPassRate(testPlan));
testPlanService.editTestPlan(testPlan); testPlanService.editTestPlan(testPlan);
} }
try {
if (sendMessage && testPlan != null && StringUtils.equalsAny(report.getTriggerMode(), if (sendMessage && testPlan != null && StringUtils.equalsAny(report.getTriggerMode(),
ReportTriggerMode.MANUAL.name(), ReportTriggerMode.MANUAL.name(),
ReportTriggerMode.API.name(), ReportTriggerMode.API.name(),
@ -76,11 +72,13 @@ public class TestPlanMessageService {
//发送通知 //发送通知
this.sendMessage(testPlan, report, testPlan.getProjectId()); this.sendMessage(testPlan, report, testPlan.getProjectId());
} }
}
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e); LogUtil.error("检查测试计划状态出错", e);
} finally { } finally {
HttpHeaderUtils.clearUser(); HttpHeaderUtils.clearUser();
} }
} }
} }

View File

@ -683,6 +683,10 @@ public class TestPlanReportService {
Map jsonObject = JSON.parseMap(testPlan.getRunModeConfig()); Map jsonObject = JSON.parseMap(testPlan.getRunModeConfig());
TestPlanRequestUtil.changeStringToBoolean(jsonObject); TestPlanRequestUtil.changeStringToBoolean(jsonObject);
TestPlanRunRequest runRequest = JSON.parseObject(JSON.toJSONString(jsonObject), TestPlanRunRequest.class); TestPlanRunRequest runRequest = JSON.parseObject(JSON.toJSONString(jsonObject), TestPlanRunRequest.class);
if (StringUtils.isNotBlank(testPlanExecutionQueue.getExecuteUser())) {
runRequest.setUserId(testPlanExecutionQueue.getExecuteUser());
}
runRequest.setTestPlanId(testPlanExecutionQueue.getTestPlanId());
runRequest.setReportId(testPlanExecutionQueue.getReportId()); runRequest.setReportId(testPlanExecutionQueue.getReportId());
runRequest.setTestPlanId(testPlan.getId()); runRequest.setTestPlanId(testPlan.getId());
try { try {

View File

@ -474,8 +474,7 @@ public class TestPlanService {
} }
}); });
testPlan.setTotal(testPlan.getTotal() + execResults.size()); testPlan.setTotal(testPlan.getTotal() + execResults.size());
} catch (MSException e) { } catch (MSException ignore) {
LogUtil.error(e);
} }
} }
@ -527,6 +526,7 @@ public class TestPlanService {
/** /**
* 异步将测试计划的状态置为已结束 * 异步将测试计划的状态置为已结束
*
* @param changeToFinishedIds * @param changeToFinishedIds
*/ */
@Async @Async
@ -2159,6 +2159,7 @@ public class TestPlanService {
executionQueue.setRunMode(request.getMode()); executionQueue.setRunMode(request.getMode());
executionQueue.setResourceId(resourceId); executionQueue.setResourceId(resourceId);
executionQueue.setNum(nextNum[0]); executionQueue.setNum(nextNum[0]);
executionQueue.setExecuteUser(request.getUserId());
nextNum[0]++; nextNum[0]++;
planExecutionQueues.add(executionQueue); planExecutionQueues.add(executionQueue);
}); });
@ -2174,6 +2175,10 @@ public class TestPlanService {
Map jsonObject = JSON.parseMap(testPlan.getRunModeConfig()); Map jsonObject = JSON.parseMap(testPlan.getRunModeConfig());
TestPlanRequestUtil.changeStringToBoolean(jsonObject); TestPlanRequestUtil.changeStringToBoolean(jsonObject);
TestPlanRunRequest runRequest = JSON.parseObject(JSON.toJSONString(jsonObject), TestPlanRunRequest.class); TestPlanRunRequest runRequest = JSON.parseObject(JSON.toJSONString(jsonObject), TestPlanRunRequest.class);
if (StringUtils.isNotBlank(planExecutionQueue.getExecuteUser())) {
runRequest.setUserId(planExecutionQueue.getExecuteUser());
}
runRequest.setTestPlanId(planExecutionQueue.getTestPlanId());
runRequest.setReportId(planExecutionQueue.getReportId()); runRequest.setReportId(planExecutionQueue.getReportId());
runPlan(runRequest); runPlan(runRequest);
} else { } else {

View File

@ -0,0 +1,8 @@
SET SESSION innodb_lock_wait_timeout = 7200;
-- 队列增加执行人
ALTER TABLE test_plan_execution_queue
ADD COLUMN `execute_user` VARCHAR(50);
SET SESSION innodb_lock_wait_timeout = DEFAULT;