refactor(ziniao/shopkey)+fix(stale): 打破模块循环依赖 + 采集陈旧判死全局化
模块边界(消除 ziniao ↔ shopkey 真实循环依赖):
- 新增 ziniao/service/port/{ShopKeyCatalogPort,ManagedShopNamePort}:消费方声明契约
- shopkey 侧新增 ShopKeyCatalogAdapter(读 shop_key + 白名单状态回写)、
ManagedShopNameAdapter(店铺名校验),实现上述端口
- ZiniaoApiKeyProvider 改经端口取数,不再 import shopkey 的 Mapper/Entity;
ZiniaoShopSwitchService 改依赖 ManagedShopNamePort
- 结果:ziniao → shopkey 的 import 归零,依赖单向(shopkey → ziniao)
陈旧判死(G1 全局判死 + D9 条件更新,替代此前的 owner 过滤/旧实体覆盖):
- ShopDataCrawlTaskService.finalizeOwnedStaleTasks → finalizeStaleTasks:去掉 owner 过滤,
并入 DeleteBrandStaleTaskService 的 stale-check 巡检线(job 锁保证单实例扫描)
- 判死前必须持有任务锁(非阻塞获取,锁被占本轮跳过),FAILED 写入改 status CAS,
仅在确实由 RUNNING 翻转为 FAILED 时才删缓存与分片(原实现会用扫描期旧实体覆盖在途任务)
- tryFinalizeTask 增加 allowOwnerTakeover 重载:判死场景允许跨实例接管(P1-8 盲区)
- 测试同步:owner 契约用例改为全局判死口径;mock 的 CAS 需先渲染 SQL 片段
(MyBatis-Plus 的 where 参数延迟填充)才读参数表;新增锁被占跳过的用例
mvn test 2796 全绿
This commit is contained in:
+23
-31
@@ -1,41 +1,26 @@
|
||||
package com.nanri.aiimage.modules.ziniao.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.modules.shopkey.mapper.ShopKeyMapper;
|
||||
import com.nanri.aiimage.modules.shopkey.model.entity.ShopKeyEntity;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import com.nanri.aiimage.modules.ziniao.service.port.ShopKeyCatalogPort;
|
||||
import com.nanri.aiimage.modules.ziniao.service.port.ShopKeyCatalogPort.ShopKeyRecord;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class ZiniaoApiKeyProviderTest {
|
||||
|
||||
@BeforeAll
|
||||
static void initializeMybatisMetadata() {
|
||||
TableInfoHelper.initTableInfo(
|
||||
new MapperBuilderAssistant(new MybatisConfiguration(), "ziniao-api-key-provider-test"),
|
||||
ShopKeyEntity.class
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateNormalizedTokensShareOneRefreshAccountAndAllRecordIds() {
|
||||
ShopKeyMapper mapper = mock(ShopKeyMapper.class);
|
||||
ShopKeyEntity latest = shopKey(12L, " Bearer duplicate-key ", "最新账号");
|
||||
ShopKeyEntity older = shopKey(8L, "duplicate-key", "旧账号");
|
||||
when(mapper.selectList(any())).thenReturn(List.of(latest, older));
|
||||
ShopKeyCatalogPort port = mock(ShopKeyCatalogPort.class);
|
||||
ShopKeyRecord latest = shopKey(12L, " Bearer duplicate-key ", "最新账号");
|
||||
ShopKeyRecord older = shopKey(8L, "duplicate-key", "旧账号");
|
||||
when(port.listAllOrderByIdDesc()).thenReturn(List.of(latest, older));
|
||||
|
||||
ZiniaoApiKeyProvider provider = new ZiniaoApiKeyProvider(mapper);
|
||||
ZiniaoApiKeyProvider provider = new ZiniaoApiKeyProvider(port);
|
||||
|
||||
List<ZiniaoApiKeyProvider.ApiKeyAccount> accounts = provider.listApiKeyAccounts();
|
||||
|
||||
@@ -47,8 +32,8 @@ class ZiniaoApiKeyProviderTest {
|
||||
|
||||
@Test
|
||||
void whitelistResultUpdatesEveryRecordForTheNormalizedToken() {
|
||||
ShopKeyMapper mapper = mock(ShopKeyMapper.class);
|
||||
ZiniaoApiKeyProvider provider = new ZiniaoApiKeyProvider(mapper);
|
||||
ShopKeyCatalogPort port = mock(ShopKeyCatalogPort.class);
|
||||
ZiniaoApiKeyProvider provider = new ZiniaoApiKeyProvider(port);
|
||||
ZiniaoApiKeyProvider.ApiKeyAccount account = new ZiniaoApiKeyProvider.ApiKeyAccount(
|
||||
"duplicate-key",
|
||||
"账号",
|
||||
@@ -57,14 +42,21 @@ class ZiniaoApiKeyProviderTest {
|
||||
|
||||
provider.markIpWhitelistBlocked(account, "当前服务器 IP 未加入紫鸟白名单");
|
||||
|
||||
verify(mapper).update(isNull(), any(Wrapper.class));
|
||||
verify(port).updateIpWhitelist(List.of(12L, 8L), ZiniaoApiKeyProvider.IP_WHITELIST_STATUS_BLOCKED,
|
||||
"当前服务器 IP 未加入紫鸟白名单", 1);
|
||||
}
|
||||
|
||||
private ShopKeyEntity shopKey(long id, String token, String accountName) {
|
||||
ShopKeyEntity entity = new ShopKeyEntity();
|
||||
entity.setId(id);
|
||||
entity.setZiniaoToken(token);
|
||||
entity.setZiniaoAccountName(accountName);
|
||||
return entity;
|
||||
@Test
|
||||
void recordsWithoutValidTokenAreIgnored() {
|
||||
ShopKeyCatalogPort port = mock(ShopKeyCatalogPort.class);
|
||||
when(port.listAllOrderByIdDesc()).thenReturn(List.of(shopKey(3L, " ", "空令牌账号")));
|
||||
|
||||
ZiniaoApiKeyProvider provider = new ZiniaoApiKeyProvider(port);
|
||||
|
||||
assertEquals(List.of(), provider.listApiKeyAccounts());
|
||||
}
|
||||
|
||||
private ShopKeyRecord shopKey(long id, String token, String accountName) {
|
||||
return new ShopKeyRecord(id, token, accountName, null, 0);
|
||||
}
|
||||
}
|
||||
|
||||
+13
-13
@@ -1,7 +1,7 @@
|
||||
package com.nanri.aiimage.modules.ziniao.service;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.shopkey.service.ShopManageService;
|
||||
import com.nanri.aiimage.modules.ziniao.service.port.ManagedShopNamePort;
|
||||
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -24,15 +25,14 @@ class ZiniaoShopSwitchServiceTest {
|
||||
@Mock
|
||||
private ZiniaoShopIndexService ziniaoShopIndexService;
|
||||
@Mock
|
||||
private ShopManageService shopManageService;
|
||||
private ManagedShopNamePort managedShopNamePort;
|
||||
|
||||
@Test
|
||||
void missingManagedShopStopsIndexedLookup() {
|
||||
String message = "后台店铺管理中未找到店铺:缺失店铺,请先添加店铺信息";
|
||||
when(shopManageService.requireShopByName("缺失店铺"))
|
||||
.thenThrow(new BusinessException(message));
|
||||
doThrow(new BusinessException(message)).when(managedShopNamePort).requireShopByName("缺失店铺");
|
||||
ZiniaoShopSwitchService service = new ZiniaoShopSwitchService(
|
||||
ziniaoAuthService, ziniaoShopIndexService, shopManageService);
|
||||
ziniaoAuthService, ziniaoShopIndexService, managedShopNamePort);
|
||||
|
||||
BusinessException exception = assertThrows(BusinessException.class,
|
||||
() -> service.findIndexedStoreByName("缺失店铺", false));
|
||||
@@ -47,22 +47,22 @@ class ZiniaoShopSwitchServiceTest {
|
||||
expected.setMatched(true);
|
||||
when(ziniaoShopIndexService.findIndexedStoreByName("测试店铺", false)).thenReturn(expected);
|
||||
ZiniaoShopSwitchService service = new ZiniaoShopSwitchService(
|
||||
ziniaoAuthService, ziniaoShopIndexService, shopManageService);
|
||||
ziniaoAuthService, ziniaoShopIndexService, managedShopNamePort);
|
||||
|
||||
ZiniaoShopMatchResultVo actual = service.findIndexedStoreByName(" 测试店铺 ", false);
|
||||
|
||||
assertSame(expected, actual);
|
||||
InOrder order = inOrder(shopManageService, ziniaoShopIndexService);
|
||||
order.verify(shopManageService).requireShopByName("测试店铺");
|
||||
InOrder order = inOrder(managedShopNamePort, ziniaoShopIndexService);
|
||||
order.verify(managedShopNamePort).requireShopByName("测试店铺");
|
||||
order.verify(ziniaoShopIndexService).findIndexedStoreByName("测试店铺", false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingManagedShopStopsDirectStaffMatch() {
|
||||
when(shopManageService.requireShopByName("缺失店铺"))
|
||||
.thenThrow(new BusinessException("后台店铺管理中未找到店铺:缺失店铺,请先添加店铺信息"));
|
||||
doThrow(new BusinessException("后台店铺管理中未找到店铺:缺失店铺,请先添加店铺信息"))
|
||||
.when(managedShopNamePort).requireShopByName("缺失店铺");
|
||||
ZiniaoShopSwitchService service = new ZiniaoShopSwitchService(
|
||||
ziniaoAuthService, ziniaoShopIndexService, shopManageService);
|
||||
ziniaoAuthService, ziniaoShopIndexService, managedShopNamePort);
|
||||
|
||||
assertThrows(BusinessException.class,
|
||||
() -> service.matchStoreByNameAcrossStaff("缺失店铺", 12L));
|
||||
@@ -72,12 +72,12 @@ class ZiniaoShopSwitchServiceTest {
|
||||
@Test
|
||||
void blankShopNameDoesNotQueryManagedShopOrZiniao() {
|
||||
ZiniaoShopSwitchService service = new ZiniaoShopSwitchService(
|
||||
ziniaoAuthService, ziniaoShopIndexService, shopManageService);
|
||||
ziniaoAuthService, ziniaoShopIndexService, managedShopNamePort);
|
||||
|
||||
ZiniaoShopMatchResultVo result = service.findIndexedStoreByName(" ", false);
|
||||
|
||||
assertEquals(ZiniaoShopIndexService.MATCH_STATUS_PENDING, result.getMatchStatus());
|
||||
assertEquals("店铺名为空,无法匹配索引", result.getMatchMessage());
|
||||
verifyNoInteractions(shopManageService, ziniaoShopIndexService, ziniaoAuthService);
|
||||
verifyNoInteractions(managedShopNamePort, ziniaoShopIndexService, ziniaoAuthService);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user