mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
test: DailyTimeIntervalStrategyHandlerTest
This commit is contained in:
parent
e01770adc7
commit
369ebdab0b
@ -30,7 +30,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
public class DailyTimeIntervalStrategyHandler implements TimingStrategyHandler {
|
public class DailyTimeIntervalStrategyHandler implements TimingStrategyHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用中国人的星期!!!
|
* 使用中国星期!!!
|
||||||
*/
|
*/
|
||||||
private static final Set<Integer> ALL_DAY = Sets.newHashSet(1, 2, 3, 4, 5, 6, 7);
|
private static final Set<Integer> ALL_DAY = Sets.newHashSet(1, 2, 3, 4, 5, 6, 7);
|
||||||
|
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
package tech.powerjob.server.core.scheduler.auxiliary.impl;
|
package tech.powerjob.server.core.scheduler.auxiliary.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import tech.powerjob.common.OmsConstant;
|
||||||
|
import tech.powerjob.common.exception.PowerJobException;
|
||||||
|
import tech.powerjob.common.serialize.JsonUtils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DailyTimeIntervalStrategyHandler
|
* DailyTimeIntervalStrategyHandler
|
||||||
* @author 550w
|
* @author 550w
|
||||||
@ -16,6 +22,8 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
class DailyTimeIntervalStrategyHandlerTest {
|
class DailyTimeIntervalStrategyHandlerTest {
|
||||||
|
|
||||||
|
private static final DailyTimeIntervalStrategyHandler HD = new DailyTimeIntervalStrategyHandler();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void calculateInRangeTime() {
|
void calculateInRangeTime() {
|
||||||
|
|
||||||
@ -36,12 +44,47 @@ class DailyTimeIntervalStrategyHandlerTest {
|
|||||||
// 星期不满足(2023-02-15 11:00:00)
|
// 星期不满足(2023-02-15 11:00:00)
|
||||||
Long notTodayStartTs = DailyTimeIntervalStrategyHandler.calculateInRangeTime(ts, simpleBuild("11:00:00", "12:15:00", Sets.newHashSet(3)));
|
Long notTodayStartTs = DailyTimeIntervalStrategyHandler.calculateInRangeTime(ts, simpleBuild("11:00:00", "12:15:00", Sets.newHashSet(3)));
|
||||||
assert notTodayStartTs.equals(1676430000000L);
|
assert notTodayStartTs.equals(1676430000000L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValid() throws Exception {
|
||||||
|
// 合法数据
|
||||||
|
HD.validate(JsonUtils.toJSONString(simpleBuild("12:33:33", "13:33:33", null)));
|
||||||
|
HD.validate(JsonUtils.toJSONString(simpleBuild("12:33:33", "13:33:33", Sets.newHashSet(1, 2, 3))));
|
||||||
|
|
||||||
|
// start > end
|
||||||
|
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
|
||||||
|
HD.validate(JsonUtils.toJSONString(simpleBuild("17:33:33", "13:33:33", null)));
|
||||||
|
});
|
||||||
|
|
||||||
|
// start is empty
|
||||||
|
Assertions.assertThrowsExactly(PowerJobException.class, () -> {
|
||||||
|
HD.validate(JsonUtils.toJSONString(simpleBuild(null, "13:33:33", null)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCalculateNextTriggerTime() {
|
||||||
|
long ts = 1676126794874L;
|
||||||
|
String expression = JsonUtils.toJSONString(simpleBuild("12:33:33", "15:34:33", Sets.newHashSet(1, 4)));
|
||||||
|
int i = 0;
|
||||||
|
List<Long> ret = Lists.newArrayList();
|
||||||
|
for (;; i++) {
|
||||||
|
Long triggerTime = HD.calculateNextTriggerTime(ts, expression, 1276126794874L, 1676735920000L);
|
||||||
|
if (triggerTime == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ret.add(triggerTime);
|
||||||
|
ts = triggerTime;
|
||||||
|
log.info("[DailyTimeIntervalStrategyHandlerTest] [calculateNextTriggerTime] {}st ->ts={},date={}", i, triggerTime, DateFormatUtils.format(triggerTime, OmsConstant.TIME_PATTERN));
|
||||||
|
}
|
||||||
|
assert i == 8;
|
||||||
|
assert ret.equals(JSONArray.parseArray("[1676262813000, 1676266413000, 1676270013000, 1676273613000, 1676522013000, 1676525613000, 1676529213000, 1676532813000]", Long.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DailyTimeIntervalStrategyHandler.DailyTimeIntervalExpress simpleBuild(String startTimeOfDay, String endTimeOfDay, Set<Integer> days) {
|
private static DailyTimeIntervalStrategyHandler.DailyTimeIntervalExpress simpleBuild(String startTimeOfDay, String endTimeOfDay, Set<Integer> days) {
|
||||||
DailyTimeIntervalStrategyHandler.DailyTimeIntervalExpress ep = new DailyTimeIntervalStrategyHandler.DailyTimeIntervalExpress();
|
DailyTimeIntervalStrategyHandler.DailyTimeIntervalExpress ep = new DailyTimeIntervalStrategyHandler.DailyTimeIntervalExpress();
|
||||||
ep.setInterval(30L);
|
ep.setInterval(3600L);
|
||||||
ep.setStartTimeOfDay(startTimeOfDay);
|
ep.setStartTimeOfDay(startTimeOfDay);
|
||||||
ep.setEndTimeOfDay(endTimeOfDay);
|
ep.setEndTimeOfDay(endTimeOfDay);
|
||||||
ep.setDaysOfWeek(days);
|
ep.setDaysOfWeek(days);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user