#
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
package com.zy.core.thread.impl;
 
import com.zy.asrs.entity.DeviceConfig;
import com.zy.common.utils.RedisUtil;
import com.zy.core.cache.MessageQueue;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.Task;
import com.zy.core.model.command.StationCommand;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.model.protocol.StationTaskBufferItem;
import com.zy.core.network.ZyStationConnectDriver;
import com.zy.core.thread.impl.v5.StationV5SegmentExecutor;
import com.zy.core.thread.impl.v5.StationV5StatusReader;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
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.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
class ZyStationV5ThreadTest {
 
    @Test
    void pollAndDispatchQueuedCommand_submitsQueuedMoveCommandToSegmentExecutor() {
        DeviceConfig deviceConfig = new DeviceConfig();
        deviceConfig.setDeviceNo(1);
        RedisUtil redisUtil = mock(RedisUtil.class);
        StationCommand command = new StationCommand();
        StationV5SegmentExecutor segmentExecutor = mock(StationV5SegmentExecutor.class);
 
        ZyStationV5Thread thread = new ZyStationV5Thread(deviceConfig, redisUtil);
        ReflectionTestUtils.setField(thread, "segmentExecutor", segmentExecutor);
 
        MessageQueue.init(SlaveType.Devp, 1);
        try {
            MessageQueue.offer(SlaveType.Devp, 1, new Task(2, command));
 
            ReflectionTestUtils.invokeMethod(thread, "pollAndDispatchQueuedCommand");
 
            verify(segmentExecutor, timeout(1000)).execute(command);
        } finally {
            MessageQueue.clear(SlaveType.Devp, 1);
            thread.close();
        }
    }
 
    @Test
    void clearPath_delegatesPureSlotClearingToDriver() {
        DeviceConfig deviceConfig = new DeviceConfig();
        deviceConfig.setDeviceNo(1);
        RedisUtil redisUtil = mock(RedisUtil.class);
        ZyStationConnectDriver connectDriver = mock(ZyStationConnectDriver.class);
        when(connectDriver.clearTaskBufferSlot(10, 2)).thenReturn(true);
 
        ZyStationV5Thread thread = new ZyStationV5Thread(deviceConfig, redisUtil);
        ReflectionTestUtils.setField(thread, "zyStationConnectDriver", connectDriver);
 
        StationTaskBufferItem hitItem = new StationTaskBufferItem();
        hitItem.setSlotIdx(2);
        hitItem.setTaskNo(100);
        hitItem.setTargetStaNo(88);
 
        StationProtocol station20 = new StationProtocol();
        station20.setStationId(20);
        station20.setTaskBufferItems(Collections.emptyList());
 
        StationProtocol station10 = new StationProtocol();
        station10.setStationId(10);
        station10.setTaskBufferItems(List.of(hitItem));
 
        StationV5StatusReader statusReader = (StationV5StatusReader) ReflectionTestUtils.getField(thread, "statusReader");
        ReflectionTestUtils.setField(statusReader, "statusList", Arrays.asList(station20, station10));
 
        boolean result = thread.clearPath(100);
 
        assertTrue(result);
        verify(connectDriver).clearTaskBufferSlot(eq(10), eq(2));
        verify(redisUtil, never()).set(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.anyLong());
        verify(redisUtil, never()).del(org.mockito.ArgumentMatchers.any(String[].class));
        assertEquals(0, hitItem.getTaskNo());
        assertEquals(0, hitItem.getTargetStaNo());
    }
}