feat(上架): 店铺互斥改为按(设备,店铺),同店可在不同客户端并行跑不同国家
背景:紫鸟浏览器会话是每台客户端一份,同一家店可以在两台机器上并行; 国家是会话内的可切换状态(SwitchingCountries,断线重连还会再切), 所以真正必须串行的只有"同一台设备上的同一家店"。原来的全局按店铺互斥 把跨机器的合法并行也挡了(2026-09-18 任务 28624 被 28616 误挡)。 - V130:biz_publish_file 增加 device_id 列 - AdminAuthSupport.currentDeviceId():只读 JWT 签名的 deviceId claim, 不信任客户端可控的 X-Device-Id 请求头 - activateFile 互斥键改为 (device_id, shop_name);device 为空(旧客户端 token 无该 claim / 内部令牌调用)时退回全局店铺互斥,保守不放宽; 存量 RUNNING 行的 NULL device 视为"来源不明",同样保守拦到跑完为止 - 前端与客户端无需改动:页面 axios 对 /newApi 已带 Authorization Bearer
This commit is contained in:
+98
-8
@@ -37,6 +37,7 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
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;
|
||||
@@ -109,6 +110,10 @@ class PublishTaskServiceTest {
|
||||
|
||||
@InjectMocks private PublishTaskService service;
|
||||
|
||||
/** 店铺互斥按 (设备, 店铺) 判定:两台机器各自的设备标识。 */
|
||||
private static final String DEVICE_A = "device-aaaa";
|
||||
private static final String DEVICE_B = "device-bbbb";
|
||||
|
||||
private final List<TaskChunkEntity> storedChunks = new ArrayList<>();
|
||||
private final List<TaskScopeStateEntity> storedScopes = new ArrayList<>();
|
||||
private final Map<String, String> rustfsPayloads = new LinkedHashMap<>();
|
||||
@@ -744,7 +749,7 @@ class PublishTaskServiceTest {
|
||||
when(publishFileMapper.selectOne(any())).thenReturn(running);
|
||||
|
||||
BusinessException error = assertThrows(BusinessException.class,
|
||||
() -> service.activateFile(taskId, fileId, 7L));
|
||||
() -> service.activateFile(taskId, fileId, 7L, DEVICE_A));
|
||||
|
||||
assertTrue(error.getMessage().contains("已有文件正在执行"));
|
||||
verify(publishFileMapper, never()).update(isNull(), any());
|
||||
@@ -752,25 +757,26 @@ class PublishTaskServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void activateFileRejectsShopAlreadyRunningInAnotherTask() {
|
||||
void activateFileRejectsShopAlreadyRunningOnSameDevice() {
|
||||
long taskId = 106L;
|
||||
long fileId = 206L;
|
||||
FileTaskEntity task = task(taskId, 7L, "RUNNING");
|
||||
PublishFileEntity target = file(taskId, fileId, "PENDING", "林洪武.xlsx");
|
||||
target.setShopName("林洪武");
|
||||
// 另一个任务(28520)正跑同一店铺 —— 2026-09-17 事故形态
|
||||
// 同一台设备上另一个任务(28520)正跑同一店铺 —— 2026-09-17 事故形态
|
||||
PublishFileEntity otherTaskRunning = file(105L, 205L, "RUNNING", "林洪武.xlsx");
|
||||
otherTaskRunning.setShopName("林洪武");
|
||||
otherTaskRunning.setDeviceId(DEVICE_A);
|
||||
TaskDistributedLockService.LockHandle lock = mock(TaskDistributedLockService.LockHandle.class);
|
||||
|
||||
when(taskDistributedLockService.acquire(PublishTaskService.MODULE_TYPE, taskId)).thenReturn(lock);
|
||||
when(fileTaskMapper.selectById(taskId)).thenReturn(task);
|
||||
when(publishFileMapper.selectById(fileId)).thenReturn(target);
|
||||
// 第一次 selectOne:同任务其它 RUNNING 文件(无);第二次:同店铺跨任务(有)
|
||||
// 第一次 selectOne:同任务其它 RUNNING 文件(无);第二次:同设备同店铺跨任务(有)
|
||||
when(publishFileMapper.selectOne(any())).thenReturn(null, otherTaskRunning);
|
||||
|
||||
BusinessException error = assertThrows(BusinessException.class,
|
||||
() -> service.activateFile(taskId, fileId, 7L));
|
||||
() -> service.activateFile(taskId, fileId, 7L, DEVICE_A));
|
||||
|
||||
assertTrue(error.getMessage().contains("林洪武"), "提示要带店铺名: " + error.getMessage());
|
||||
assertTrue(error.getMessage().contains("105"), "提示要带占用中的任务号: " + error.getMessage());
|
||||
@@ -778,6 +784,90 @@ class PublishTaskServiceTest {
|
||||
verify(lock).close();
|
||||
}
|
||||
|
||||
/** 用户需求:同一家店允许在不同客户端(不同机器)上并行跑不同国家。 */
|
||||
@Test
|
||||
void activateFileAllowsShopRunningOnAnotherDevice() {
|
||||
long taskId = 109L;
|
||||
long fileId = 209L;
|
||||
FileTaskEntity task = task(taskId, 7L, "RUNNING");
|
||||
PublishFileEntity target = file(taskId, fileId, "PENDING", "林芳.xlsx");
|
||||
target.setShopName("林芳");
|
||||
TaskDistributedLockService.LockHandle lock = mock(TaskDistributedLockService.LockHandle.class);
|
||||
|
||||
when(taskDistributedLockService.acquire(PublishTaskService.MODULE_TYPE, taskId)).thenReturn(lock);
|
||||
when(fileTaskMapper.selectById(taskId)).thenReturn(task);
|
||||
when(publishFileMapper.selectById(fileId)).thenReturn(target);
|
||||
// 另一台设备的占用行不会命中本设备的查询,因此这里返回 null
|
||||
when(publishFileMapper.selectOne(any())).thenReturn(null);
|
||||
when(publishFileMapper.update(isNull(), any())).thenReturn(1);
|
||||
|
||||
service.activateFile(taskId, fileId, 7L, DEVICE_B);
|
||||
|
||||
verify(publishFileMapper).update(isNull(), any());
|
||||
// 店铺互斥查询必须真正带上设备维度,否则跨机器并行会被重新挡住
|
||||
ArgumentCaptor<LambdaQueryWrapper> captor = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||
verify(publishFileMapper, times(2)).selectOne(captor.capture());
|
||||
String shopSql = captor.getAllValues().get(1).getSqlSegment();
|
||||
assertTrue(shopSql.contains("device_id"), "店铺互斥查询要带设备维度: " + shopSql);
|
||||
assertTrue(shopSql.contains("shop_name"), "设备维度不能替代店铺维度: " + shopSql);
|
||||
}
|
||||
|
||||
/** 存量行(改动前激活、device_id 为空)无法判断落在哪台机器上,一律保守视为可能同机。 */
|
||||
@Test
|
||||
void activateFileRejectsWhenRunningRowHasUnknownDevice() {
|
||||
long taskId = 110L;
|
||||
long fileId = 210L;
|
||||
FileTaskEntity task = task(taskId, 7L, "RUNNING");
|
||||
PublishFileEntity target = file(taskId, fileId, "PENDING", "林芳.xlsx");
|
||||
target.setShopName("林芳");
|
||||
PublishFileEntity legacyRunning = file(105L, 205L, "RUNNING", "林芳.xlsx");
|
||||
legacyRunning.setShopName("林芳");
|
||||
legacyRunning.setDeviceId(null);
|
||||
TaskDistributedLockService.LockHandle lock = mock(TaskDistributedLockService.LockHandle.class);
|
||||
|
||||
when(taskDistributedLockService.acquire(PublishTaskService.MODULE_TYPE, taskId)).thenReturn(lock);
|
||||
when(fileTaskMapper.selectById(taskId)).thenReturn(task);
|
||||
when(publishFileMapper.selectById(fileId)).thenReturn(target);
|
||||
when(publishFileMapper.selectOne(any())).thenReturn(null, legacyRunning);
|
||||
|
||||
BusinessException error = assertThrows(BusinessException.class,
|
||||
() -> service.activateFile(taskId, fileId, 7L, DEVICE_B));
|
||||
|
||||
assertTrue(error.getMessage().contains("105"), "存量未知设备行必须继续拦住: " + error.getMessage());
|
||||
verify(publishFileMapper, never()).update(isNull(), any());
|
||||
verify(lock).close();
|
||||
}
|
||||
|
||||
/** 无设备标识(旧客户端 token 无 deviceId claim / 内部令牌调用)→ 退回改动前的全局店铺互斥。 */
|
||||
@Test
|
||||
void activateFileFallsBackToGlobalShopCheckWhenDeviceMissing() {
|
||||
long taskId = 111L;
|
||||
long fileId = 211L;
|
||||
FileTaskEntity task = task(taskId, 7L, "RUNNING");
|
||||
PublishFileEntity target = file(taskId, fileId, "PENDING", "林芳.xlsx");
|
||||
target.setShopName("林芳");
|
||||
PublishFileEntity otherDeviceRunning = file(105L, 205L, "RUNNING", "林芳.xlsx");
|
||||
otherDeviceRunning.setShopName("林芳");
|
||||
otherDeviceRunning.setDeviceId(DEVICE_A);
|
||||
TaskDistributedLockService.LockHandle lock = mock(TaskDistributedLockService.LockHandle.class);
|
||||
|
||||
when(taskDistributedLockService.acquire(PublishTaskService.MODULE_TYPE, taskId)).thenReturn(lock);
|
||||
when(fileTaskMapper.selectById(taskId)).thenReturn(task);
|
||||
when(publishFileMapper.selectById(fileId)).thenReturn(target);
|
||||
when(publishFileMapper.selectOne(any())).thenReturn(null, otherDeviceRunning);
|
||||
|
||||
BusinessException error = assertThrows(BusinessException.class,
|
||||
() -> service.activateFile(taskId, fileId, 7L, ""));
|
||||
|
||||
assertTrue(error.getMessage().contains("105"), "来源不明时必须保持全局互斥: " + error.getMessage());
|
||||
ArgumentCaptor<LambdaQueryWrapper> captor = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||
verify(publishFileMapper, times(2)).selectOne(captor.capture());
|
||||
assertFalse(captor.getAllValues().get(1).getSqlSegment().contains("device_id"),
|
||||
"来源不明时不应按设备收窄,否则等于放宽互斥");
|
||||
verify(publishFileMapper, never()).update(isNull(), any());
|
||||
verify(lock).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void activateFileAllowsWhenShopHasNoOtherRunningTask() {
|
||||
long taskId = 107L;
|
||||
@@ -793,7 +883,7 @@ class PublishTaskServiceTest {
|
||||
when(publishFileMapper.selectOne(any())).thenReturn(null);
|
||||
when(publishFileMapper.update(isNull(), any())).thenReturn(1);
|
||||
|
||||
service.activateFile(taskId, fileId, 7L);
|
||||
service.activateFile(taskId, fileId, 7L, DEVICE_A);
|
||||
|
||||
verify(publishFileMapper).update(isNull(), any());
|
||||
}
|
||||
@@ -813,7 +903,7 @@ class PublishTaskServiceTest {
|
||||
when(publishFileMapper.selectOne(any())).thenReturn(null);
|
||||
when(publishFileMapper.update(isNull(), any())).thenReturn(1);
|
||||
|
||||
service.activateFile(taskId, fileId, 7L);
|
||||
service.activateFile(taskId, fileId, 7L, DEVICE_A);
|
||||
|
||||
verify(publishFileMapper, times(1)).selectOne(any());
|
||||
}
|
||||
@@ -830,7 +920,7 @@ class PublishTaskServiceTest {
|
||||
when(fileTaskMapper.selectById(taskId)).thenReturn(task);
|
||||
when(publishFileMapper.selectById(fileId)).thenReturn(running);
|
||||
|
||||
service.activateFile(taskId, fileId, 7L);
|
||||
service.activateFile(taskId, fileId, 7L, DEVICE_A);
|
||||
|
||||
verify(publishFileMapper, never()).selectOne(any());
|
||||
verify(publishFileMapper, never()).update(isNull(), any());
|
||||
|
||||
+3
-1
@@ -107,7 +107,9 @@ class ResultSuccessTimingContractTest {
|
||||
doAnswer(invocation -> null)
|
||||
.when(publishTaskService).submitResult(eq(TASK_ID), any(PublishSubmitResultRequest.class));
|
||||
|
||||
ApiResponse<Void> response = new PublishController(publishTaskService)
|
||||
// 第二个构造参数是 AdminAuthSupport(activateFile 取设备号用),本用例只测 submitResult,
|
||||
// 不经过该依赖,故传 null(同文件其它 controller 用例亦有传 null 的先例)。
|
||||
ApiResponse<Void> response = new PublishController(publishTaskService, null)
|
||||
.submitResult(TASK_ID, new PublishSubmitResultRequest());
|
||||
|
||||
assertTrue(response.isSuccess(), "success=true");
|
||||
|
||||
Reference in New Issue
Block a user