Skip to content

Commit b4bac2a

Browse files
committed
[Fix #1343] Add ForkTask shortcut to DSL
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 04a3563 commit b4bac2a

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

  • experimental

‎experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,22 @@ public static <T> FuncTaskConfigurer forEach(
11161116
return list -> list.forEach(j -> j.collection(ctx -> collection).tasks(body));
11171117
}
11181118

1119+
public static FuncTaskConfigurer fork(String name, FuncTaskConfigurer... branches) {
1120+
return list ->
1121+
list.fork(
1122+
name,
1123+
forkBuilder -> {
1124+
for (FuncTaskConfigurer branch : branches) {
1125+
forkBuilder.branch((FuncTaskItemListBuilder v) -> branch.accept(v));
1126+
}
1127+
});
1128+
}
1129+
1130+
public static FuncTaskConfigurer fork(FuncTaskConfigurer... branches) {
1131+
Objects.requireNonNull(branches, "Array of branches cannot be null");
1132+
return fork(null, branches);
1133+
}
1134+
11191135
/**
11201136
* Set a raw expression on the current list (advanced).
11211137
*
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)