feat: 悬挂学习会话惰性收敛与定时兜底
This commit is contained in:
+70
@@ -1,6 +1,7 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.guo.learningprogresstracker.common.StudyTimePolicy;
|
||||
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
|
||||
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
@@ -19,9 +20,11 @@ import org.mockito.Spy;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -55,6 +58,10 @@ class StudySessionsServiceImplTest {
|
||||
@Mock
|
||||
private AiServiceClient aiServiceClient;
|
||||
|
||||
/** 时间口径用真实默认值,避免测试里出现 null 策略;需要改口径时换成自建实例即可 */
|
||||
@Spy
|
||||
private StudyTimePolicy studyTimePolicy = StudyTimePolicy.DEFAULT;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// stub updateById,避免调用真实的 getBaseMapper()
|
||||
@@ -63,6 +70,8 @@ class StudySessionsServiceImplTest {
|
||||
doReturn(true).when(studySessionsService).update(any(StudySessionsEntity.class), any(Wrapper.class));
|
||||
// stub removeById,避免调用真实的 getBaseMapper()
|
||||
doReturn(true).when(studySessionsService).removeById(any(Serializable.class));
|
||||
// 收敛路径直接使用 ServiceImpl 的 baseMapper 字段,必须显式注入(Mockito 不会填父类字段)
|
||||
ReflectionTestUtils.setField(studySessionsService, "baseMapper", studySessionsMapper);
|
||||
}
|
||||
|
||||
private StudySessionsEntity createSession(String state, int minutesAgo) {
|
||||
@@ -411,4 +420,65 @@ class StudySessionsServiceImplTest {
|
||||
assertNull(message);
|
||||
verify(studyReportsMapper, never()).insert(any(StudyReportsEntity.class));
|
||||
}
|
||||
|
||||
// ============ 会话-04:悬挂会话收敛 ============
|
||||
|
||||
@Test
|
||||
void convergeStaleSessionsOfCurrentUser_staleOngoing_shouldPauseTruncateAndMark() {
|
||||
StudySessionsEntity stale = createSession(StudySessionStateEnum.ONGOING.name(), 180);
|
||||
doReturn(List.of(stale)).when(studySessionsService).list(any(Wrapper.class));
|
||||
when(studySessionsMapper.convergeStaleSession(any())).thenReturn(1);
|
||||
|
||||
int converged = studySessionsService.convergeStaleSessionsOfCurrentUser();
|
||||
|
||||
assertEquals(1, converged);
|
||||
assertEquals(StudySessionStateEnum.PAUSED.name(), stale.getSessionState());
|
||||
assertEquals(25 * 60, stale.getEffectiveTime(), "悬挂 3 小时只应计入一个番茄钟");
|
||||
assertEquals(Boolean.TRUE, stale.getAutoConverged());
|
||||
}
|
||||
|
||||
@Test
|
||||
void convergeStaleSessionsOfCurrentUser_freshOngoing_shouldNotWrite() {
|
||||
StudySessionsEntity fresh = createSession(StudySessionStateEnum.ONGOING.name(), 10);
|
||||
doReturn(List.of(fresh)).when(studySessionsService).list(any(Wrapper.class));
|
||||
|
||||
int converged = studySessionsService.convergeStaleSessionsOfCurrentUser();
|
||||
|
||||
assertEquals(0, converged, "未超过硬兜底阈值不应收敛");
|
||||
assertEquals(StudySessionStateEnum.ONGOING.name(), fresh.getSessionState());
|
||||
verify(studySessionsMapper, never()).convergeStaleSession(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void convergeStaleSessionsOfCurrentUser_concurrentClaimLost_shouldCountZero() {
|
||||
StudySessionsEntity stale = createSession(StudySessionStateEnum.ONGOING.name(), 180);
|
||||
doReturn(List.of(stale)).when(studySessionsService).list(any(Wrapper.class));
|
||||
// 条件更新未命中:说明已被其他请求收敛
|
||||
when(studySessionsMapper.convergeStaleSession(any())).thenReturn(0);
|
||||
|
||||
assertEquals(0, studySessionsService.convergeStaleSessionsOfCurrentUser());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanAndConvergeStaleSessions_shouldUseCrossTenantScan() {
|
||||
StudySessionsEntity stale = createSession(StudySessionStateEnum.ONGOING.name(), 200);
|
||||
when(studySessionsMapper.selectForcePausableSessions(any(), anyInt())).thenReturn(List.of(stale));
|
||||
when(studySessionsMapper.convergeStaleSession(any())).thenReturn(1);
|
||||
|
||||
int converged = studySessionsService.scanAndConvergeStaleSessions();
|
||||
|
||||
assertEquals(1, converged);
|
||||
assertEquals(Boolean.TRUE, stale.getAutoConverged());
|
||||
verify(studySessionsMapper).selectForcePausableSessions(any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getActiveSession_shouldRunLazyConvergenceFirst() {
|
||||
when(studySessionsMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
assertNull(studySessionsService.getActiveSession(null));
|
||||
|
||||
// 惰性收敛先按状态 + last_start_time 查一次悬挂会话
|
||||
verify(studySessionsMapper, atLeastOnce()).selectList(any());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user