package com.zy.core.thread.impl.v5; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.zy.common.utils.RedisUtil; import com.zy.core.enums.RedisKeyType; import com.zy.core.model.command.StationCommand; import com.zy.core.service.StationTaskLoopService; import org.junit.jupiter.api.Test; import java.util.HashSet; import java.util.HashMap; import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; class StationV5RunBlockReroutePlannerTest { @Test void plan_prefersLongerCandidateWhenShortestPathIsOverusedInsideTriggeredLoop() { RedisUtil redisUtil = mock(RedisUtil.class); String stateKey = RedisKeyType.STATION_RUN_BLOCK_REROUTE_STATE_.key + "100_10"; when(redisUtil.get(stateKey)).thenReturn(JSON.toJSONString(seedState( 100, 10, 2, List.of(), Map.of("10->11->20", 2) ))); StationV5RunBlockReroutePlanner planner = new StationV5RunBlockReroutePlanner(redisUtil); StationCommand shortest = moveCommand(100, 10, 20, 10, 11, 20); StationCommand longer = moveCommand(100, 10, 20, 10, 31, 32, 20); StationTaskLoopService.LoopIdentitySnapshot loopIdentity = new StationTaskLoopService.LoopIdentitySnapshot("10|11|20", new HashSet<>(List.of(10, 11, 20)), 3, 3, "wholeLoop"); StationTaskLoopService.LoopEvaluation loopEvaluation = new StationTaskLoopService.LoopEvaluation(100, 10, loopIdentity, 2, 3, true); StationV5RunBlockReroutePlanner.PlanResult result = planner.plan( 100, 10, loopEvaluation, List.of(shortest, longer) ); assertSame(longer, result.getCommand()); assertEquals(3, result.getPlanCount()); } @Test void plan_resetsIssuedRoutesWhenAllCandidatesHaveBeenTried() { RedisUtil redisUtil = mock(RedisUtil.class); String stateKey = RedisKeyType.STATION_RUN_BLOCK_REROUTE_STATE_.key + "100_10"; when(redisUtil.get(stateKey)).thenReturn(JSON.toJSONString(seedState( 100, 10, 1, List.of(List.of(10, 11, 20), List.of(10, 31, 32, 20)), Map.of("10->11->20", 1, "10->31->32->20", 1) ))); StationV5RunBlockReroutePlanner planner = new StationV5RunBlockReroutePlanner(redisUtil); StationCommand first = moveCommand(100, 10, 20, 10, 11, 20); StationCommand second = moveCommand(100, 10, 20, 10, 31, 32, 20); StationTaskLoopService.LoopEvaluation loopEvaluation = new StationTaskLoopService.LoopEvaluation(100, 10, StationTaskLoopService.LoopIdentitySnapshot.empty(), 0, 0, false); StationV5RunBlockReroutePlanner.PlanResult result = planner.plan( 100, 10, loopEvaluation, List.of(first, second) ); assertSame(first, result.getCommand()); assertEquals(2, result.getPlanCount()); } private static StationCommand moveCommand(Integer taskNo, Integer stationId, Integer targetStationId, Integer... path) { StationCommand command = new StationCommand(); command.setTaskNo(taskNo); command.setStationId(stationId); command.setTargetStaNo(targetStationId); command.setNavigatePath(List.of(path)); return command; } private static JSONObject seedState(Integer taskNo, Integer blockStationId, Integer planCount, List> issuedRoutePathList, Map routeIssueCountMap) { JSONObject state = new JSONObject(); state.put("taskNo", taskNo); state.put("blockStationId", blockStationId); state.put("planCount", planCount); state.put("issuedRoutePathList", issuedRoutePathList); state.put("routeIssueCountMap", new HashMap<>(routeIssueCountMap)); return state; } }