|
24 | 24 | import io.serverlessworkflow.api.WorkflowReader;
|
25 | 25 | import io.serverlessworkflow.impl.jackson.JsonUtils;
|
26 | 26 | import java.io.IOException;
|
| 27 | +import java.time.Instant; |
| 28 | +import java.time.temporal.ChronoUnit; |
27 | 29 | import java.util.Map;
|
28 | 30 | import java.util.concurrent.CompletableFuture;
|
29 | 31 | import java.util.stream.Stream;
|
30 | 32 | import org.junit.jupiter.api.BeforeAll;
|
| 33 | +import org.junit.jupiter.api.Test; |
31 | 34 | import org.junit.jupiter.params.ParameterizedTest;
|
32 | 35 | import org.junit.jupiter.params.provider.Arguments;
|
33 | 36 | import org.junit.jupiter.params.provider.MethodSource;
|
@@ -81,6 +84,31 @@ void testEventsListened(String listen, String emit1, String emit2, JsonNode expe
|
81 | 84 | assertThat(waitingInstance.outputAs(JsonNode.class)).isEqualTo(expectedResult);
|
82 | 85 | }
|
83 | 86 |
|
| 87 | + @Test |
| 88 | + void testForEachInAnyIsExecutedAsEventArrive() throws IOException, InterruptedException { |
| 89 | + WorkflowDefinition listenDefinition = |
| 90 | + appl.workflowDefinition( |
| 91 | + WorkflowReader.readWorkflowFromClasspath("listen-to-any-until.yaml")); |
| 92 | + WorkflowDefinition emitDoctorDefinition = |
| 93 | + appl.workflowDefinition(WorkflowReader.readWorkflowFromClasspath("emit-doctor.yaml")); |
| 94 | + WorkflowInstance waitingInstance = listenDefinition.instance(Map.of()); |
| 95 | + CompletableFuture<WorkflowModel> future = waitingInstance.start(); |
| 96 | + assertThat(waitingInstance.status()).isEqualTo(WorkflowStatus.WAITING); |
| 97 | + emitDoctorDefinition.instance(Map.of("temperature", 35)).start().join(); |
| 98 | + assertThat(waitingInstance.status()).isEqualTo(WorkflowStatus.WAITING); |
| 99 | + Thread.sleep(1100); |
| 100 | + emitDoctorDefinition.instance(Map.of("temperature", 39)).start().join(); |
| 101 | + assertThat(future).isCompleted(); |
| 102 | + assertThat(waitingInstance.status()).isEqualTo(WorkflowStatus.COMPLETED); |
| 103 | + ArrayNode result = waitingInstance.outputAs(ArrayNode.class); |
| 104 | + assertThat(ChronoUnit.SECONDS.between(getInstant(result, 0), getInstant(result, 1))) |
| 105 | + .isGreaterThanOrEqualTo(1L); |
| 106 | + } |
| 107 | + |
| 108 | + private static Instant getInstant(ArrayNode result, int index) { |
| 109 | + return Instant.ofEpochSecond(result.get(index).get("time").asLong()); |
| 110 | + } |
| 111 | + |
84 | 112 | private static Stream<Arguments> eventListenerParameters() {
|
85 | 113 | return Stream.of(
|
86 | 114 | Arguments.of("listen-to-any.yaml", "emit.yaml", array(cruellaDeVil()), Map.of()),
|
|
0 commit comments