| File was renamed from ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TTestLiveServiceImplTest.java |
| | |
| | | package com.ruoyi.system.service.impl; |
| | | package com.battery.business.service.impl; |
| | | |
| | | import static org.junit.jupiter.api.Assertions.assertFalse; |
| | | import static org.junit.jupiter.api.Assertions.assertEquals; |
| | |
| | | import org.junit.jupiter.api.Test; |
| | | import org.springframework.web.context.request.async.AsyncRequestNotUsableException; |
| | | import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| | | import com.battery.device.simulator.BtsDevelopmentSimulator; |
| | | import com.battery.device.discovery.DeviceDiscoveryCandidateStore; |
| | | import com.battery.device.model.ChannelIdentity; |
| | | import com.battery.device.model.DeviceWorkStatus; |
| | | import com.battery.device.model.RealtimeSnapshot; |
| | | import com.ruoyi.system.domain.TTest; |
| | | import com.ruoyi.system.domain.TTestBattery; |
| | | import com.ruoyi.system.domain.TTestChannelBinding; |
| | | import com.ruoyi.system.domain.TTestWarningException; |
| | | import com.ruoyi.system.mapper.TTestMapper; |
| | | import com.ruoyi.system.service.ITTestService; |
| | | import com.ruoyi.system.service.ITestLiveService.LiveState; |
| | | import com.battery.device.api.TestDataObservationAdapter; |
| | | import com.battery.business.web.TestLiveStream; |
| | | import com.battery.business.service.ITestLiveService; |
| | | import com.battery.device.api.discovery.DeviceDiscoveryCandidateStore; |
| | | import com.battery.device.api.model.ChannelIdentity; |
| | | import com.battery.device.api.model.DeviceWorkStatus; |
| | | import com.battery.device.api.model.RealtimeSnapshot; |
| | | import com.battery.business.domain.TTest; |
| | | import com.battery.business.domain.TTestBattery; |
| | | import com.battery.business.domain.TTestChannelBinding; |
| | | import com.battery.business.domain.TTestWarningException; |
| | | import com.battery.business.mapper.TTestMapper; |
| | | import com.battery.business.service.ITTestService; |
| | | import com.battery.business.service.ITestLiveService.LiveState; |
| | | |
| | | /** 实时流客户端断连时应结束连接而不触发全局异常处理。 */ |
| | | class TTestLiveServiceImplTest |
| | |
| | | DeviceWorkStatus workStatus, TTestMapper mapper) |
| | | { |
| | | DeviceDiscoveryCandidateStore candidates = mock(DeviceDiscoveryCandidateStore.class); |
| | | BtsDevelopmentSimulator simulator = mock(BtsDevelopmentSimulator.class); |
| | | TestDataObservationAdapter simulator = mock(TestDataObservationAdapter.class); |
| | | ITTestService tests = mock(ITTestService.class); |
| | | when(tests.selectTestById(10L)).thenReturn(test); |
| | | ChannelIdentity identity = new ChannelIdentity("dev-sim", 1, 1, 0, 1); |
| | |
| | | List.of(channel), now, now.plusSeconds(60)); |
| | | when(candidates.snapshot(any(Instant.class))).thenReturn( |
| | | new DeviceDiscoveryCandidateStore.CandidateSnapshot(List.of(cabinet), List.of())); |
| | | var plan = new BtsDevelopmentSimulator.BtsTestSnapshot(1, 1, 1, List.of()); |
| | | when(simulator.btsTest(anyInt(), anyInt())).thenReturn(plan); |
| | | when(simulator.btsTest(anyInt(), anyInt(), anyInt())).thenReturn(plan); |
| | | var plan = new TestDataObservationAdapter.TestPlan(1, 1, 1, List.of()); |
| | | when(simulator.testPlan(any(ChannelIdentity.class))).thenReturn(plan); |
| | | |
| | | return new CurrentLiveService(tests, candidates, simulator, mapper); |
| | | } |
| | | |
| | |
| | | void initialWriteFailureShouldCompleteStreamWithoutErrorDispatch() |
| | | { |
| | | RecordingEmitter emitter = new RecordingEmitter(0); |
| | | TTestLiveServiceImpl service = new TestableLiveService(emitter); |
| | | TestLiveStream service = new TestableLiveService(emitter); |
| | | |
| | | try |
| | | { |
| | |
| | | } |
| | | finally |
| | | { |
| | | service.shutdown(); |
| | | service.close(); |
| | | } |
| | | |
| | | assertTrue(emitter.completed); |
| | |
| | | void scheduledWriteFailureShouldCompleteStreamWithoutErrorDispatch() |
| | | { |
| | | RecordingEmitter emitter = new RecordingEmitter(6); |
| | | TTestLiveServiceImpl service = new TestableLiveService(emitter); |
| | | TestLiveStream service = new TestableLiveService(emitter); |
| | | |
| | | try |
| | | { |
| | |
| | | } |
| | | finally |
| | | { |
| | | service.shutdown(); |
| | | service.close(); |
| | | } |
| | | |
| | | assertFalse(emitter.completedWithError); |
| | | } |
| | | |
| | | private static final class TestableLiveService extends TTestLiveServiceImpl |
| | | private static final class TestableLiveService extends TestLiveStream |
| | | { |
| | | private final RecordingEmitter emitter; |
| | | |
| | | private TestableLiveService(RecordingEmitter emitter) |
| | | { |
| | | super(mock(ITTestService.class), mock(DeviceDiscoveryCandidateStore.class), |
| | | mock(BtsDevelopmentSimulator.class)); |
| | | super(testLive()); |
| | | this.emitter = emitter; |
| | | } |
| | | |
| | |
| | | return emitter; |
| | | } |
| | | |
| | | @Override |
| | | public LiveState current(Long testId) |
| | | { |
| | | return new LiveState(testId, "RUNNING", "RECEIVING", Instant.now(), List.of(), List.of()); |
| | | private static ITestLiveService testLive() { |
| | | ITestLiveService live = mock(ITestLiveService.class); |
| | | when(live.current(any())).thenReturn(new LiveState(4L, "RUNNING", "RECEIVING", Instant.now(), List.of(), List.of())); |
| | | return live; |
| | | } |
| | | } |
| | | |
| | | private static final class CurrentLiveService extends TTestLiveServiceImpl |
| | | { |
| | | private CurrentLiveService(ITTestService tests, DeviceDiscoveryCandidateStore candidates, |
| | | BtsDevelopmentSimulator simulator, TTestMapper mapper) |
| | | TestDataObservationAdapter simulator, TTestMapper mapper) |
| | | { |
| | | super(tests, candidates, simulator, mapper); |
| | | } |