yeg
2026-08-30 30264a7c67d44989eb9f1de1f01f0081bd298e6c
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
package com.ruoyi.system.service.impl;
 
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
 
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import org.awaitility.Awaitility;
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.ruoyi.device.bts.BtsDevelopmentSimulator;
import com.ruoyi.device.discovery.DeviceDiscoveryCandidateStore;
import com.ruoyi.system.mapper.TTestMapper;
import com.ruoyi.system.service.ITTestService;
import com.ruoyi.system.service.ITestLiveService.LiveState;
 
/** 实时流客户端断连时应结束连接而不触发全局异常处理。 */
class TTestLiveServiceImplTest
{
    @Test
    void initialWriteFailureShouldCompleteStreamWithoutErrorDispatch()
    {
        RecordingEmitter emitter = new RecordingEmitter(0);
        TTestLiveServiceImpl service = new TestableLiveService(emitter);
 
        try
        {
            service.stream(4L);
        }
        finally
        {
            service.shutdown();
        }
 
        assertTrue(emitter.completed);
        assertFalse(emitter.completedWithError);
    }
 
    @Test
    void scheduledWriteFailureShouldCompleteStreamWithoutErrorDispatch()
    {
        RecordingEmitter emitter = new RecordingEmitter(6);
        TTestLiveServiceImpl service = new TestableLiveService(emitter);
 
        try
        {
            service.stream(4L);
            Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> emitter.completed);
        }
        finally
        {
            service.shutdown();
        }
 
        assertFalse(emitter.completedWithError);
    }
 
    private static final class TestableLiveService extends TTestLiveServiceImpl
    {
        private final RecordingEmitter emitter;
 
        private TestableLiveService(RecordingEmitter emitter)
        {
            super(mock(ITTestService.class), mock(DeviceDiscoveryCandidateStore.class),
                    mock(BtsDevelopmentSimulator.class), mock(TTestMapper.class));
            this.emitter = emitter;
        }
 
        @Override
        protected SseEmitter createEmitter()
        {
            return emitter;
        }
 
        @Override
        public LiveState current(Long testId)
        {
            return new LiveState(testId, "RUNNING", "RECEIVING", Instant.now(), List.of(), List.of());
        }
    }
 
    private static final class RecordingEmitter extends SseEmitter
    {
        private final int successfulWrites;
        private int writes;
        private volatile boolean completed;
        private volatile boolean completedWithError;
 
        private RecordingEmitter(int successfulWrites)
        {
            this.successfulWrites = successfulWrites;
        }
 
        @Override
        public void send(SseEventBuilder builder) throws IOException
        {
            if (writes++ >= successfulWrites)
                throw new AsyncRequestNotUsableException("client disconnected", new IOException("connection closed"));
        }
 
        @Override
        public synchronized void complete()
        {
            completed = true;
        }
 
        @Override
        public synchronized void completeWithError(Throwable error)
        {
            completedWithError = true;
        }
    }
}