-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Schedule with scheduleCondition was never triggered after …
…first trigger
- Loading branch information
1 parent
604f98b
commit f636301
Showing
6 changed files
with
157 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
core/src/test/java/io/kestra/core/schedulers/SchedulerConditionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package io.kestra.core.schedulers; | ||
|
||
import io.kestra.core.models.conditions.types.DayWeekInMonthCondition; | ||
import io.kestra.core.models.executions.Execution; | ||
import io.kestra.core.models.flows.Flow; | ||
import io.kestra.core.models.flows.State; | ||
import io.kestra.core.models.triggers.Trigger; | ||
import io.kestra.core.models.triggers.types.Schedule; | ||
import io.kestra.runner.memory.MemoryFlowListeners; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.DayOfWeek; | ||
import java.time.ZonedDateTime; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
class SchedulerConditionTest extends AbstractSchedulerTest { | ||
@Inject | ||
protected MemoryFlowListeners flowListenersService; | ||
|
||
@Inject | ||
protected SchedulerTriggerStateInterface triggerState; | ||
|
||
@Inject | ||
protected SchedulerExecutionStateInterface executionState; | ||
|
||
private static Flow createScheduleFlow() { | ||
Schedule schedule = Schedule.builder() | ||
.id("hourly") | ||
.type(Schedule.class.getName()) | ||
.cron("0 0 * * *") | ||
.inputs(Map.of( | ||
"testInputs", "test-inputs" | ||
)) | ||
.scheduleConditions(List.of( | ||
DayWeekInMonthCondition.builder() | ||
.type(DayWeekInMonthCondition.class.getName()) | ||
.date("{{ trigger.date }}") | ||
.dayOfWeek(DayOfWeek.MONDAY) | ||
.dayInMonth(DayWeekInMonthCondition.DayInMonth.FIRST) | ||
.build() | ||
)) | ||
.build(); | ||
|
||
return createFlow(Collections.singletonList(schedule)); | ||
} | ||
|
||
@Test | ||
void schedule() throws Exception { | ||
// mock flow listeners | ||
MemoryFlowListeners flowListenersServiceSpy = spy(this.flowListenersService); | ||
SchedulerExecutionStateInterface executionRepositorySpy = spy(this.executionState); | ||
CountDownLatch queueCount = new CountDownLatch(4); | ||
|
||
Flow flow = createScheduleFlow(); | ||
|
||
triggerState.save(Trigger.builder() | ||
.namespace(flow.getNamespace()) | ||
.flowId(flow.getId()) | ||
.flowRevision(flow.getRevision()) | ||
.triggerId("hourly") | ||
.date(ZonedDateTime.parse("2021-09-06T02:00:00+01:00[Europe/Paris]")) | ||
.build() | ||
); | ||
|
||
doReturn(Collections.singletonList(flow)) | ||
.when(flowListenersServiceSpy) | ||
.flows(); | ||
|
||
// mock the backfill execution is ended | ||
doAnswer(invocation -> Optional.of(Execution.builder().state(new State().withState(State.Type.SUCCESS)).build())) | ||
.when(executionRepositorySpy) | ||
.findById(any()); | ||
|
||
// scheduler | ||
try (AbstractScheduler scheduler = new DefaultScheduler( | ||
applicationContext, | ||
flowListenersServiceSpy, | ||
executionRepositorySpy, | ||
triggerState | ||
)) { | ||
// wait for execution | ||
executionQueue.receive(SchedulerConditionTest.class, execution -> { | ||
if (execution.getState().getCurrent() == State.Type.CREATED) { | ||
executionQueue.emit(execution.withState(State.Type.SUCCESS)); | ||
|
||
queueCount.countDown(); | ||
if (queueCount.getCount() == 0) { | ||
assertThat((ZonedDateTime) execution.getTrigger().getVariables().get("date"), is(ZonedDateTime.parse("2022-01-03T00:00:00+01:00"))); | ||
} | ||
} | ||
assertThat(execution.getFlowId(), is(flow.getId())); | ||
}); | ||
|
||
scheduler.run(); | ||
queueCount.await(1, TimeUnit.MINUTES); | ||
|
||
assertThat(queueCount.getCount(), is(0L)); | ||
} | ||
} | ||
} |