From 15391dd3942be671f642cbdd875b962b0b0d3fc9 Mon Sep 17 00:00:00 2001 From: neilz Date: Mon, 15 Mar 2021 07:53:29 +0800 Subject: [PATCH] =?UTF-8?q?job=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A0=A1=E9=AA=8C=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/job/vo/job/InfJobBaseVO.java | 1 - .../infra/service/job/InfJobServiceTest.java | 60 +++++++++++++------ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/job/vo/job/InfJobBaseVO.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/job/vo/job/InfJobBaseVO.java index 246f2a7a9..2df3ad823 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/job/vo/job/InfJobBaseVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/job/vo/job/InfJobBaseVO.java @@ -34,5 +34,4 @@ public class InfJobBaseVO { @ApiModelProperty(value = "监控超时时间", example = "1000") private Integer monitorTimeout; - } diff --git a/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfJobServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfJobServiceTest.java index 10a2dba7a..22f4cc14c 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfJobServiceTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfJobServiceTest.java @@ -14,6 +14,9 @@ import static cn.iocoder.dashboard.util.RandomUtils.randomString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import java.util.ArrayList; import java.util.List; @@ -84,6 +87,9 @@ public class InfJobServiceTest extends BaseDbUnitTest { InfJobDO job = jobMapper.selectById(jobId); assertPojoEquals(reqVO, job); assertEquals(InfJobStatusEnum.NORMAL.getStatus(), job.getStatus()); + // 校验调用 + verify(schedulerManager, times(1)).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()), eq(job.getCronExpression()), + eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval())); } @Test @@ -120,8 +126,6 @@ public class InfJobServiceTest extends BaseDbUnitTest { job.setStatus(InfJobStatusEnum.NORMAL.getStatus()); fillJobMonitorTimeoutEmpty(job); jobMapper.insert(job); - schedulerManager.addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(), - createReqVO.getRetryCount(), createReqVO.getRetryInterval()); // 准备参数 InfJobUpdateReqVO updateReqVO = randomPojo(InfJobUpdateReqVO.class, o -> { o.setId(job.getId()); @@ -133,6 +137,9 @@ public class InfJobServiceTest extends BaseDbUnitTest { // 校验记录的属性是否正确 InfJobDO updateJob = jobMapper.selectById(updateReqVO.getId()); assertPojoEquals(updateReqVO, updateJob); + // 校验调用 + verify(schedulerManager, times(1)).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()), eq(updateReqVO.getCronExpression()), + eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval())); } @Test @@ -154,36 +161,51 @@ public class InfJobServiceTest extends BaseDbUnitTest { } @Test - public void testUpdateJobStatus_success() throws SchedulerException { + public void testUpdateJobStatus_NormalToStop_success() throws SchedulerException { // mock 数据 InfJobCreateReqVO createReqVO = randomPojo(InfJobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *")); InfJobDO job = InfJobConvert.INSTANCE.convert(createReqVO); job.setStatus(InfJobStatusEnum.NORMAL.getStatus()); fillJobMonitorTimeoutEmpty(job); jobMapper.insert(job); - schedulerManager.addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(), - createReqVO.getRetryCount(), createReqVO.getRetryInterval()); // 调用 jobService.updateJobStatus(job.getId(), InfJobStatusEnum.STOP.getStatus()); // 校验记录的属性是否正确 InfJobDO updateJob = jobMapper.selectById(job.getId()); assertEquals(InfJobStatusEnum.STOP.getStatus(), updateJob.getStatus()); + // 校验调用 + verify(schedulerManager, times(1)).pauseJob(eq(job.getHandlerName())); + } + + @Test + public void testUpdateJobStatus_StopToNormal_success() throws SchedulerException { + // mock 数据 + InfJobCreateReqVO createReqVO = randomPojo(InfJobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *")); + InfJobDO job = InfJobConvert.INSTANCE.convert(createReqVO); + job.setStatus(InfJobStatusEnum.STOP.getStatus()); + fillJobMonitorTimeoutEmpty(job); + jobMapper.insert(job); + // 调用 + jobService.updateJobStatus(job.getId(), InfJobStatusEnum.NORMAL.getStatus()); + // 校验记录的属性是否正确 + InfJobDO updateJob = jobMapper.selectById(job.getId()); + assertEquals(InfJobStatusEnum.NORMAL.getStatus(), updateJob.getStatus()); + // 校验调用 + verify(schedulerManager, times(1)).resumeJob(eq(job.getHandlerName())); } - /** - * 页面"执行一次"按钮功能集成测试发现问题: - * inf_job 表初始化任务 sysUserSessionTimeoutJob 点击报错,是因为 Job 并没有添加到 Quartz 中; - * 没有走 createJob 中 scheduler.scheduleJob() 这一步,报错任务找不到。 - * // FINISHED Quartz 相关表新增初始化任务 sysUserSessionTimeoutJob sql - */ @Test public void testTriggerJob_success() throws SchedulerException { - /** - * TODO 不知道是否要将 Quartz 相关 SQL 引入来做单元测试 - * 1、schedulerManager.addJob sysUserSessionTimeoutJob - * 2、schedulerManager.triggerJob - * 3、check inf_job_log - */ + // mock 数据 + InfJobCreateReqVO createReqVO = randomPojo(InfJobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *")); + InfJobDO job = InfJobConvert.INSTANCE.convert(createReqVO); + job.setStatus(InfJobStatusEnum.NORMAL.getStatus()); + fillJobMonitorTimeoutEmpty(job); + jobMapper.insert(job); + // 调用 + jobService.triggerJob(job.getId()); + // 校验调用 + verify(schedulerManager, times(1)).triggerJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam())); } @Test @@ -194,12 +216,12 @@ public class InfJobServiceTest extends BaseDbUnitTest { job.setStatus(InfJobStatusEnum.NORMAL.getStatus()); fillJobMonitorTimeoutEmpty(job); jobMapper.insert(job); - schedulerManager.addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(), - createReqVO.getRetryCount(), createReqVO.getRetryInterval()); // 调用 UPDATE inf_job SET deleted=1 WHERE id=? AND deleted=0 jobService.deleteJob(job.getId()); // 校验数据不存在了 WHERE id=? AND deleted=0 查询为空正常 assertNull(jobMapper.selectById(job.getId())); + // 校验调用 + verify(schedulerManager, times(1)).deleteJob(eq(job.getHandlerName())); } @Test