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()); } }