移除超时的在线用户 优化一些代码风格,增加test校验

This commit is contained in:
Lyon 2021-03-09 20:23:36 +08:00
parent 492b27cb23
commit a9436d2931
3 changed files with 10 additions and 12 deletions

View File

@ -21,10 +21,7 @@ public class SysUserSessionTimeoutJob implements JobHandler {
@Override @Override
public String execute(String param) throws Exception { public String execute(String param) throws Exception {
log.info("[execute][执行任务:{}]", "移除超时的在线用户"); return String.valueOf(sysUserSessionService.clearSessionTimeout());
long timeoutCount = sysUserSessionService.clearSessionTimeout();
log.info("[execute][执行任务:{}:{}]", "移除超时的在线用户完成", timeoutCount);
return null;
} }
} }

View File

@ -64,8 +64,6 @@ public interface SysUserSessionService {
* 移除超时的在线用户 * 移除超时的在线用户
* *
* @return {@link Long } 移出的超时用户数量 * @return {@link Long } 移出的超时用户数量
* @author Lyon
* @date 2021/3/7
**/ **/
long clearSessionTimeout(); long clearSessionTimeout();
} }

View File

@ -11,6 +11,7 @@ import cn.iocoder.dashboard.modules.system.service.auth.impl.SysUserSessionServi
import cn.iocoder.dashboard.modules.system.service.dept.impl.SysDeptServiceImpl; import cn.iocoder.dashboard.modules.system.service.dept.impl.SysDeptServiceImpl;
import cn.iocoder.dashboard.modules.system.service.logger.impl.SysLoginLogServiceImpl; import cn.iocoder.dashboard.modules.system.service.logger.impl.SysLoginLogServiceImpl;
import cn.iocoder.dashboard.modules.system.service.user.SysUserServiceImpl; import cn.iocoder.dashboard.modules.system.service.user.SysUserServiceImpl;
import cn.iocoder.dashboard.util.AssertUtils;
import cn.iocoder.dashboard.util.RandomUtils; import cn.iocoder.dashboard.util.RandomUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
@ -31,9 +32,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* @version 1.0 * @version 1.0
* @since <pre>3月 8, 2021</pre> * @since <pre>3月 8, 2021</pre>
*/ */
@Import({ @Import(
SysUserSessionServiceImpl.class SysUserSessionServiceImpl.class)
})
public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest { public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest {
@Resource @Resource
@ -62,14 +62,17 @@ public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest {
.limit(expectedTimeoutCount) .limit(expectedTimeoutCount)
.map(i -> RandomUtils.randomPojo(SysUserSessionDO.class, o -> o.setSessionTimeout(DateUtil.offsetSecond(new Date(), -1)))) .map(i -> RandomUtils.randomPojo(SysUserSessionDO.class, o -> o.setSessionTimeout(DateUtil.offsetSecond(new Date(), -1))))
.collect(Collectors.toList()); .collect(Collectors.toList());
prepareData.add(RandomUtils.randomPojo(SysUserSessionDO.class, o -> o.setSessionTimeout(DateUtil.offsetMinute(new Date(), 30)))); SysUserSessionDO sessionDO = RandomUtils.randomPojo(SysUserSessionDO.class, o -> o.setSessionTimeout(DateUtil.offsetMinute(new Date(), 30)));
prepareData.add(sessionDO);
prepareData.forEach(sysUserSessionMapper::insert); prepareData.forEach(sysUserSessionMapper::insert);
//清空超时数据 //清空超时数据
long actualTimeoutCount = sysUserSessionService.clearSessionTimeout(); long actualTimeoutCount = sysUserSessionService.clearSessionTimeout();
//校验
assertEquals(expectedTimeoutCount, actualTimeoutCount); assertEquals(expectedTimeoutCount, actualTimeoutCount);
Integer actualTotal = sysUserSessionMapper.selectCount(new QueryWrapperX<>()); List<SysUserSessionDO> userSessionDOS = sysUserSessionMapper.selectList();
assertEquals(expectedTotal, actualTotal); assertEquals(expectedTotal, userSessionDOS.size());
AssertUtils.assertPojoEquals(sessionDO, userSessionDOS.get(0), "updateTime");
} }
} }