#
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
package com.zy.core.dispatch;
 
import com.zy.common.utils.RedisUtil;
import com.zy.core.cache.MessageQueue;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.StationCommandType;
import com.zy.core.model.command.StationCommand;
import com.zy.core.move.StationMoveCoordinator;
import org.junit.jupiter.api.Test;
 
import java.util.HashMap;
import java.util.Map;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
class StationCommandDispatcherTest {
 
    @Test
    void dispatch_acceptsMoveCommandAndReturnsQueueDepth() {
        StationCommandDispatcher dispatcher = new StationCommandDispatcher();
 
        StationCommand command = new StationCommand();
        command.setCommandType(StationCommandType.MOVE);
        command.setTaskNo(100);
        command.setStationId(10);
        command.setTargetStaNo(20);
 
        MessageQueue.init(SlaveType.Devp, 1);
        try {
            StationCommandDispatchResult result = dispatcher.dispatch(1, command, "unit-test", "move");
 
            assertTrue(result.isAccepted());
            assertEquals("accepted", result.getReason());
            assertEquals(1, result.getQueueDepth());
            assertEquals("unit-test", result.getSource());
            assertEquals("move", result.getScene());
        } finally {
            MessageQueue.clear(SlaveType.Devp, 1);
        }
    }
 
    @Test
    void dispatch_suppressesDuplicateMoveCommandWithinDedupWindow() {
        RedisUtil redisUtil = mock(RedisUtil.class);
        StationMoveCoordinator coordinator = mock(StationMoveCoordinator.class);
        StationCommandDispatcher dispatcher = new StationCommandDispatcher(redisUtil, coordinator);
        Map<String, Object> dedupStore = new HashMap<>();
 
        when(redisUtil.get(anyString())).thenAnswer(invocation -> dedupStore.get(invocation.getArgument(0)));
        when(redisUtil.set(anyString(), eq("lock"), anyLong())).thenAnswer(invocation -> {
            dedupStore.put(invocation.getArgument(0), invocation.getArgument(1));
            return true;
        });
        when(coordinator.buildPathSignatureHash(org.mockito.ArgumentMatchers.any(StationCommand.class)))
                .thenReturn("same-path");
 
        StationCommand command = new StationCommand();
        command.setCommandType(StationCommandType.MOVE);
        command.setTaskNo(100);
        command.setStationId(10);
        command.setTargetStaNo(20);
 
        MessageQueue.init(SlaveType.Devp, 1);
        try {
            StationCommandDispatchResult first = dispatcher.dispatch(1, command, "unit-test", "move");
            StationCommandDispatchResult second = dispatcher.dispatch(1, command, "unit-test", "move");
 
            assertTrue(first.isAccepted());
            assertFalse(second.isAccepted());
            assertEquals("dedup-suppressed", second.getReason());
            assertEquals(1, second.getQueueDepth());
        } finally {
            MessageQueue.clear(SlaveType.Devp, 1);
        }
    }
 
    @Test
    void dispatch_rejectsWhenDevpQueueIsNotInitialized() {
        StationCommandDispatcher dispatcher = new StationCommandDispatcher();
 
        StationCommand command = new StationCommand();
        command.setCommandType(StationCommandType.MOVE);
        command.setTaskNo(100);
        command.setStationId(10);
        command.setTargetStaNo(20);
 
        StationCommandDispatchResult result = dispatcher.dispatch(999, command, "unit-test", "move");
 
        assertFalse(result.isAccepted());
        assertEquals("queue-not-initialized", result.getReason());
        assertEquals(0, result.getQueueDepth());
    }
}