Search in sources :

Example 1 with MemoryFlowListeners

use of io.kestra.runner.memory.MemoryFlowListeners in project kestra by kestra-io.

the class SchedulerConditionTest method 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));
    }
}
Also used : State(io.kestra.core.models.flows.State) MemoryFlowListeners(io.kestra.runner.memory.MemoryFlowListeners) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(io.kestra.core.models.flows.Flow) Test(org.junit.jupiter.api.Test)

Example 2 with MemoryFlowListeners

use of io.kestra.runner.memory.MemoryFlowListeners in project kestra by kestra-io.

the class SchedulerScheduleTest method schedule.

@Test
void schedule() throws Exception {
    // mock flow listeners
    MemoryFlowListeners flowListenersServiceSpy = spy(this.flowListenersService);
    SchedulerExecutionStateInterface executionRepositorySpy = spy(this.executionState);
    CountDownLatch queueCount = new CountDownLatch(5);
    Flow flow = createScheduleFlow();
    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(SchedulerScheduleTest.class, execution -> {
            assertThat(execution.getInputs().get("testInputs"), is("test-inputs"));
            queueCount.countDown();
            if (execution.getState().getCurrent() == State.Type.CREATED) {
                executionQueue.emit(execution.withState(State.Type.SUCCESS));
            }
            assertThat(execution.getFlowId(), is(flow.getId()));
        });
        scheduler.run();
        queueCount.await(1, TimeUnit.MINUTES);
        assertThat(queueCount.getCount(), is(0L));
    }
}
Also used : State(io.kestra.core.models.flows.State) MemoryFlowListeners(io.kestra.runner.memory.MemoryFlowListeners) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(io.kestra.core.models.flows.Flow) Test(org.junit.jupiter.api.Test)

Example 3 with MemoryFlowListeners

use of io.kestra.runner.memory.MemoryFlowListeners in project kestra by kestra-io.

the class SchedulerThreadTest method thread.

@Test
void thread() throws Exception {
    // mock flow listeners
    MemoryFlowListeners flowListenersServiceSpy = spy(this.flowListenersService);
    SchedulerExecutionStateInterface schedulerExecutionStateSpy = spy(this.executionState);
    CountDownLatch queueCount = new CountDownLatch(2);
    Flow flow = createThreadFlow();
    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(schedulerExecutionStateSpy).findById(any());
    // scheduler
    try (AbstractScheduler scheduler = new DefaultScheduler(applicationContext, flowListenersServiceSpy, schedulerExecutionStateSpy, triggerState)) {
        AtomicReference<Execution> last = new AtomicReference<>();
        // wait for execution
        executionQueue.receive(SchedulerThreadTest.class, execution -> {
            last.set(execution);
            assertThat(execution.getFlowId(), is(flow.getId()));
            if (execution.getState().getCurrent() != State.Type.SUCCESS) {
                executionQueue.emit(execution.withState(State.Type.SUCCESS));
                queueCount.countDown();
            }
        });
        scheduler.run();
        queueCount.await(1, TimeUnit.MINUTES);
        assertThat(last.get().getVariables().get("defaultInjected"), is("done"));
        assertThat(last.get().getVariables().get("counter"), is(3));
        AbstractSchedulerTest.COUNTER = 0;
    }
}
Also used : Execution(io.kestra.core.models.executions.Execution) State(io.kestra.core.models.flows.State) MemoryFlowListeners(io.kestra.runner.memory.MemoryFlowListeners) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(io.kestra.core.models.flows.Flow) Test(org.junit.jupiter.api.Test)

Aggregations

Flow (io.kestra.core.models.flows.Flow)3 State (io.kestra.core.models.flows.State)3 MemoryFlowListeners (io.kestra.runner.memory.MemoryFlowListeners)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.jupiter.api.Test)3 Execution (io.kestra.core.models.executions.Execution)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1