|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.fluent.test; |
| 17 | + |
| 18 | +import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.*; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import io.serverlessworkflow.api.types.Workflow; |
| 22 | +import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder; |
| 23 | +import io.serverlessworkflow.impl.WorkflowApplication; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +public class ForkFuncTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + void testForkVerbose() { |
| 30 | + testIt( |
| 31 | + FuncWorkflowBuilder.workflow("parallel-execution-workflow") |
| 32 | + .tasks( |
| 33 | + funcTaskItemListBuilder -> |
| 34 | + funcTaskItemListBuilder.fork( |
| 35 | + funcForkTaskBuilder -> |
| 36 | + funcForkTaskBuilder.branches( |
| 37 | + inner -> { |
| 38 | + inner.function(f -> f.function(this::doubleIt, int.class)); |
| 39 | + inner.function(f -> f.function(this::halfIt, int.class)); |
| 40 | + }))) |
| 41 | + .build()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testForkSyntaxSugar() { |
| 46 | + testIt( |
| 47 | + FuncWorkflowBuilder.workflow("parallel-execution-workflow") |
| 48 | + .tasks(fork(function(this::doubleIt), function(this::halfIt))) |
| 49 | + .build()); |
| 50 | + } |
| 51 | + |
| 52 | + private void testIt(Workflow workflow) { |
| 53 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 54 | + assertThat( |
| 55 | + app.workflowDefinition(workflow).instance(8).start().join().asCollection().stream() |
| 56 | + .flatMap(m -> m.asMap().orElseThrow().values().stream()) |
| 57 | + .toList()) |
| 58 | + .containsExactlyInAnyOrder(4, 16); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private int doubleIt(int number) { |
| 63 | + return number << 1; |
| 64 | + } |
| 65 | + |
| 66 | + private int halfIt(int number) { |
| 67 | + return number >> 1; |
| 68 | + } |
| 69 | +} |
0 commit comments