feat(brand-check): 品牌检测按任务归属用户取代理并计用量——三处调用接入
- BrandCheckClient 契约扩展:构造器收 UserApiSecretService,check/checkAll/ checkTitleText(s) 全系带 userId,请求体新增 user_id + proxy_url(proxy 取自 用户密钥库 module=proxy);响应新增 reason/retryable,retryable=false(供应商 拒付、用户没配代理)当场停重试阶梯并短路整批 - 三处调用方接入:CollectDataService 品牌过滤、CollectDataBrandRecheckScheduler 补测(markAttempted 带真实原因)、AppearancePatentLlmClient 标题查标(取 SecretUsageContext 归属用户) - 用户未配置代理:整批本地定论「用户未配置代理」,零 HTTP,不再有平台钥匙兜底 - 测试迁移:15 支测试文件按新契约迁移(3 参构造、userId 实参、BrandCheckBatchResult 第 4 位 reason、Mockito 匹配器加宽);HTTP 类测试 stub findPlainValue 非空代理 以绕开零 HTTP 短路。13 类 119 例全绿(1 skip 为既有快照写盘门),test-compile 零错误
This commit is contained in:
+13
-13
@@ -117,7 +117,7 @@ class CollectDataBrandRecheckSchedulerTest {
|
||||
scheduler.runRound();
|
||||
|
||||
verify(recheckService).postponeWithoutAttempt(List.of(e1), 30);
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), anyString());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), anyString(), any());
|
||||
verify(notificationDispatchService, never()).pushRecheckOutcome(
|
||||
any(), any(), anyString(), anyString(), anyInt(), anyInt(), anyInt(), anyInt());
|
||||
}
|
||||
@@ -131,7 +131,7 @@ class CollectDataBrandRecheckSchedulerTest {
|
||||
scheduler.runRound();
|
||||
|
||||
verify(recheckService).markExhausted(e1, "任务不存在(可能已被清理)");
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), anyString());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -144,7 +144,7 @@ class CollectDataBrandRecheckSchedulerTest {
|
||||
scheduler.runRound();
|
||||
|
||||
verify(recheckService).postponeWithoutAttempt(List.of(e1), 30);
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), anyString());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -155,8 +155,8 @@ class CollectDataBrandRecheckSchedulerTest {
|
||||
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS"));
|
||||
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
|
||||
// "Nike " 带尾随空格:验证取品牌时按归一化口径去查(入队时已归一化,防御性兜底)
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of("Zara"), List.of()));
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of("Zara"), List.of(), ""));
|
||||
when(injector.apply(any(FileTaskEntity.class), any())).thenReturn(
|
||||
new GroupOutcome(9L, 3, 1));
|
||||
|
||||
@@ -176,8 +176,8 @@ class CollectDataBrandRecheckSchedulerTest {
|
||||
when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(e1));
|
||||
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS"));
|
||||
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of("zara")));
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of("zara"), ""));
|
||||
|
||||
scheduler.runRound();
|
||||
|
||||
@@ -194,7 +194,7 @@ class CollectDataBrandRecheckSchedulerTest {
|
||||
when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(e1));
|
||||
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "FAILED"));
|
||||
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms")))
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any()))
|
||||
.thenThrow(new RuntimeException("connection reset"));
|
||||
|
||||
scheduler.runRound();
|
||||
@@ -209,8 +209,8 @@ class CollectDataBrandRecheckSchedulerTest {
|
||||
when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(e1));
|
||||
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS"));
|
||||
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of()));
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), ""));
|
||||
when(injector.apply(any(FileTaskEntity.class), any()))
|
||||
.thenThrow(new RuntimeException("结果明细写入失败"));
|
||||
|
||||
@@ -232,9 +232,9 @@ class CollectDataBrandRecheckSchedulerTest {
|
||||
when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(f1, f2, f3, later));
|
||||
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS"));
|
||||
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(),
|
||||
List.of("bad-one", "bad-two", "bad-three")));
|
||||
List.of("bad-one", "bad-two", "bad-three"), ""));
|
||||
|
||||
scheduler.runRound();
|
||||
|
||||
@@ -243,7 +243,7 @@ class CollectDataBrandRecheckSchedulerTest {
|
||||
verify(recheckService).markAttempted(f2, "query_faild_data 持续非空");
|
||||
verify(recheckService).markAttempted(f3, "query_faild_data 持续非空");
|
||||
// 第二组整组顺延:不实测、不计数
|
||||
verify(brandCheckClient, org.mockito.Mockito.times(1)).checkAll(anyList(), anyString());
|
||||
verify(brandCheckClient, org.mockito.Mockito.times(1)).checkAll(anyList(), anyString(), any());
|
||||
verify(recheckService).postponeWithoutAttempt(List.of(later), 30);
|
||||
verify(recheckService, never()).markInjected(any());
|
||||
verify(recheckService, never()).markRejected(any());
|
||||
|
||||
+8
-8
@@ -143,8 +143,8 @@ class CollectDataServiceTxBoundaryTest {
|
||||
row.setAsin("B0COLLECT1");
|
||||
lenient().when(collectDataBatchQuery.filter(any(), any())).thenReturn(
|
||||
new CollectDataBatchQuery.FilterResult(List.of(row), 0, 0));
|
||||
lenient().when(brandBatchFilter.filter(any())).thenReturn(
|
||||
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row)));
|
||||
lenient().when(brandBatchFilter.filter(any(), any())).thenReturn(
|
||||
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row), ""));
|
||||
lenient().when(resultItemBatchWriter.upsertAccepted(anyLong(), anyLong(), anyString(), anyInt(), any(), anyString()))
|
||||
.thenReturn(new CollectDataResultItemBatchWriter.UpsertCounts(1, 0, 1));
|
||||
lenient().when(resultDetailCodec.encodeChunk(any())).thenReturn("{\"chunk\":1}");
|
||||
@@ -199,8 +199,8 @@ class CollectDataServiceTxBoundaryTest {
|
||||
void brandRejectedRowsWrittenToInvalidWriter() {
|
||||
CollectDataResultRowVo bad = new CollectDataResultRowVo();
|
||||
bad.setAsin("B0INVALID");
|
||||
when(brandBatchFilter.filter(any())).thenReturn(
|
||||
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(bad), List.of(), List.of()));
|
||||
when(brandBatchFilter.filter(any(), any())).thenReturn(
|
||||
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(bad), List.of(), List.of(), ""));
|
||||
|
||||
service.submitResult(TASK_ID, submitRequest());
|
||||
|
||||
@@ -211,8 +211,8 @@ class CollectDataServiceTxBoundaryTest {
|
||||
void brandQueryFailedRowsNotWrittenToInvalidWriter() {
|
||||
CollectDataResultRowVo queryFailed = new CollectDataResultRowVo();
|
||||
queryFailed.setAsin("B0QUERYFAIL");
|
||||
when(brandBatchFilter.filter(any())).thenReturn(
|
||||
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(queryFailed), List.of()));
|
||||
when(brandBatchFilter.filter(any(), any())).thenReturn(
|
||||
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(queryFailed), List.of(), ""));
|
||||
|
||||
service.submitResult(TASK_ID, submitRequest());
|
||||
|
||||
@@ -223,8 +223,8 @@ class CollectDataServiceTxBoundaryTest {
|
||||
void emptyRejectedOutcomeSkipsInvalidWriter() {
|
||||
CollectDataResultRowVo accepted = new CollectDataResultRowVo();
|
||||
accepted.setAsin("B0OK");
|
||||
when(brandBatchFilter.filter(any())).thenReturn(
|
||||
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(accepted)));
|
||||
when(brandBatchFilter.filter(any(), any())).thenReturn(
|
||||
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(accepted), ""));
|
||||
|
||||
service.submitResult(TASK_ID, submitRequest());
|
||||
|
||||
|
||||
+2
-2
@@ -337,11 +337,11 @@ class CollectDataSubmitResultSnapshotTest {
|
||||
+ " dedupe=0 invalid=0");
|
||||
return new CollectDataBatchQuery.FilterResult(kept, 0, 0);
|
||||
});
|
||||
when(brandFilter.filter(anyList())).thenAnswer(invocation -> {
|
||||
when(brandFilter.filter(anyList(), any())).thenAnswer(invocation -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<CollectDataResultRowVo> rows = invocation.getArgument(0);
|
||||
f.events.add("outsideLock brandFilter accepted=" + rows.size() + " rejected=0 queryFailed=0");
|
||||
return new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), rows);
|
||||
return new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), rows, "");
|
||||
});
|
||||
doAnswer(invocation -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
+48
-48
@@ -42,22 +42,22 @@ class CollectDataBrandBatchFilterTest {
|
||||
@Test
|
||||
void test_task_046_brand_normal_default_path() {
|
||||
// 正常输入:非空品牌批次发起检查,失败品牌行 rejected、其余 accepted。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
List.of(), List.of("Zara"), List.of(), ""));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(List.of(
|
||||
row("B000000001", "Zara"),
|
||||
row("B000000002", "Nike"),
|
||||
row("B000000003", "H&M")
|
||||
));
|
||||
), 7L);
|
||||
|
||||
assertEquals(1, outcome.rejected().size(), "失败品牌行进入 rejected");
|
||||
assertEquals("B000000001", outcome.rejected().get(0).getAsin());
|
||||
assertEquals(2, outcome.accepted().size(), "其余行 accepted");
|
||||
assertEquals("B000000002", outcome.accepted().get(0).getAsin(), "accepted 顺序稳定");
|
||||
assertEquals(0, outcome.queryFailed().size(), "无查询失败行");
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -67,74 +67,74 @@ class CollectDataBrandBatchFilterTest {
|
||||
for (int i = 0; i < 120; i++) {
|
||||
rows.add(row("B" + String.format("%09d", i + 1), i % 10 == 0 ? "bad-brand" : "good-" + (i / 10)));
|
||||
}
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("bad-brand"), List.of()));
|
||||
List.of(), List.of("bad-brand"), List.of(), ""));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows, 7L);
|
||||
|
||||
assertEquals(12, outcome.rejected().size(), "每批次 1 个失败品牌行");
|
||||
assertEquals(108, outcome.accepted().size(), "其余行 accepted");
|
||||
assertEquals("B000000001", outcome.rejected().get(0).getAsin(), "rejected 顺序稳定");
|
||||
assertEquals("B000000002", outcome.accepted().get(0).getAsin(), "accepted 顺序稳定");
|
||||
verify(brandCheckClient, times(12)).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, times(12)).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_046_brand_normal_repeated_operation_is_idempotent() {
|
||||
// 幂等:同一输入重复执行结果一致,不产生重复对象。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Nike"), List.of()));
|
||||
List.of(), List.of("Nike"), List.of(), ""));
|
||||
List<CollectDataResultRowVo> rows = List.of(
|
||||
row("B000000001", "Nike"),
|
||||
row("B000000002", "Adidas")
|
||||
);
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows, 7L);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows, 7L);
|
||||
|
||||
assertEquals(first.rejected(), second.rejected(), "重复执行 rejected 一致");
|
||||
assertEquals(first.accepted(), second.accepted(), "重复执行 accepted 一致");
|
||||
// 缓存生效:第二次执行全部命中缓存,不再发起远程调用。
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_046_brand_boundary_empty_input() {
|
||||
// 空输入:空列表返回空结果;全空品牌批次不发起远程检查,行全部 rejected。
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of());
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of(), 7L);
|
||||
assertEquals(0, empty.rejected().size() + empty.accepted().size() + empty.queryFailed().size(),
|
||||
"空列表返回空结果");
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any());
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome blankBatch = filter.filter(List.of(
|
||||
row("B000000001", ""), row("B000000002", null), row("B000000003", " ")
|
||||
));
|
||||
), 7L);
|
||||
assertEquals(3, blankBatch.rejected().size(), "空品牌批次行全部 rejected");
|
||||
assertEquals(0, blankBatch.accepted().size(), "空品牌批次无 accepted");
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_046_brand_boundary_single_item() {
|
||||
// 单元素:单行单品牌批次正常检查;单行空品牌批次不发远程调用。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of(), List.of()));
|
||||
List.of(), List.of(), List.of(), ""));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome single = filter.filter(List.of(
|
||||
row("B000000001", "solo")
|
||||
));
|
||||
), 7L);
|
||||
assertEquals(1, single.accepted().size(), "单行未命中失败归 accepted");
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any());
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome blank = filter.filter(List.of(
|
||||
row("B000000002", "")
|
||||
));
|
||||
), 7L);
|
||||
assertEquals(1, blank.rejected().size(), "单行空品牌归 rejected");
|
||||
// 同实例缓存生效:solo 已查过,空品牌批次无远程调用,总调用保持 1 次。
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -145,50 +145,50 @@ class CollectDataBrandBatchFilterTest {
|
||||
rows.add(row("B" + String.format("%09d", i + 1), "brand-" + (i % 100)));
|
||||
}
|
||||
rows.add(row("B999999999", " "));
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of(), List.of()));
|
||||
List.of(), List.of(), List.of(), ""));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows, 7L);
|
||||
|
||||
assertEquals(1, outcome.rejected().size(), "空品牌行归 rejected");
|
||||
assertEquals(5000, outcome.accepted().size(), "非空品牌行全 accepted");
|
||||
// 每批次 10 行恰好引入 10 个新品牌,10 批次后 100 品牌全部缓存,后续批次零调用。
|
||||
verify(brandCheckClient, times(10)).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, times(10)).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_046_brand_invalid_input_rejected() {
|
||||
// 非法参数:checkAll 返回 null 按无失败处理;null 行安全跳过不计数。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(null);
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(null);
|
||||
|
||||
List<CollectDataResultRowVo> rows = new ArrayList<>();
|
||||
rows.add(null);
|
||||
rows.add(row("B000000001", "Nike"));
|
||||
rows.add(row("B000000002", "Zara"));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows, 7L);
|
||||
|
||||
assertEquals(2, outcome.accepted().size(), "null 结果按无失败处理,行全 accepted");
|
||||
assertEquals(0, outcome.rejected().size(), "null 行不计数");
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_046_brand_dependency_failure_releases_resources() {
|
||||
// 依赖失败:远程检查抛错时批次整组降级 queryFailed,可恢复;恢复后重新检查成功。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong()))
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any()))
|
||||
.thenThrow(new RuntimeException("brand service down"))
|
||||
.thenReturn(new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
List.of(), List.of("Zara"), List.of(), ""));
|
||||
List<CollectDataResultRowVo> rows = List.of(
|
||||
row("B000000001", "Zara"), row("B000000002", "Nike"));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows, 7L);
|
||||
assertEquals(2, failed.queryFailed().size(), "依赖失败批次整组降级 queryFailed");
|
||||
assertEquals(0, failed.accepted().size() + failed.rejected().size(), "降级后无其他分类");
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows, 7L);
|
||||
assertEquals(1, recovered.rejected().size(), "恢复后失败品牌重新分类");
|
||||
assertEquals(1, recovered.accepted().size(), "恢复后其余行 accepted");
|
||||
assertTrue(recovered.queryFailed().isEmpty(), "恢复后无残留 queryFailed");
|
||||
@@ -202,17 +202,17 @@ class CollectDataBrandBatchFilterTest {
|
||||
@Test
|
||||
void test_chunk_budget_exhausted_stops_calling_upstream() {
|
||||
AtomicInteger calls = new AtomicInteger();
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> {
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenAnswer(invocation -> {
|
||||
calls.incrementAndGet();
|
||||
Thread.sleep(30);
|
||||
return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of());
|
||||
return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), "");
|
||||
});
|
||||
CollectDataBrandBatchFilter tight = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 1L);
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = tight.filter(List.of(
|
||||
row("B000000001", "brand-1"), row("B000000002", "brand-2"),
|
||||
row("B000000003", "brand-3"), row("B000000004", "brand-4"),
|
||||
row("B000000005", "brand-5"), row("B000000006", "brand-6")));
|
||||
row("B000000005", "brand-5"), row("B000000006", "brand-6")), 7L);
|
||||
|
||||
assertEquals(1, calls.get(), "预算用尽后不再为剩余批次发起远程检查");
|
||||
assertEquals(2, outcome.accepted().size(), "首个已检查批次照常分类");
|
||||
@@ -224,18 +224,18 @@ class CollectDataBrandBatchFilterTest {
|
||||
void test_cached_verdict_still_applies_and_blank_brand_rejects_after_budget_exhausted() {
|
||||
// 预算用尽只做「不再花代价」,不改分类口径:缓存里有结论的仍按结论走,
|
||||
// 空品牌行仍归 rejected(补测队列按品牌入队,空品牌进去就永远回不来)。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> {
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenAnswer(invocation -> {
|
||||
Thread.sleep(30);
|
||||
List<String> brands = invocation.getArgument(0);
|
||||
List<Object> hits = new ArrayList<>(brands.stream().filter("bad-brand"::equals).toList());
|
||||
return new BrandCheckClient.BrandCheckBatchResult(List.of(), hits, List.of());
|
||||
return new BrandCheckClient.BrandCheckBatchResult(List.of(), hits, List.of(), "");
|
||||
});
|
||||
CollectDataBrandBatchFilter tight = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 1L);
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = tight.filter(List.of(
|
||||
row("B000000001", "bad-brand"), row("B000000002", "seen-ok"),
|
||||
row("B000000003", "bad-brand"), row("B000000004", "brand-4"),
|
||||
row("B000000005", "brand-5"), row("B000000006", " ")));
|
||||
row("B000000005", "brand-5"), row("B000000006", " ")), 7L);
|
||||
|
||||
assertEquals(List.of("B000000001", "B000000003", "B000000006"), asins(outcome.rejected()),
|
||||
"侵权品牌与空品牌行归 rejected");
|
||||
@@ -247,19 +247,19 @@ class CollectDataBrandBatchFilterTest {
|
||||
@Test
|
||||
void test_each_batch_gets_only_the_remaining_chunk_budget() {
|
||||
// 共用一个截止时刻,而不是每批各领一份预算。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> {
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenAnswer(invocation -> {
|
||||
Thread.sleep(40);
|
||||
return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of());
|
||||
return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), "");
|
||||
});
|
||||
CollectDataBrandBatchFilter bounded = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 5_000L);
|
||||
|
||||
bounded.filter(List.of(
|
||||
row("B000000001", "brand-1"), row("B000000002", "brand-2"),
|
||||
row("B000000003", "brand-3"), row("B000000004", "brand-4"),
|
||||
row("B000000005", "brand-5"), row("B000000006", "brand-6")));
|
||||
row("B000000005", "brand-5"), row("B000000006", "brand-6")), 7L);
|
||||
|
||||
ArgumentCaptor<Long> budgets = ArgumentCaptor.forClass(Long.class);
|
||||
verify(brandCheckClient, times(3)).checkAll(anyList(), any(), budgets.capture());
|
||||
verify(brandCheckClient, times(3)).checkAll(anyList(), any(), budgets.capture(), any());
|
||||
List<Long> values = budgets.getAllValues();
|
||||
assertTrue(values.get(0) <= 5_000L, "首批拿到的不超过 chunk 预算");
|
||||
assertTrue(values.get(1) < values.get(0), "第二批只能用剩余的");
|
||||
@@ -269,15 +269,15 @@ class CollectDataBrandBatchFilterTest {
|
||||
@Test
|
||||
void test_zero_budget_passes_through_as_unlimited() {
|
||||
// 逃生阀:预算配 0 表示不掐批间,把每批的预算交给 checkAll 按配置走(旧口径)。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of()));
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), ""));
|
||||
CollectDataBrandBatchFilter unbounded = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 0L);
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = unbounded.filter(List.of(
|
||||
row("B000000001", "brand-1"), row("B000000002", "brand-2"), row("B000000003", "brand-3")));
|
||||
row("B000000001", "brand-1"), row("B000000002", "brand-2"), row("B000000003", "brand-3")), 7L);
|
||||
|
||||
ArgumentCaptor<Long> budgets = ArgumentCaptor.forClass(Long.class);
|
||||
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), budgets.capture());
|
||||
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), budgets.capture(), any());
|
||||
assertEquals(List.of(0L, 0L), budgets.getAllValues(), "预算 0 逐批透传 0(不限制)");
|
||||
assertEquals(3, outcome.accepted().size(), "不限制时没有行被延后");
|
||||
}
|
||||
|
||||
+35
-35
@@ -41,32 +41,32 @@ class CollectDataBrandCacheTest {
|
||||
@Test
|
||||
void test_task_047_cache_brand_normal_default_path() {
|
||||
// 正常输入:首查品牌发起远程检查并缓存判定,分类正确。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
List.of(), List.of("Zara"), List.of(), ""));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(List.of(
|
||||
row("B000000001", "Zara"),
|
||||
row("B000000002", "Nike")
|
||||
));
|
||||
), 7L);
|
||||
|
||||
assertEquals(1, outcome.rejected().size(), "失败品牌行 rejected");
|
||||
assertEquals("B000000001", outcome.rejected().get(0).getAsin());
|
||||
assertEquals(1, outcome.accepted().size(), "其余行 accepted");
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_normal_multiple_items() {
|
||||
// 批量场景:多 chunk 提交时同品牌只远程检查一次,每 chunk 只查未缓存品牌。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
filter.filter(List.of(row("B000000001", "Zara"), row("B000000002", "Nike")));
|
||||
List.of(), List.of("Zara"), List.of(), ""));
|
||||
filter.filter(List.of(row("B000000001", "Zara"), row("B000000002", "Nike")), 7L);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(List.of(
|
||||
row("B000000001", "Zara"),
|
||||
row("B000000003", "Adidas")
|
||||
));
|
||||
), 7L);
|
||||
|
||||
assertEquals(1, second.rejected().size(), "chunk2 Zara 命中缓存仍 rejected");
|
||||
assertEquals(1, second.accepted().size(), "chunk2 新品牌 Adidas accepted");
|
||||
@@ -78,63 +78,63 @@ class CollectDataBrandCacheTest {
|
||||
@Test
|
||||
void test_task_047_cache_brand_normal_repeated_operation_is_idempotent() {
|
||||
// 幂等:同一输入重复执行结果一致,第二次全部命中缓存不发远程调用。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Nike"), List.of()));
|
||||
List.of(), List.of("Nike"), List.of(), ""));
|
||||
List<CollectDataResultRowVo> rows = List.of(
|
||||
row("B000000001", "Nike"),
|
||||
row("B000000002", "Adidas")
|
||||
);
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows, 7L);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows, 7L);
|
||||
|
||||
assertEquals(first.rejected(), second.rejected(), "重复执行 rejected 一致");
|
||||
assertEquals(first.accepted(), second.accepted(), "重复执行 accepted 一致");
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_boundary_empty_input() {
|
||||
// 空输入:空列表与全空品牌批次均不发起远程调用,也不写缓存。
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of());
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of(), 7L);
|
||||
assertEquals(0, empty.rejected().size() + empty.accepted().size() + empty.queryFailed().size(),
|
||||
"空列表返回空结果");
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any());
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome blank = filter.filter(List.of(
|
||||
row("B000000001", "")
|
||||
));
|
||||
), 7L);
|
||||
assertEquals(1, blank.rejected().size(), "空品牌行 rejected");
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_boundary_single_item() {
|
||||
// 单元素:单品牌首查一次;同品牌再出现时命中缓存零调用。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of(), List.of()));
|
||||
List.of(), List.of(), List.of(), ""));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome single = filter.filter(List.of(
|
||||
row("B000000001", "solo")
|
||||
));
|
||||
), 7L);
|
||||
assertEquals(1, single.accepted().size(), "单品牌未命中失败 accepted");
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome again = filter.filter(List.of(
|
||||
row("B000000002", "solo")
|
||||
));
|
||||
), 7L);
|
||||
assertEquals(1, again.accepted().size(), "同品牌再次出现结果一致");
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_boundary_limit_and_overflow() {
|
||||
// 上限/超限:缓存容量 8,首批 10 个品牌淘汰最旧 2 个;第二批同品牌
|
||||
// 只重查被淘汰的 2 个,其余 8 个命中缓存;无无界增长。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of(), List.of()));
|
||||
List.of(), List.of(), List.of(), ""));
|
||||
CollectDataBrandBatchFilter smallCache = new CollectDataBrandBatchFilter(brandCheckClient, 10, 8);
|
||||
List<CollectDataResultRowVo> first = new ArrayList<>();
|
||||
List<CollectDataResultRowVo> second = new ArrayList<>();
|
||||
@@ -143,8 +143,8 @@ class CollectDataBrandCacheTest {
|
||||
second.add(row("B" + (100 + i), "brand-" + i));
|
||||
}
|
||||
|
||||
smallCache.filter(first);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome out = smallCache.filter(second);
|
||||
smallCache.filter(first, 7L);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome out = smallCache.filter(second, 7L);
|
||||
|
||||
assertEquals(10, out.accepted().size(), "第二批 10 行全部 accepted");
|
||||
ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
|
||||
@@ -157,39 +157,39 @@ class CollectDataBrandCacheTest {
|
||||
@Test
|
||||
void test_task_047_cache_brand_invalid_input_rejected() {
|
||||
// 非法参数:checkAll 返回 null 按无失败处理并缓存;null 行安全跳过。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(null);
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(null);
|
||||
|
||||
List<CollectDataResultRowVo> rows = new ArrayList<>();
|
||||
rows.add(null);
|
||||
rows.add(row("B000000001", "Nike"));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows, 7L);
|
||||
assertEquals(1, first.accepted().size(), "null 结果按无失败处理");
|
||||
assertEquals(0, first.rejected().size(), "null 行不计数");
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows, 7L);
|
||||
assertEquals(1, second.accepted().size(), "重复执行结果一致");
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_dependency_failure_releases_resources() {
|
||||
// 依赖失败:远程抛错整批 queryFailed 且不缓存;恢复后重查成功,无残留状态。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong()))
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any()))
|
||||
.thenThrow(new RuntimeException("brand service down"))
|
||||
.thenReturn(new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
List.of(), List.of("Zara"), List.of(), ""));
|
||||
List<CollectDataResultRowVo> rows = List.of(
|
||||
row("B000000001", "Zara"), row("B000000002", "Nike"));
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows, 7L);
|
||||
assertEquals(2, failed.queryFailed().size(), "失败批次整组降级 queryFailed");
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows);
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows, 7L);
|
||||
assertEquals(1, recovered.rejected().size(), "恢复后重新检查并分类");
|
||||
assertEquals(1, recovered.accepted().size(), "恢复后 accepted 正确");
|
||||
assertTrue(recovered.queryFailed().isEmpty(), "恢复后无残留 queryFailed");
|
||||
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), anyLong());
|
||||
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), anyLong(), any());
|
||||
}
|
||||
|
||||
private static CollectDataResultRowVo row(String asin, String brand) {
|
||||
|
||||
Reference in New Issue
Block a user