diff --git a/server/src/test/java/e2e/InterruptLifecycleTest.java b/server/src/test/java/e2e/InterruptLifecycleTest.java new file mode 100644 index 0000000000..3975132ec7 --- /dev/null +++ b/server/src/test/java/e2e/InterruptLifecycleTest.java @@ -0,0 +1,66 @@ +package e2e; + +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import io.littlehorse.sdk.common.proto.LHStatus; +import io.littlehorse.sdk.wfsdk.SpawnedThreads; +import io.littlehorse.sdk.wfsdk.Workflow; +import io.littlehorse.sdk.worker.LHTaskMethod; +import io.littlehorse.test.LHTest; +import io.littlehorse.test.LHWorkflow; +import io.littlehorse.test.WorkflowVerifier; + +/* + * Tests that Interrupts cause NodeRun lifecycle to move properly. + */ +@LHTest(externalEventNames = { + InterruptLifecycleTest.PARENT_EVENT, + InterruptLifecycleTest.INTERRUPT_TRIGGER, + InterruptLifecycleTest.COMPLETE_INTERRUPT_HANDLER, + InterruptLifecycleTest.CHILD_EVENT +}) +public class InterruptLifecycleTest { + public static final String PARENT_EVENT = "ilt-parent-event"; + public static final String CHILD_EVENT = "ilt-child-event"; + public static final String INTERRUPT_TRIGGER = "ilt-interrupt-trigger"; + public static final String COMPLETE_INTERRUPT_HANDLER = "ilt-complete-interrupt-handler"; + + @LHWorkflow("interrupt-lifecycle-test") + public Workflow interruptLifecycleTest; + + private WorkflowVerifier verifier; + + @Test + void shouldCompleteWithNoInterrupts() { + verifier.prepareRun(interruptLifecycleTest) + .thenSendExternalEventJsonContent(PARENT_EVENT, null) + .thenSendExternalEventJsonContent(PARENT_EVENT, null) + .waitForStatus(LHStatus.COMPLETED) + .start(); + } + + @LHWorkflow("interrupt-lifecycle-test") + public Workflow getInterruptLifecycleTestWf() { + return Workflow.newWorkflow("interrupt-lifecycle-test", wf -> { + wf.registerInterruptHandler(INTERRUPT_TRIGGER, handler -> { + handler.waitForEvent(COMPLETE_INTERRUPT_HANDLER); + }); + + wf.waitForEvent(PARENT_EVENT); + wf.sleepSeconds(1); + wf.execute("dummy-task"); + + // Spawn and wait for child + wf.waitForThreads(SpawnedThreads.of(wf.spawnThread(child -> { + child.waitForEvent(CHILD_EVENT); + }, "child", Map.of()))); + }); + } + + @LHTaskMethod("dummy-task") + public String obiwan() { + return "hello there"; + } +} diff --git a/server/src/test/java/e2e/InterruptOnChildThreadTest.java b/server/src/test/java/e2e/InterruptOnChildThreadTest.java index 3019fb1ad7..aea2026c6b 100644 --- a/server/src/test/java/e2e/InterruptOnChildThreadTest.java +++ b/server/src/test/java/e2e/InterruptOnChildThreadTest.java @@ -205,6 +205,7 @@ void shouldCompleteWhenBothInterruptedChildInterruptedFirst() { ThreadRun childInterruptThread = wfRun.getThreadRuns(2); Assertions.assertThat(childInterruptThread.getStatus()).isNotEqualTo(LHStatus.HALTED); }) + .waitForStatus(LHStatus.COMPLETED) .start(); } diff --git a/test-utils/src/main/java/io/littlehorse/test/LHExtension.java b/test-utils/src/main/java/io/littlehorse/test/LHExtension.java index 7836f63d55..2359ba9d82 100644 --- a/test-utils/src/main/java/io/littlehorse/test/LHExtension.java +++ b/test-utils/src/main/java/io/littlehorse/test/LHExtension.java @@ -16,6 +16,7 @@ import io.littlehorse.sdk.worker.LHTaskWorker; import io.littlehorse.test.exception.LHTestExceptionUtil; import io.littlehorse.test.exception.LHTestInitializationException; +import io.littlehorse.test.internal.ExternalTestBootstrapper; import io.littlehorse.test.internal.StandaloneTestBootstrapper; import io.littlehorse.test.internal.TestContext; import java.time.Duration; @@ -40,7 +41,7 @@ public void beforeAll(ExtensionContext context) { getStore(context) .getOrComputeIfAbsent( LH_TEST_CONTEXT, - s -> new TestContext(new StandaloneTestBootstrapper(new PrincipalIdModel(getPrincipalId()))), + s -> new TestContext(new ExternalTestBootstrapper(new PrincipalIdModel(getPrincipalId()))), TestContext.class); } diff --git a/test-utils/src/main/java/io/littlehorse/test/WfRunVerifier.java b/test-utils/src/main/java/io/littlehorse/test/WfRunVerifier.java index 3390896681..b054ee3708 100644 --- a/test-utils/src/main/java/io/littlehorse/test/WfRunVerifier.java +++ b/test-utils/src/main/java/io/littlehorse/test/WfRunVerifier.java @@ -90,9 +90,13 @@ public WfRunVerifier thenSendExternalEventJsonContent(String externalEventName, } public WfRunVerifier waitForStatus(LHStatus status) { + return this.waitForStatus(status, null); + } + + public WfRunVerifier waitForStatus(LHStatus status, Duration timeout) { Function objectLHStatusFunction = context -> lhClient.getWfRun(context.getWfRunId()).getStatus(); - steps.add(new WaitForStatusStep<>(objectLHStatusFunction, status, steps.size() + 1)); + steps.add(new WaitForStatusStep<>(objectLHStatusFunction, status, timeout, steps.size() + 1)); return this; }