Skip to content

Commit 20118b8

Browse files
committed
[Fix #1414] Use try rather than do as position name
Signed-off-by: fjtirado <ftirados@ibm.com>
1 parent 5b201fb commit 20118b8

4 files changed

Lines changed: 68 additions & 16 deletions

File tree

impl/core/src/main/java/io/serverlessworkflow/impl/executors/TaskExecutorHelper.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,16 @@ public static TaskExecutor<?> createExecutorList(
6161
WorkflowMutablePosition position,
6262
List<TaskItem> taskItems,
6363
WorkflowDefinition workflowDefinition) {
64+
return createExecutorList(position, taskItems, workflowDefinition, "do");
65+
}
66+
67+
public static TaskExecutor<?> createExecutorList(
68+
WorkflowMutablePosition position,
69+
List<TaskItem> taskItems,
70+
WorkflowDefinition workflowDefinition,
71+
String positionPrefix) {
6472
Map<String, TaskExecutorBuilder<?>> executors =
65-
createExecutorBuilderList(position, taskItems, workflowDefinition, "do");
73+
createExecutorBuilderList(position, taskItems, workflowDefinition, positionPrefix);
6674
executors.values().forEach(t -> t.connect(executors));
6775
Iterator<TaskExecutorBuilder<?>> iter = executors.values().iterator();
6876
TaskExecutor<?> first = iter.next().build();

impl/core/src/main/java/io/serverlessworkflow/impl/executors/TryExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ protected TryExecutorBuilder(
7272
this.whenFilter = WorkflowUtils.optionalPredicate(application, catchInfo.getWhen());
7373
this.exceptFilter = WorkflowUtils.optionalPredicate(application, catchInfo.getExceptWhen());
7474
this.taskExecutor =
75-
TaskExecutorHelper.createExecutorList(position, task.getTry(), definition);
75+
TaskExecutorHelper.createExecutorList(position, task.getTry(), definition, "try");
7676
TryTaskCatch catchTask = task.getCatch();
7777
if (catchTask != null) {
7878
this.errorVariable = catchTask.getAs();
7979
List<TaskItem> catchTaskDo = catchTask.getDo();
8080
this.catchTaskExecutor =
8181
catchTaskDo != null && !catchTaskDo.isEmpty()
8282
? Optional.of(
83-
TaskExecutorHelper.createExecutorList(position, catchTaskDo, definition))
83+
TaskExecutorHelper.createExecutorList(position.copy(), catchTaskDo, definition))
8484
: Optional.empty();
8585

8686
Retry retry = catchTask.getRetry();

impl/test/src/test/java/io/serverlessworkflow/impl/test/RetryTimeoutTest.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@
2727
import io.serverlessworkflow.impl.jackson.JsonUtils;
2828
import io.serverlessworkflow.impl.lifecycle.TaskCompletedEvent;
2929
import io.serverlessworkflow.impl.lifecycle.TaskRetriedEvent;
30-
import io.serverlessworkflow.impl.lifecycle.TraceExecutionListener;
3130
import io.serverlessworkflow.impl.lifecycle.WorkflowExecutionListener;
3231
import java.io.IOException;
3332
import java.time.Duration;
3433
import java.util.Map;
35-
import java.util.Set;
3634
import java.util.concurrent.CompletableFuture;
3735
import java.util.concurrent.ConcurrentHashMap;
3836
import okhttp3.mockwebserver.MockResponse;
@@ -55,11 +53,7 @@ void setUp() throws IOException {
5553
apiServer = new MockWebServer();
5654
apiServer.start(9797);
5755
retryListener = new RetryListener();
58-
app =
59-
WorkflowApplication.builder()
60-
.withListener(retryListener)
61-
.withListener(new TraceExecutionListener())
62-
.build();
56+
app = WorkflowApplication.builder().withListener(retryListener).build();
6357
}
6458

6559
@AfterEach
@@ -71,15 +65,16 @@ void tearDown() throws IOException {
7165
private class RetryListener implements WorkflowExecutionListener {
7266

7367
private Map<String, Short> taskRetried = new ConcurrentHashMap<>();
74-
private Set<Short> contexts = ConcurrentHashMap.newKeySet();
68+
private Map<String, Short> tryTaskCompleted = new ConcurrentHashMap<>();
7569

7670
public void onTaskRetried(TaskRetriedEvent ev) {
7771
taskRetried.put(ev.taskContext().position().jsonPointer(), ev.taskContext().retryAttempt());
7872
}
7973

8074
public void onTaskCompleted(TaskCompletedEvent ev) {
8175
if (ev.taskContext().task() instanceof TryTask) {
82-
contexts.add(ev.taskContext().retryAttempt());
76+
tryTaskCompleted.put(
77+
ev.taskContext().position().jsonPointer(), ev.taskContext().retryAttempt());
8378
}
8479
}
8580
}
@@ -107,8 +102,8 @@ void testRetry(String path) throws IOException {
107102
.atMost(Duration.ofSeconds(1))
108103
.until(() -> future.join().as(JsonNode.class).orElseThrow().equals(result));
109104
assertThat(retryListener.taskRetried).hasSize(1);
110-
assertThat(retryListener.taskRetried.get("do/0/tryGetPet/do/0/getPet")).isEqualTo((short) 2);
111-
assertThat(retryListener.contexts).containsOnly((short) 0);
105+
assertThat(retryListener.taskRetried.get("do/0/tryGetPet/try/0/getPet")).isEqualTo((short) 2);
106+
assertThat(retryListener.tryTaskCompleted.values()).containsOnly((short) 0);
112107
}
113108

114109
@Test
@@ -135,8 +130,35 @@ void testNestedRetry() throws IOException {
135130
.atMost(Duration.ofSeconds(1))
136131
.until(() -> future.join().as(JsonNode.class).orElseThrow().equals(result));
137132
assertThat(retryListener.taskRetried).hasSize(2);
138-
assertThat(retryListener.taskRetried.values()).containsExactlyInAnyOrder((short) 5, (short) 2);
139-
assertThat(retryListener.contexts).containsExactlyInAnyOrder((short) 0, (short) 2);
133+
assertThat(retryListener.taskRetried.get("do/0/tryServerError/try/0/tryCommunication/try"))
134+
.isEqualTo((short) 2);
135+
assertThat(
136+
retryListener.taskRetried.get(
137+
"do/0/tryServerError/try/0/tryCommunication/try/0/getPet"))
138+
.isEqualTo((short) 5);
139+
assertThat(retryListener.tryTaskCompleted.get("do/0/tryServerError/try/0/tryCommunication/try"))
140+
.isEqualTo((short) 2);
141+
assertThat(retryListener.tryTaskCompleted.get("do/0/tryServerError/try")).isEqualTo((short) 0);
142+
}
143+
144+
@Test
145+
void testRetryDo() throws IOException {
146+
CompletableFuture<WorkflowModel> future =
147+
app.workflowDefinition(
148+
readWorkflowFromClasspath("workflows-samples/try-catch-with-do.yaml"))
149+
.instance(Map.of("delay", 0.01))
150+
.start();
151+
Awaitility.await()
152+
.atMost(Duration.ofSeconds(1))
153+
.until(
154+
() ->
155+
future
156+
.join()
157+
.asMap()
158+
.orElseThrow()
159+
.equals(Map.of("setAfterFailingTask", "No problem")));
160+
161+
assertThat(retryListener.tryTaskCompleted.get("do/0/attemptTask/try")).isEqualTo((short) 0);
140162
}
141163

142164
@Test
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
document:
2+
dsl: '1.0.0-alpha5'
3+
namespace: acme
4+
name: try-catch-with-do
5+
version: '0.1.0'
6+
do:
7+
- attemptTask:
8+
try:
9+
- failingTask:
10+
raise:
11+
error:
12+
type: https://example.com/errors/runtime
13+
status: 500
14+
catch:
15+
errors:
16+
with:
17+
type: https://example.com/errors/runtime
18+
status: 500
19+
do:
20+
- executeAfterFailingTask:
21+
set:
22+
setAfterFailingTask: "No problem"

0 commit comments

Comments
 (0)