#
Junjie
2 天以前 90402710d962aa357062ecb94649e0f277c1dfb3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.zy.core.utils.station;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.zy.asrs.entity.BasStationOpt;
import com.zy.asrs.service.BasStationOptService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.model.command.StationCommand;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.util.Collections;
import java.util.Date;
import java.util.concurrent.atomic.AtomicReference;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
class StationDispatchRuntimeStateSupportTest {
 
    @Test
    void idleTrack_isTimeoutUsesFirstSeenTime() {
        StationTaskIdleTrack track = new StationTaskIdleTrack(100, 145, System.currentTimeMillis() - 11_000L);
 
        assertTrue(track.isTimeout(10));
    }
 
    @Test
    void touchIdleTrack_replacesTrackWhenStationChanges() {
        StationDispatchRuntimeStateSupport support = new StationDispatchRuntimeStateSupport();
        RedisUtil redisUtil = mock(RedisUtil.class);
        ReflectionTestUtils.setField(support, "redisUtil", redisUtil);
        when(redisUtil.get(RedisKeyType.STATION_TASK_IDLE_TRACK_.key + 100))
                .thenReturn(JSON.toJSONString(new StationTaskIdleTrack(100, 145, System.currentTimeMillis() - 3_000L)));
 
        StationTaskIdleTrack track = support.touchIdleTrack(100, 146);
 
        assertEquals(146, track.getStationId());
        verify(redisUtil).set(eq(RedisKeyType.STATION_TASK_IDLE_TRACK_.key + 100), org.mockito.ArgumentMatchers.any(String.class), eq(3600L));
    }
 
    @Test
    void hasRecentIssuedMoveCommand_returnsTrueForRecentMoveLog() {
        StationDispatchRuntimeStateSupport support = new StationDispatchRuntimeStateSupport();
        BasStationOptService basStationOptService = mock(BasStationOptService.class);
        ReflectionTestUtils.setField(support, "basStationOptService", basStationOptService);
        when(basStationOptService.list(org.mockito.ArgumentMatchers.<Wrapper<BasStationOpt>>any()))
                .thenReturn(Collections.singletonList(new BasStationOpt()));
 
        boolean result = support.hasRecentIssuedMoveCommand(100, 145, 10_000L);
 
        assertTrue(result);
    }
 
    @Test
    void clearIssuedMoveCommandsDuringIdleStay_marksSendZeroAndAppendsMemo() {
        StationDispatchRuntimeStateSupport support = new StationDispatchRuntimeStateSupport();
        BasStationOptService basStationOptService = mock(BasStationOptService.class);
        ReflectionTestUtils.setField(support, "basStationOptService", basStationOptService);
        BasStationOpt opt = new BasStationOpt();
        opt.setId(1L);
        opt.setSend(1);
        opt.setMemo("existing");
        opt.setSendTime(new Date());
        when(basStationOptService.list(org.mockito.ArgumentMatchers.<Wrapper<BasStationOpt>>any()))
                .thenReturn(Collections.singletonList(opt));
 
        int clearedCount = support.clearIssuedMoveCommandsDuringIdleStay(
                new StationTaskIdleTrack(100, 145, System.currentTimeMillis() - 10_000L),
                100,
                145
        );
 
        assertEquals(1, clearedCount);
        assertEquals(0, opt.getSend());
        assertTrue(opt.getMemo().contains("existing"));
        assertTrue(opt.getMemo().contains("idleRecoverRerouteCleared(stationId=145)"));
        assertFalse(opt.getUpdateTime() == null);
        verify(basStationOptService).updateBatchById(Collections.singletonList(opt));
    }
 
    @Test
    void tryAcquireOutOrderDispatchLock_returnsFalseWhenKeyAlreadyExists() {
        StationDispatchRuntimeStateSupport support = new StationDispatchRuntimeStateSupport();
        RedisUtil redisUtil = mock(RedisUtil.class);
        ReflectionTestUtils.setField(support, "redisUtil", redisUtil);
        when(redisUtil.get(RedisKeyType.STATION_OUT_ORDER_DISPATCH_LIMIT_.key + "100_145")).thenReturn("lock");
 
        boolean acquired = support.tryAcquireOutOrderDispatchLock(100, 145, 2);
 
        assertFalse(acquired);
    }
 
    @Test
    void signalSegmentReset_setsAndClearsResetKey() {
        StationDispatchRuntimeStateSupport support = new StationDispatchRuntimeStateSupport();
        RedisUtil redisUtil = mock(RedisUtil.class);
        ReflectionTestUtils.setField(support, "redisUtil", redisUtil);
 
        support.signalSegmentReset(100, 0L);
 
        verify(redisUtil).set(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + 100, "cancel", 3);
        verify(redisUtil).del(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + 100);
    }
 
    @Test
    void watchCircleCommand_roundTripsThroughRedis() {
        StationDispatchRuntimeStateSupport support = new StationDispatchRuntimeStateSupport();
        RedisUtil redisUtil = mock(RedisUtil.class);
        ReflectionTestUtils.setField(support, "redisUtil", redisUtil);
        AtomicReference<Object> storedValue = new AtomicReference<>();
        when(redisUtil.get(RedisKeyType.WATCH_CIRCLE_STATION_.key + 100))
                .thenAnswer(invocation -> storedValue.get());
        when(redisUtil.set(eq(RedisKeyType.WATCH_CIRCLE_STATION_.key + 100), org.mockito.ArgumentMatchers.any(String.class), eq(60L * 60 * 24)))
                .thenAnswer(invocation -> {
                    storedValue.set(invocation.getArgument(1));
                    return true;
                });
 
        StationCommand command = new StationCommand();
        command.setTaskNo(100);
        command.setTargetStaNo(145);
 
        support.saveWatchCircleCommand(100, command);
        StationCommand loaded = support.loadWatchCircleCommand(100);
 
        assertNotNull(loaded);
        assertEquals(145, loaded.getTargetStaNo());
        support.clearWatchCircleCommand(100);
        verify(redisUtil).del(RedisKeyType.WATCH_CIRCLE_STATION_.key + 100);
    }
}