fix(品牌检测): 一次分片回传领一份 chunk 预算,用尽即转补测队列
上游 16890 卡死时每批各领一个 90 秒总超时,一个 chunk 十几批能叠到十几分钟, 桌面端是在同步等响应的路径上替它扛。现按 chunk 给 10 秒预算,预算内逐批只把 剩余额度交给远程;用尽后未查的品牌一律按查询失败入补测队列,既不按 accepted 放行也不写进无效品牌表。外观合批、品牌补测调度等后台路径仍按 90 秒走。
This commit is contained in:
+81
@@ -106,6 +106,87 @@ class BrandCheckClientTest {
|
||||
return new BrandCheckClient(props, null);
|
||||
}
|
||||
|
||||
/** 重试阶梯可调的构造:用于验证「耗时预算掐的是重试,不是首轮」。 */
|
||||
private BrandCheckClient newClient(HttpServer server, int retryTimes, int retryIntervalMillis, int totalTimeoutMillis) {
|
||||
BrandCheckProperties props = new BrandCheckProperties();
|
||||
props.setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
props.setPath("/brand_check");
|
||||
props.setRetryTimes(retryTimes);
|
||||
props.setRetryIntervalMillis(retryIntervalMillis);
|
||||
props.setTotalTimeoutMillis(totalTimeoutMillis);
|
||||
return new BrandCheckClient(props, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void callerBudgetCutsTheRetryLadderButNeverSkipsTheFirstAttempt() throws Exception {
|
||||
AtomicInteger attempts = new AtomicInteger();
|
||||
HttpServer server = startBrandCheckServer(exchange -> {
|
||||
attempts.incrementAndGet();
|
||||
respond(exchange, "{\"faild_data\":[],\"query_faild_data\":[\"Apple\"]}");
|
||||
});
|
||||
try {
|
||||
// 阶梯 20/40/60…:跑满 10 次要 0.9 秒,60ms 预算只够跑到第 2~3 次。
|
||||
BrandCheckClient unlimited = newClient(server, 10, 20, 0);
|
||||
unlimited.checkAll(List.of("Apple"), "Terms");
|
||||
int attemptsWithoutBudget = attempts.get();
|
||||
|
||||
attempts.set(0);
|
||||
BrandCheckClient client = newClient(server, 10, 20, 0);
|
||||
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 60L);
|
||||
|
||||
assertThat(attemptsWithoutBudget).as("不限制时跑满整条重试阶梯").isEqualTo(10);
|
||||
assertThat(attempts.get()).as("预算用尽即停重试,但首轮始终执行")
|
||||
.isBetween(1, attemptsWithoutBudget - 1);
|
||||
assertThat(result.hasQueryFailedData()).as("掐在预算上仍按查询失败收尾,交调用方降级")
|
||||
.isTrue();
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoArgCheckAllStillUsesTheConfiguredTotalTimeout() throws Exception {
|
||||
AtomicInteger attempts = new AtomicInteger();
|
||||
HttpServer server = startBrandCheckServer(exchange -> {
|
||||
attempts.incrementAndGet();
|
||||
respond(exchange, "{\"faild_data\":[],\"query_faild_data\":[\"Apple\"]}");
|
||||
});
|
||||
try {
|
||||
BrandCheckClient client = newClient(server, 10, 20, 60);
|
||||
|
||||
client.checkAll(List.of("Apple"), "Terms");
|
||||
|
||||
assertThat(attempts.get()).as("两参重载按 total-timeout-millis 兜底,未退化也不越界")
|
||||
.isBetween(1, 9);
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void exhaustedBudgetStillIssuesOneRequestPerBrand() throws Exception {
|
||||
// 预算掐的必须是「重试」而不是首轮:检查线程池只有 3 个坑,第 4 个品牌排到时 1ms 预算
|
||||
// 早已过期。若此时连首轮都不发,品牌就在"一次都没查过"的状态下被判降级。
|
||||
AtomicInteger requests = new AtomicInteger();
|
||||
HttpServer server = startBrandCheckServer(exchange -> {
|
||||
requests.incrementAndGet();
|
||||
respond(exchange, "{\"faild_data\":[],\"query_faild_data\":[\"Apple\"]}");
|
||||
});
|
||||
try {
|
||||
// 重试间隔 200ms 保证第四个品牌必然在预算耗尽后才开跑,不与首轮判断抢时序。
|
||||
BrandCheckClient client = newClient(server, 3, 200, 0);
|
||||
List<String> brands = List.of("b1", "b2", "b3", "b4");
|
||||
|
||||
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms", 1L);
|
||||
|
||||
assertThat(requests.get()).as("预算过期后每个品牌仍各发一次真实请求").isEqualTo(brands.size());
|
||||
assertThat(result.brands()).as("降级不丢品牌,入参集合原样带回").containsExactlyElementsOf(brands);
|
||||
assertThat(result.hasQueryFailedData()).as("查过但没结论,按查询失败交调用方降级").isTrue();
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpServer startBrandCheckServer(com.sun.net.httpserver.HttpHandler handler) throws IOException {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/brand_check", handler);
|
||||
|
||||
+111
-16
@@ -4,14 +4,17 @@ import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
|
||||
import com.nanri.aiimage.modules.collectdata.model.vo.CollectDataResultRowVo;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
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.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
@@ -39,7 +42,7 @@ class CollectDataBrandBatchFilterTest {
|
||||
@Test
|
||||
void test_task_046_brand_normal_default_path() {
|
||||
// 正常输入:非空品牌批次发起检查,失败品牌行 rejected、其余 accepted。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
|
||||
@@ -54,7 +57,7 @@ class CollectDataBrandBatchFilterTest {
|
||||
assertEquals(2, outcome.accepted().size(), "其余行 accepted");
|
||||
assertEquals("B000000002", outcome.accepted().get(0).getAsin(), "accepted 顺序稳定");
|
||||
assertEquals(0, outcome.queryFailed().size(), "无查询失败行");
|
||||
verify(brandCheckClient).checkAll(anyList(), any());
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -64,7 +67,7 @@ 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())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("bad-brand"), List.of()));
|
||||
|
||||
@@ -74,13 +77,13 @@ class CollectDataBrandBatchFilterTest {
|
||||
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());
|
||||
verify(brandCheckClient, times(12)).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_046_brand_normal_repeated_operation_is_idempotent() {
|
||||
// 幂等:同一输入重复执行结果一致,不产生重复对象。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Nike"), List.of()));
|
||||
List<CollectDataResultRowVo> rows = List.of(
|
||||
@@ -94,7 +97,7 @@ class CollectDataBrandBatchFilterTest {
|
||||
assertEquals(first.rejected(), second.rejected(), "重复执行 rejected 一致");
|
||||
assertEquals(first.accepted(), second.accepted(), "重复执行 accepted 一致");
|
||||
// 缓存生效:第二次执行全部命中缓存,不再发起远程调用。
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -103,20 +106,20 @@ class CollectDataBrandBatchFilterTest {
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of());
|
||||
assertEquals(0, empty.rejected().size() + empty.accepted().size() + empty.queryFailed().size(),
|
||||
"空列表返回空结果");
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome blankBatch = filter.filter(List.of(
|
||||
row("B000000001", ""), row("B000000002", null), row("B000000003", " ")
|
||||
));
|
||||
assertEquals(3, blankBatch.rejected().size(), "空品牌批次行全部 rejected");
|
||||
assertEquals(0, blankBatch.accepted().size(), "空品牌批次无 accepted");
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_046_brand_boundary_single_item() {
|
||||
// 单元素:单行单品牌批次正常检查;单行空品牌批次不发远程调用。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of(), List.of()));
|
||||
|
||||
@@ -124,14 +127,14 @@ class CollectDataBrandBatchFilterTest {
|
||||
row("B000000001", "solo")
|
||||
));
|
||||
assertEquals(1, single.accepted().size(), "单行未命中失败归 accepted");
|
||||
verify(brandCheckClient).checkAll(anyList(), any());
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome blank = filter.filter(List.of(
|
||||
row("B000000002", "")
|
||||
));
|
||||
assertEquals(1, blank.rejected().size(), "单行空品牌归 rejected");
|
||||
// 同实例缓存生效:solo 已查过,空品牌批次无远程调用,总调用保持 1 次。
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -142,7 +145,7 @@ class CollectDataBrandBatchFilterTest {
|
||||
rows.add(row("B" + String.format("%09d", i + 1), "brand-" + (i % 100)));
|
||||
}
|
||||
rows.add(row("B999999999", " "));
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of(), List.of()));
|
||||
|
||||
@@ -151,13 +154,13 @@ class CollectDataBrandBatchFilterTest {
|
||||
assertEquals(1, outcome.rejected().size(), "空品牌行归 rejected");
|
||||
assertEquals(5000, outcome.accepted().size(), "非空品牌行全 accepted");
|
||||
// 每批次 10 行恰好引入 10 个新品牌,10 批次后 100 品牌全部缓存,后续批次零调用。
|
||||
verify(brandCheckClient, times(10)).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, times(10)).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_046_brand_invalid_input_rejected() {
|
||||
// 非法参数:checkAll 返回 null 按无失败处理;null 行安全跳过不计数。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(null);
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(null);
|
||||
|
||||
List<CollectDataResultRowVo> rows = new ArrayList<>();
|
||||
rows.add(null);
|
||||
@@ -168,13 +171,13 @@ class CollectDataBrandBatchFilterTest {
|
||||
|
||||
assertEquals(2, outcome.accepted().size(), "null 结果按无失败处理,行全 accepted");
|
||||
assertEquals(0, outcome.rejected().size(), "null 行不计数");
|
||||
verify(brandCheckClient).checkAll(anyList(), any());
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_046_brand_dependency_failure_releases_resources() {
|
||||
// 依赖失败:远程检查抛错时批次整组降级 queryFailed,可恢复;恢复后重新检查成功。
|
||||
when(brandCheckClient.checkAll(anyList(), any()))
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong()))
|
||||
.thenThrow(new RuntimeException("brand service down"))
|
||||
.thenReturn(new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
@@ -191,6 +194,98 @@ class CollectDataBrandBatchFilterTest {
|
||||
assertTrue(recovered.queryFailed().isEmpty(), "恢复后无残留 queryFailed");
|
||||
}
|
||||
|
||||
/**
|
||||
* 一次调用(= 一个 chunk)共用一个总耗时预算:预算用尽后剩余批次不再捶上游。
|
||||
* 旧口径是每批各领一份 aiimage.brand-check.total-timeout-millis(90s),批数越多、
|
||||
* 客户端那次同步的分片回传 POST 等得越久(taskId 28599 实测单批就等了 103.5 秒)。
|
||||
*/
|
||||
@Test
|
||||
void test_chunk_budget_exhausted_stops_calling_upstream() {
|
||||
AtomicInteger calls = new AtomicInteger();
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> {
|
||||
calls.incrementAndGet();
|
||||
Thread.sleep(30);
|
||||
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")));
|
||||
|
||||
assertEquals(1, calls.get(), "预算用尽后不再为剩余批次发起远程检查");
|
||||
assertEquals(2, outcome.accepted().size(), "首个已检查批次照常分类");
|
||||
assertEquals(4, outcome.queryFailed().size(), "剩余批次行全部转入补测队列");
|
||||
assertEquals(0, outcome.rejected().size(), "延后行不写无效品牌表(没查过就拉黑 = 永久误杀)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_cached_verdict_still_applies_and_blank_brand_rejects_after_budget_exhausted() {
|
||||
// 预算用尽只做「不再花代价」,不改分类口径:缓存里有结论的仍按结论走,
|
||||
// 空品牌行仍归 rejected(补测队列按品牌入队,空品牌进去就永远回不来)。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).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());
|
||||
});
|
||||
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", " ")));
|
||||
|
||||
assertEquals(List.of("B000000001", "B000000003", "B000000006"), asins(outcome.rejected()),
|
||||
"侵权品牌与空品牌行归 rejected");
|
||||
assertEquals(List.of("B000000002"), asins(outcome.accepted()), "已判定无侵权的行放行");
|
||||
assertEquals(List.of("B000000004", "B000000005"), asins(outcome.queryFailed()),
|
||||
"只有没查过的品牌延后补测");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_each_batch_gets_only_the_remaining_chunk_budget() {
|
||||
// 共用一个截止时刻,而不是每批各领一份预算。
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> {
|
||||
Thread.sleep(40);
|
||||
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")));
|
||||
|
||||
ArgumentCaptor<Long> budgets = ArgumentCaptor.forClass(Long.class);
|
||||
verify(brandCheckClient, times(3)).checkAll(anyList(), any(), budgets.capture());
|
||||
List<Long> values = budgets.getAllValues();
|
||||
assertTrue(values.get(0) <= 5_000L, "首批拿到的不超过 chunk 预算");
|
||||
assertTrue(values.get(1) < values.get(0), "第二批只能用剩余的");
|
||||
assertTrue(values.get(2) < values.get(1), "预算随批次单调递减");
|
||||
}
|
||||
|
||||
@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()));
|
||||
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")));
|
||||
|
||||
ArgumentCaptor<Long> budgets = ArgumentCaptor.forClass(Long.class);
|
||||
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), budgets.capture());
|
||||
assertEquals(List.of(0L, 0L), budgets.getAllValues(), "预算 0 逐批透传 0(不限制)");
|
||||
assertEquals(3, outcome.accepted().size(), "不限制时没有行被延后");
|
||||
}
|
||||
|
||||
private static List<String> asins(List<CollectDataResultRowVo> rows) {
|
||||
return rows.stream().map(CollectDataResultRowVo::getAsin).toList();
|
||||
}
|
||||
|
||||
private static CollectDataResultRowVo row(String asin, String brand) {
|
||||
CollectDataResultRowVo row = new CollectDataResultRowVo();
|
||||
row.setAsin(asin);
|
||||
|
||||
+17
-16
@@ -13,6 +13,7 @@ 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.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
@@ -40,7 +41,7 @@ class CollectDataBrandCacheTest {
|
||||
@Test
|
||||
void test_task_047_cache_brand_normal_default_path() {
|
||||
// 正常输入:首查品牌发起远程检查并缓存判定,分类正确。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
|
||||
@@ -52,13 +53,13 @@ class CollectDataBrandCacheTest {
|
||||
assertEquals(1, outcome.rejected().size(), "失败品牌行 rejected");
|
||||
assertEquals("B000000001", outcome.rejected().get(0).getAsin());
|
||||
assertEquals(1, outcome.accepted().size(), "其余行 accepted");
|
||||
verify(brandCheckClient).checkAll(anyList(), any());
|
||||
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_normal_multiple_items() {
|
||||
// 批量场景:多 chunk 提交时同品牌只远程检查一次,每 chunk 只查未缓存品牌。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
filter.filter(List.of(row("B000000001", "Zara"), row("B000000002", "Nike")));
|
||||
@@ -70,14 +71,14 @@ class CollectDataBrandCacheTest {
|
||||
assertEquals(1, second.rejected().size(), "chunk2 Zara 命中缓存仍 rejected");
|
||||
assertEquals(1, second.accepted().size(), "chunk2 新品牌 Adidas accepted");
|
||||
ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(brandCheckClient, times(2)).checkAll(captor.capture(), any());
|
||||
verify(brandCheckClient, times(2)).checkAll(captor.capture(), any(), anyLong());
|
||||
assertEquals(List.of("Adidas"), captor.getAllValues().get(1), "第二次只查未缓存品牌");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_normal_repeated_operation_is_idempotent() {
|
||||
// 幂等:同一输入重复执行结果一致,第二次全部命中缓存不发远程调用。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Nike"), List.of()));
|
||||
List<CollectDataResultRowVo> rows = List.of(
|
||||
@@ -90,7 +91,7 @@ class CollectDataBrandCacheTest {
|
||||
|
||||
assertEquals(first.rejected(), second.rejected(), "重复执行 rejected 一致");
|
||||
assertEquals(first.accepted(), second.accepted(), "重复执行 accepted 一致");
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -99,19 +100,19 @@ class CollectDataBrandCacheTest {
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of());
|
||||
assertEquals(0, empty.rejected().size() + empty.accepted().size() + empty.queryFailed().size(),
|
||||
"空列表返回空结果");
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome blank = filter.filter(List.of(
|
||||
row("B000000001", "")
|
||||
));
|
||||
assertEquals(1, blank.rejected().size(), "空品牌行 rejected");
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_boundary_single_item() {
|
||||
// 单元素:单品牌首查一次;同品牌再出现时命中缓存零调用。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of(), List.of()));
|
||||
|
||||
@@ -124,14 +125,14 @@ class CollectDataBrandCacheTest {
|
||||
row("B000000002", "solo")
|
||||
));
|
||||
assertEquals(1, again.accepted().size(), "同品牌再次出现结果一致");
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_boundary_limit_and_overflow() {
|
||||
// 上限/超限:缓存容量 8,首批 10 个品牌淘汰最旧 2 个;第二批同品牌
|
||||
// 只重查被淘汰的 2 个,其余 8 个命中缓存;无无界增长。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
|
||||
new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of(), List.of()));
|
||||
CollectDataBrandBatchFilter smallCache = new CollectDataBrandBatchFilter(brandCheckClient, 10, 8);
|
||||
@@ -147,7 +148,7 @@ class CollectDataBrandCacheTest {
|
||||
|
||||
assertEquals(10, out.accepted().size(), "第二批 10 行全部 accepted");
|
||||
ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(brandCheckClient, times(2)).checkAll(captor.capture(), any());
|
||||
verify(brandCheckClient, times(2)).checkAll(captor.capture(), any(), anyLong());
|
||||
List<String> secondCall = captor.getAllValues().get(1);
|
||||
assertEquals(2, secondCall.size(), "仅重查被淘汰的最旧 2 个品牌");
|
||||
assertTrue(secondCall.containsAll(List.of("brand-0", "brand-1")), "淘汰的是最旧条目");
|
||||
@@ -156,7 +157,7 @@ class CollectDataBrandCacheTest {
|
||||
@Test
|
||||
void test_task_047_cache_brand_invalid_input_rejected() {
|
||||
// 非法参数:checkAll 返回 null 按无失败处理并缓存;null 行安全跳过。
|
||||
when(brandCheckClient.checkAll(anyList(), any())).thenReturn(null);
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(null);
|
||||
|
||||
List<CollectDataResultRowVo> rows = new ArrayList<>();
|
||||
rows.add(null);
|
||||
@@ -168,13 +169,13 @@ class CollectDataBrandCacheTest {
|
||||
|
||||
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows);
|
||||
assertEquals(1, second.accepted().size(), "重复执行结果一致");
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_047_cache_brand_dependency_failure_releases_resources() {
|
||||
// 依赖失败:远程抛错整批 queryFailed 且不缓存;恢复后重查成功,无残留状态。
|
||||
when(brandCheckClient.checkAll(anyList(), any()))
|
||||
when(brandCheckClient.checkAll(anyList(), any(), anyLong()))
|
||||
.thenThrow(new RuntimeException("brand service down"))
|
||||
.thenReturn(new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(), List.of("Zara"), List.of()));
|
||||
@@ -188,7 +189,7 @@ class CollectDataBrandCacheTest {
|
||||
assertEquals(1, recovered.rejected().size(), "恢复后重新检查并分类");
|
||||
assertEquals(1, recovered.accepted().size(), "恢复后 accepted 正确");
|
||||
assertTrue(recovered.queryFailed().isEmpty(), "恢复后无残留 queryFailed");
|
||||
verify(brandCheckClient, times(2)).checkAll(anyList(), any());
|
||||
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), anyLong());
|
||||
}
|
||||
|
||||
private static CollectDataResultRowVo row(String asin, String brand) {
|
||||
|
||||
Reference in New Issue
Block a user