feat(任务判死): 心跳正常但 180 分钟无结果上报的二次判死线(13 模块)+ 同期待发改动
判死线(治 28131 型「主线程卡死、心跳线程照发」): - 判据改看 biz_task_scope_state.last_chunk_at(HTTP 心跳不刷新它);从未上报跳过不判 - 中央线覆盖 DELETE_BRAND/PRODUCT_RISK_RESOLVE/PRICE_TRACK/SHOP_MATCH/PATROL_DELETE/QUERY_ASIN/WITHDRAW - 自带线接入 COLLECT_DATA/SIMILAR_ASIN/APPEARANCE_PATENT/SHOP_DATA_CRAWL/PUBLISH/BRAND - 客户端心跳带处理位置 progressText,判死文案含最后位置;no-result-upload-timeout-minutes 默认 180(0 关闭) 同期带上另一工作流的待发改动:跟价换 IP 重试、品牌检测重试上限与 LLM 并发下调、 教程包后台管理页与 V126 迁移、admin-vue 教程记录页。
This commit is contained in:
+143
@@ -0,0 +1,143 @@
|
||||
package com.nanri.aiimage.modules.collectdata.service;
|
||||
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.dto.TaskScopeLastChunkDto;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* collect-data 二次判死线(心跳正常但连续 N 分钟无结果分片上报)。
|
||||
*
|
||||
* <p>既有心跳线候选条件是 updated_at 陈旧(心跳线),而 Python 心跳线程在主线程卡死时照发——
|
||||
* 任务永远命不中。本线候选取「心跳新鲜 + 创建超过 N 分钟」,判据用
|
||||
* biz_task_scope_state.last_chunk_at(仅分片上传时刷新);从未上报(无 scope 行)跳过。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CollectDataNoUploadStaleTest {
|
||||
|
||||
@Mock private FileTaskMapper fileTaskMapper;
|
||||
@Mock private FileResultMapper fileResultMapper;
|
||||
@Mock private TaskChunkMapper taskChunkMapper;
|
||||
@Mock private TaskScopeStateMapper taskScopeStateMapper;
|
||||
@Mock private TaskDistributedLockService taskDistributedLockService;
|
||||
@Mock private TaskFileJobService taskFileJobService;
|
||||
@Mock private TaskDistributedLockService.LockHandle lockHandle;
|
||||
|
||||
@Spy
|
||||
private com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
|
||||
|
||||
@InjectMocks
|
||||
private CollectDataService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ReflectionTestUtils.setField(service, "staleTimeoutMinutes", 30L);
|
||||
ReflectionTestUtils.setField(service, "noResultUploadTimeoutMinutes", 180L);
|
||||
ReflectionTestUtils.setField(service, "progressThrottleMillis", 0L);
|
||||
ReflectionTestUtils.setField(service, "progressDirtyWindowMillis", 0L);
|
||||
}
|
||||
|
||||
private FileTaskEntity runningTask(long id) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(id);
|
||||
task.setModuleType("COLLECT_DATA");
|
||||
task.setStatus("RUNNING");
|
||||
task.setCreatedAt(LocalDateTime.now().minusDays(1));
|
||||
task.setUpdatedAt(LocalDateTime.now().minusMinutes(1));
|
||||
return task;
|
||||
}
|
||||
|
||||
private void invokeNoUpload() {
|
||||
ReflectionTestUtils.invokeMethod(service, "finalizeNoUploadStaleTasks",
|
||||
LocalDateTime.now().minusMinutes(30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 上报仍新鲜时不判死() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(runningTask(1L)));
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(1L, LocalDateTime.now().minusMinutes(5))));
|
||||
|
||||
invokeNoUpload();
|
||||
|
||||
verify(taskDistributedLockService, never()).acquire(any(), anyLong(), anyLong());
|
||||
verify(fileTaskMapper, never()).updateById(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 从未上报时跳过() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(runningTask(2L)));
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any())).thenReturn(List.of());
|
||||
|
||||
invokeNoUpload();
|
||||
|
||||
verify(taskDistributedLockService, never()).acquire(any(), anyLong(), anyLong());
|
||||
verify(fileTaskMapper, never()).updateById(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 开关为0时不扫描() {
|
||||
ReflectionTestUtils.setField(service, "noResultUploadTimeoutMinutes", 0L);
|
||||
|
||||
invokeNoUpload();
|
||||
|
||||
verify(fileTaskMapper, never()).selectList(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 任务锁忙时跳过() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(runningTask(3L)));
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(3L, LocalDateTime.now().minusHours(4))));
|
||||
when(taskDistributedLockService.acquire(any(), anyLong(), anyLong())).thenReturn(null);
|
||||
|
||||
invokeNoUpload();
|
||||
|
||||
verify(fileTaskMapper, never()).updateById(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 心跳正常但上报超时触发判死() {
|
||||
FileTaskEntity task = runningTask(5L);
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(task));
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(5L, LocalDateTime.now().minusHours(4))));
|
||||
when(taskDistributedLockService.acquire(any(), anyLong(), anyLong())).thenReturn(lockHandle);
|
||||
when(fileTaskMapper.selectById(5L)).thenReturn(task);
|
||||
when(taskFileJobService.countUnfinishedAssembleJobs(5L, "COLLECT_DATA")).thenReturn(0L);
|
||||
when(taskChunkMapper.selectCount(any())).thenReturn(0L);
|
||||
when(fileResultMapper.selectOne(any())).thenReturn(null);
|
||||
|
||||
invokeNoUpload();
|
||||
|
||||
ArgumentCaptor<FileTaskEntity> taskCaptor = ArgumentCaptor.forClass(FileTaskEntity.class);
|
||||
verify(fileTaskMapper).updateById(taskCaptor.capture());
|
||||
assertEquals("FAILED", taskCaptor.getValue().getStatus());
|
||||
assertTrue(taskCaptor.getValue().getErrorMessage().contains("无结果回传"),
|
||||
taskCaptor.getValue().getErrorMessage());
|
||||
}
|
||||
}
|
||||
+184
-2
@@ -5,10 +5,15 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.config.DeleteBrandProgressProperties;
|
||||
import com.nanri.aiimage.modules.productrisk.service.ProductRiskTaskCacheService;
|
||||
import com.nanri.aiimage.modules.productrisk.service.ProductRiskTaskService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.dto.TaskScopeLastChunkDto;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -50,6 +55,10 @@ class DeleteBrandStaleTaskServiceTest {
|
||||
@Mock private DeleteBrandProgressProperties deleteBrandProgressProperties;
|
||||
@Mock private TaskDistributedLockService taskDistributedLockService;
|
||||
@Mock private TaskFileJobService taskFileJobService;
|
||||
@Mock private TaskScopeStateMapper taskScopeStateMapper;
|
||||
@Mock private ProductRiskTaskService productRiskTaskService;
|
||||
@Mock private ProductRiskTaskCacheService productRiskTaskCacheService;
|
||||
@Mock private TaskHeartbeatPositionService taskHeartbeatPositionService;
|
||||
|
||||
@BeforeAll
|
||||
static void initializeTableInfo() {
|
||||
@@ -212,11 +221,184 @@ class DeleteBrandStaleTaskServiceTest {
|
||||
assertTrue(errorMessage.contains("缺失分片"), errorMessage);
|
||||
}
|
||||
|
||||
// ---------- 二次判死线:心跳正常但连续 N 分钟无结果分片上报 ----------
|
||||
|
||||
@Test
|
||||
void noUploadStaleTaskIsFailedWithCasAndCacheEvicted() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(noUploadTask(901L, "PRODUCT_RISK_RESOLVE")));
|
||||
noUploadEnabled();
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(901L, LocalDateTime.now().minusHours(4))));
|
||||
when(taskFileJobService.countUnfinishedAssembleJobs(901L, "PRODUCT_RISK_RESOLVE")).thenReturn(0L);
|
||||
noUploadTaskLockAvailable();
|
||||
when(fileTaskMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
|
||||
|
||||
DeleteBrandStaleTaskService service = service();
|
||||
ReflectionTestUtils.invokeMethod(service, "failNoResultUploadTasks");
|
||||
|
||||
verify(productRiskTaskService).tryFinalizeTask(901L, true);
|
||||
verify(productRiskTaskCacheService).deleteTaskCache(901L);
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
ArgumentCaptor<LambdaUpdateWrapper> update = ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
|
||||
verify(fileTaskMapper).update(isNull(), update.capture());
|
||||
List<String> values = update.getValue().getParamNameValuePairs().values().stream()
|
||||
.map(String::valueOf)
|
||||
.toList();
|
||||
assertTrue(values.stream().anyMatch(v -> v.contains("无结果回传")), values.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noUploadFreshResultIsSkipped() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(noUploadTask(902L, "PRODUCT_RISK_RESOLVE")));
|
||||
noUploadEnabled();
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(902L, LocalDateTime.now().minusMinutes(5))));
|
||||
|
||||
DeleteBrandStaleTaskService service = service();
|
||||
ReflectionTestUtils.invokeMethod(service, "failNoResultUploadTasks");
|
||||
|
||||
verify(taskDistributedLockService, never()).acquire(any(), any(), anyLong());
|
||||
verify(fileTaskMapper, never()).update(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noUploadNeverUploadedIsSkipped() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(noUploadTask(903L, "PRODUCT_RISK_RESOLVE")));
|
||||
noUploadEnabled();
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any())).thenReturn(List.of());
|
||||
|
||||
DeleteBrandStaleTaskService service = service();
|
||||
ReflectionTestUtils.invokeMethod(service, "failNoResultUploadTasks");
|
||||
|
||||
verify(taskDistributedLockService, never()).acquire(any(), any(), anyLong());
|
||||
verify(fileTaskMapper, never()).update(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noUploadAlreadyFinalizedTaskKeepsCache() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(noUploadTask(904L, "PRODUCT_RISK_RESOLVE")));
|
||||
noUploadEnabled();
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(904L, LocalDateTime.now().minusHours(4))));
|
||||
when(taskFileJobService.countUnfinishedAssembleJobs(904L, "PRODUCT_RISK_RESOLVE")).thenReturn(0L);
|
||||
noUploadTaskLockAvailable();
|
||||
// CAS 未命中:finalize 已把任务终结(状态不再是 RUNNING)
|
||||
when(fileTaskMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(0);
|
||||
|
||||
DeleteBrandStaleTaskService service = service();
|
||||
ReflectionTestUtils.invokeMethod(service, "failNoResultUploadTasks");
|
||||
|
||||
verify(productRiskTaskCacheService, never()).deleteTaskCache(anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noUploadBusyTaskLockIsSkipped() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(noUploadTask(905L, "PRODUCT_RISK_RESOLVE")));
|
||||
noUploadEnabled();
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(905L, LocalDateTime.now().minusHours(4))));
|
||||
when(taskFileJobService.countUnfinishedAssembleJobs(905L, "PRODUCT_RISK_RESOLVE")).thenReturn(0L);
|
||||
when(taskDistributedLockService.acquire(any(), any(), anyLong())).thenReturn(null);
|
||||
|
||||
DeleteBrandStaleTaskService service = service();
|
||||
ReflectionTestUtils.invokeMethod(service, "failNoResultUploadTasks");
|
||||
|
||||
verify(productRiskTaskService, never()).tryFinalizeTask(anyLong(), anyBoolean());
|
||||
verify(fileTaskMapper, never()).update(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noUploadPendingAssembleJobsIsSkipped() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(noUploadTask(906L, "PRODUCT_RISK_RESOLVE")));
|
||||
noUploadEnabled();
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(906L, LocalDateTime.now().minusHours(4))));
|
||||
when(taskFileJobService.countUnfinishedAssembleJobs(906L, "PRODUCT_RISK_RESOLVE")).thenReturn(1L);
|
||||
|
||||
DeleteBrandStaleTaskService service = service();
|
||||
ReflectionTestUtils.invokeMethod(service, "failNoResultUploadTasks");
|
||||
|
||||
verify(productRiskTaskService, never()).tryFinalizeTask(anyLong(), anyBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noUploadDisabledByConfigSkipsScan() {
|
||||
when(deleteBrandProgressProperties.isNoResultUploadCheckEnabled()).thenReturn(false);
|
||||
|
||||
DeleteBrandStaleTaskService service = service();
|
||||
ReflectionTestUtils.invokeMethod(service, "failNoResultUploadTasks");
|
||||
|
||||
verify(fileTaskMapper, never()).selectList(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void noUploadDeleteBrandDispatchesToRunService() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(noUploadTask(907L, "DELETE_BRAND")));
|
||||
noUploadEnabled();
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(907L, LocalDateTime.now().minusHours(4))));
|
||||
when(taskFileJobService.countUnfinishedAssembleJobs(907L, "DELETE_BRAND")).thenReturn(0L);
|
||||
noUploadTaskLockAvailable();
|
||||
when(fileTaskMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
|
||||
|
||||
DeleteBrandStaleTaskService service = service();
|
||||
ReflectionTestUtils.invokeMethod(service, "failNoResultUploadTasks");
|
||||
|
||||
verify(deleteBrandRunService).tryFinalizeTask(907L, true);
|
||||
verify(deleteBrandTaskCacheService).delete(907L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void noUploadFailReasonIncludesLastPosition() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(noUploadTask(908L, "PRODUCT_RISK_RESOLVE")));
|
||||
noUploadEnabled();
|
||||
when(taskScopeStateMapper.selectLastChunkAtByTaskIds(any()))
|
||||
.thenReturn(List.of(new TaskScopeLastChunkDto(908L, LocalDateTime.now().minusHours(4))));
|
||||
when(taskFileJobService.countUnfinishedAssembleJobs(908L, "PRODUCT_RISK_RESOLVE")).thenReturn(0L);
|
||||
noUploadTaskLockAvailable();
|
||||
when(taskHeartbeatPositionService.describe(908L)).thenReturn("店铺 魏振峰(2/10),国家 德国(1/3)");
|
||||
when(fileTaskMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
|
||||
|
||||
DeleteBrandStaleTaskService service = service();
|
||||
ReflectionTestUtils.invokeMethod(service, "failNoResultUploadTasks");
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
ArgumentCaptor<LambdaUpdateWrapper> update = ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
|
||||
verify(fileTaskMapper).update(isNull(), update.capture());
|
||||
List<String> values = update.getValue().getParamNameValuePairs().values().stream()
|
||||
.map(String::valueOf)
|
||||
.toList();
|
||||
assertTrue(values.stream().anyMatch(v -> v.contains("最后处理位置:店铺 魏振峰(2/10)")), values.toString());
|
||||
}
|
||||
|
||||
private void noUploadEnabled() {
|
||||
when(deleteBrandProgressProperties.isNoResultUploadCheckEnabled()).thenReturn(true);
|
||||
when(deleteBrandProgressProperties.getNoResultUploadTimeoutMinutes()).thenReturn(180L);
|
||||
}
|
||||
|
||||
private void noUploadTaskLockAvailable() {
|
||||
when(taskDistributedLockService.acquire(any(), any(), anyLong()))
|
||||
.thenReturn(mock(TaskDistributedLockService.LockHandle.class));
|
||||
}
|
||||
|
||||
private static FileTaskEntity noUploadTask(Long id, String moduleType) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(id);
|
||||
task.setModuleType(moduleType);
|
||||
task.setStatus("RUNNING");
|
||||
task.setCreatedAt(LocalDateTime.now().minusDays(1));
|
||||
task.setUpdatedAt(LocalDateTime.now().minusMinutes(1));
|
||||
return task;
|
||||
}
|
||||
|
||||
private DeleteBrandStaleTaskService service() {
|
||||
return new DeleteBrandStaleTaskService(
|
||||
fileTaskMapper, deleteBrandTaskCacheService, deleteBrandTaskStorageService, deleteBrandRunService,
|
||||
null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
|
||||
deleteBrandProgressProperties, null, taskDistributedLockService, taskFileJobService, null);
|
||||
productRiskTaskService, productRiskTaskCacheService,
|
||||
null, null, null, null, null, null, null, null, null, null,
|
||||
null, null, null,
|
||||
deleteBrandProgressProperties, null, taskDistributedLockService, taskFileJobService, null,
|
||||
taskScopeStateMapper, taskHeartbeatPositionService);
|
||||
}
|
||||
|
||||
private void lockAvailable() {
|
||||
|
||||
+2
-1
@@ -81,7 +81,8 @@ class TaskModuleCoverageTest {
|
||||
mock(AppearancePatentTaskService.class),
|
||||
mock(SimilarAsinTaskService.class),
|
||||
null, null, null, null,
|
||||
mock(ShopDataCrawlTaskService.class));
|
||||
mock(ShopDataCrawlTaskService.class),
|
||||
null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package com.nanri.aiimage.modules.task.service;
|
||||
|
||||
import com.nanri.aiimage.modules.task.model.dto.TaskHeartbeatRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 心跳「当前处理位置」存储:写读往返、空位置不写、Redis 异常只降级不抛出。
|
||||
* 位置仅用于判死文案与排查,任何异常都不得影响心跳本身。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class TaskHeartbeatPositionServiceTest {
|
||||
|
||||
@Mock private StringRedisTemplate stringRedisTemplate;
|
||||
@Mock private HashOperations<String, Object, Object> hashOperations;
|
||||
|
||||
private TaskHeartbeatPositionService service() {
|
||||
return new TaskHeartbeatPositionService(stringRedisTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordWritesHashWithProgressText() {
|
||||
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
|
||||
TaskHeartbeatRequest request = new TaskHeartbeatRequest();
|
||||
request.setPhase("crawling");
|
||||
request.setCurrent(2);
|
||||
request.setTotal(10);
|
||||
request.setProgressText("店铺 魏振峰(2/10)");
|
||||
|
||||
service().record(123L, request);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, String>> values = ArgumentCaptor.forClass(Map.class);
|
||||
verify(hashOperations).putAll(eq("task:heartbeat:position:123"), values.capture());
|
||||
assertEquals("店铺 魏振峰(2/10)", values.getValue().get("progressText"));
|
||||
assertEquals("crawling", values.getValue().get("phase"));
|
||||
assertEquals("2", values.getValue().get("current"));
|
||||
assertEquals("10", values.getValue().get("total"));
|
||||
verify(stringRedisTemplate).expire(eq("task:heartbeat:position:123"), any(Duration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordSkipsWhenNoPosition() {
|
||||
TaskHeartbeatRequest request = new TaskHeartbeatRequest();
|
||||
|
||||
service().record(123L, request);
|
||||
|
||||
verify(stringRedisTemplate, never()).opsForHash();
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordSwallowsRedisError() {
|
||||
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
|
||||
doThrow(new RuntimeException("redis down")).when(hashOperations).putAll(any(), any());
|
||||
TaskHeartbeatRequest request = new TaskHeartbeatRequest();
|
||||
request.setProgressText("店铺 A(1/2)");
|
||||
|
||||
service().record(123L, request);
|
||||
|
||||
verify(stringRedisTemplate, never()).expire(any(), any(Duration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describePrefersProgressText() {
|
||||
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
|
||||
when(hashOperations.entries("task:heartbeat:position:123"))
|
||||
.thenReturn(Map.of("progressText", "店铺 魏振峰(2/10)", "current", "2", "total", "10"));
|
||||
|
||||
assertEquals("店铺 魏振峰(2/10)", service().describe(123L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeBuildsFromPhaseAndCountersWhenNoText() {
|
||||
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
|
||||
when(hashOperations.entries("task:heartbeat:position:123"))
|
||||
.thenReturn(Map.of("phase", "crawling", "current", "2", "total", "10"));
|
||||
|
||||
assertEquals("阶段=crawling,2/10", service().describe(123L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeReturnsNullWhenMissing() {
|
||||
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
|
||||
when(hashOperations.entries("task:heartbeat:position:123")).thenReturn(Map.of());
|
||||
|
||||
assertNull(service().describe(123L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeReturnsNullOnRedisError() {
|
||||
when(stringRedisTemplate.opsForHash()).thenReturn(hashOperations);
|
||||
when(hashOperations.entries("task:heartbeat:position:123"))
|
||||
.thenThrow(new RuntimeException("redis down"));
|
||||
|
||||
assertNull(service().describe(123L));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user