【发布时间】:2016-11-13 00:01:45
【问题描述】:
我们有一个服务类,负责根据用户输入安排作业。该类的方法之一接受具有用户输入的对象并为其构建 cron 表达式。 我开始为每个用例创建单元测试,并且在两个几乎相同的测试之间遇到了绝对无法解释的差异:
每 2 天测试一次重复作业的情况:
"ChecklistCreationScheduler#buildCronExpression" should {
"build correct cron expressions for day interval of 2" in {
val jobScheduler = mock[JobScheduler]
val futureChecklistRepository = mock[FutureChecklistRepository]
val chlCreationScheduler = new ChecklistCreationScheduler(jobScheduler, futureChecklistRepository)
val now = DateTime.now.withSecondOfMinute(0).withMillisOfSecond(0)
val dayIntervalForm = mock[CreateJobForm]
dayIntervalForm.maybeDayInterval returns Some(2)
val cronStr = chlCreationScheduler.buildCronExpression(List(dayIntervalForm), now)
cronStr must_== "0 %s %s 1/2 * ? *".format(now.getMinuteOfHour, now.getHourOfDay)
val cronExpression = new CronExpression(cronStr)
val firstRun = cronExpression.getNextValidTimeAfter(now.minusMinutes(1).toDate)
firstRun must_== now.toDate
cronExpression.getNextValidTimeAfter(firstRun) must_== now.plusDays(2).toDate
}
}
而且它每次都能正常工作。
另外,每隔 4 天测试同样的东西:
"build correct cron expressions for day interval of 4" in {
val jobScheduler = mock[JobScheduler]
val futureChecklistRepository = mock[FutureChecklistRepository]
val chlCreationScheduler = new ChecklistCreationScheduler(jobScheduler, futureChecklistRepository)
val now = DateTime.now.withSecondOfMinute(0).withMillisOfSecond(0)
val dayIntervalForm = mock[CreateJobForm]
dayIntervalForm.maybeDayInterval returns Some(4)
val cronStr = chlCreationScheduler.buildCronExpression(List(dayIntervalForm), now)
cronStr must_== "0 %s %s 1/4 * ? *".format(now.getMinuteOfHour, now.getHourOfDay)
val cronExpression = new CronExpression(cronStr)
val firstRun = cronExpression.getNextValidTimeAfter(now.minusMinutes(1).toDate)
firstRun must_== now.toDate
cronExpression.getNextValidTimeAfter(firstRun) must_== now.plusDays(4).toDate
}
最后一个曾经在几天前工作,然后我开始遇到同样的错误而没有真正改变任何东西。
'Wed Jul 13 05:57:00 UTC 2016' 不等于'Mon Jul 11 05:57:00 UTC 2016'
这个错误意味着下一个计算的日期不是第一个运行日期,而是后面的那个。为什么会这样?我错过了什么?
【问题讨论】:
标签: scala cron quartz-scheduler scheduling