fix(货源): 品牌检测部分结果文案只写可观测事实,不再指认猜测的故障原因
通知里那句「(品牌检测服务超时或被限流)」是本层代码推断出来的,真实原因 只有 BrandCheckClient 的 brand-check warn 日志知道(本轮实际是代理取不到 IP)。 把猜测写进用户通知,会把排查方向从「代理/账号余额」带到「品牌服务」上去。 新增 CollectDataPartialResultNoticeTest 3 例:驱动真 processResultFileJob, 断言文案含可核对的语义、不含「超时/限流」,且任务已被判失败时不推这条。
This commit is contained in:
+149
@@ -0,0 +1,149 @@
|
||||
package com.nanri.aiimage.modules.collectdata.service;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.modules.notification.service.NotificationDispatchService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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 java.io.File;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 「任务成功但有一部分行没跑出来」通知文案(2026-10-02)。
|
||||
*
|
||||
* <p>背景:后台通知里这条写死了「品牌检测服务超时或被限流」,而 10-02 取证出的真实根因是品牌链
|
||||
* 上游的采集代理取不到 IP(余额不足)与 503 —— 代码在这个位置只观测到「品牌检测没返回结果」,
|
||||
* 观测不到原因。写死一个猜测的原因会把排查方向从代理/上游带到自身服务上。
|
||||
*
|
||||
* <p>锁定契约:文案只陈述可观测事实(缺失行数、不计入结果条数、未判无效、可重跑),
|
||||
* 不指认代码没有依据的具体故障原因;真实原因留在 BrandCheckClient 的 warn 日志里
|
||||
* (brand-check 的 lastErr 字段),由运维按日志排查。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CollectDataPartialResultNoticeTest {
|
||||
|
||||
@Mock private FileTaskMapper fileTaskMapper;
|
||||
@Mock private FileResultMapper fileResultMapper;
|
||||
@Mock private CollectDataExcelAssemblyService excelAssemblyService;
|
||||
@Mock private OssStorageService ossStorageService;
|
||||
@Mock private NotificationDispatchService notificationDispatchService;
|
||||
|
||||
@Spy private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@InjectMocks private CollectDataService service;
|
||||
|
||||
private static final long TASK_ID = 29991L;
|
||||
|
||||
/** LambdaUpdateWrapper 需要实体的列缓存,纯单测里没有 Spring 容器要手工初始化。 */
|
||||
@BeforeAll
|
||||
static void initTableInfo() {
|
||||
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
||||
TableInfoHelper.initTableInfo(assistant, FileTaskEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, FileResultEntity.class);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanWorkDir() {
|
||||
FileUtil.del(new File(System.getProperty("java.io.tmpdir"), "collect-data-result/" + TASK_ID));
|
||||
}
|
||||
|
||||
private FileTaskEntity taskWithQueryFailedRows(int queryFailedRows) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(TASK_ID);
|
||||
task.setModuleType("COLLECT_DATA");
|
||||
task.setStatus("RUNNING");
|
||||
task.setTaskNo("CD-29991");
|
||||
task.setUserId(7L);
|
||||
task.setResultJson("{\"brandQueryFailedCount\":" + queryFailedRows + "}");
|
||||
return task;
|
||||
}
|
||||
|
||||
private TaskFileJobEntity job() {
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setTaskId(TASK_ID);
|
||||
job.setResultId(5001L);
|
||||
return job;
|
||||
}
|
||||
|
||||
private FileResultEntity result() {
|
||||
FileResultEntity result = new FileResultEntity();
|
||||
result.setId(5001L);
|
||||
result.setModuleType("COLLECT_DATA");
|
||||
result.setSourceFilename("keywords.xlsx");
|
||||
return result;
|
||||
}
|
||||
|
||||
private String stubHappyPathAndCaptureDetail() {
|
||||
when(fileTaskMapper.selectById(TASK_ID)).thenReturn(taskWithQueryFailedRows(7));
|
||||
when(fileResultMapper.selectById(5001L)).thenReturn(result());
|
||||
when(excelAssemblyService.writeWorkbookStreaming(any(), any(), any(), any())).thenReturn(12L);
|
||||
when(ossStorageService.uploadResultFile(any(), anyString())).thenReturn("collect-data/29991.xlsx");
|
||||
// 条件更新返回 1:任务翻成成功,这条「部分缺失」通知才会推给用户
|
||||
when(fileTaskMapper.update(isNull(), any())).thenReturn(1);
|
||||
|
||||
service.processResultFileJob(job());
|
||||
|
||||
ArgumentCaptor<String> detail = ArgumentCaptor.forClass(String.class);
|
||||
verify(notificationDispatchService).pushPartialResult(eq(7L), eq(TASK_ID), eq("COLLECT_DATA"),
|
||||
eq("CD-29991"), detail.capture());
|
||||
return detail.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void 部分结果通知只说可观测事实_不指认猜测的故障原因() {
|
||||
String detail = stubHappyPathAndCaptureDetail();
|
||||
|
||||
assertThat(detail).contains("7 行");
|
||||
// 「超时」「被限流」都是这一层观测不到的原因,写进文案会把排查方向带偏
|
||||
assertThat(detail).doesNotContain("超时");
|
||||
assertThat(detail).doesNotContain("限流");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 部分结果通知保留用户能自己核对的语义() {
|
||||
String detail = stubHappyPathAndCaptureDetail();
|
||||
|
||||
assertThat(detail).contains("不计入结果条数");
|
||||
assertThat(detail).contains("未返回结果");
|
||||
assertThat(detail).contains("重跑");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 任务已被判失败时不推部分结果通知() {
|
||||
when(fileTaskMapper.selectById(TASK_ID)).thenReturn(taskWithQueryFailedRows(7));
|
||||
when(fileResultMapper.selectById(5001L)).thenReturn(result());
|
||||
when(excelAssemblyService.writeWorkbookStreaming(any(), any(), any(), any())).thenReturn(12L);
|
||||
when(ossStorageService.uploadResultFile(any(), anyString())).thenReturn("collect-data/29991.xlsx");
|
||||
when(fileTaskMapper.update(isNull(), any())).thenReturn(0);
|
||||
|
||||
service.processResultFileJob(job());
|
||||
|
||||
verify(notificationDispatchService, org.mockito.Mockito.never())
|
||||
.pushPartialResult(anyLong(), anyLong(), anyString(), anyString(), anyString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user