Skip to content

Commit fbe3b83

Browse files
committed
chore: New task execution task id test
test how taskExecutionTaskId can be used for idempotency Signed-off-by: Javier Aliaga <[email protected]>
1 parent ab8e411 commit fbe3b83

File tree

9 files changed

+214
-2
lines changed

9 files changed

+214
-2
lines changed

sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprWorkflowsIT.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.fasterxml.jackson.core.JsonProcessingException;
1717
import com.fasterxml.jackson.databind.ObjectMapper;
18+
1819
import io.dapr.testcontainers.Component;
1920
import io.dapr.testcontainers.DaprContainer;
2021
import io.dapr.testcontainers.DaprLogLevel;
@@ -40,6 +41,7 @@
4041
import java.util.Map;
4142

4243
import static io.dapr.it.testcontainers.ContainerConstants.DAPR_RUNTIME_IMAGE_TAG;
44+
import static org.junit.Assert.assertTrue;
4345
import static org.junit.jupiter.api.Assertions.assertEquals;
4446
import static org.junit.jupiter.api.Assertions.assertNotNull;
4547

@@ -117,6 +119,29 @@ public void testWorkflows() throws Exception {
117119
assertEquals(instanceId, workflowOutput.getWorkflowId());
118120
}
119121

122+
@Test
123+
public void testExecutionKeyWorkflows() throws Exception {
124+
TestWorkflowPayload payload = new TestWorkflowPayload(new ArrayList<>());
125+
String instanceId = workflowClient.scheduleNewWorkflow(TestExecutionKeysWorkflow.class, payload);
126+
127+
workflowClient.waitForInstanceStart(instanceId, Duration.ofSeconds(100), false);
128+
129+
Duration timeout = Duration.ofSeconds(1000);
130+
WorkflowInstanceStatus workflowStatus = workflowClient.waitForInstanceCompletion(instanceId, timeout, true);
131+
132+
assertNotNull(workflowStatus);
133+
134+
TestWorkflowPayload workflowOutput = deserialize(workflowStatus.getSerializedOutput());
135+
136+
assertEquals(1, workflowOutput.getPayloads().size());
137+
assertEquals("Execution key found", workflowOutput.getPayloads().get(0));
138+
139+
String executionKey = workflowOutput.getWorkflowId() +"-"+"io.dapr.it.testcontainers.TaskExecutionKeyActivity";
140+
assertTrue(KeyStore.getInstance().getKey(executionKey));
141+
142+
assertEquals(instanceId, workflowOutput.getWorkflowId());
143+
}
144+
120145
private TestWorkflowPayload deserialize(String value) throws JsonProcessingException {
121146
return OBJECT_MAPPER.readValue(value, TestWorkflowPayload.class);
122147
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.dapr.it.testcontainers;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class KeyStore {
7+
8+
private final Map<String, Boolean> keyStore = new HashMap<>();
9+
10+
private static KeyStore instance;
11+
12+
private KeyStore() {
13+
}
14+
15+
public static KeyStore getInstance() {
16+
if (instance == null) {
17+
synchronized (KeyStore.class) {
18+
if (instance == null) {
19+
instance = new KeyStore();
20+
}
21+
}
22+
}
23+
return instance;
24+
}
25+
26+
27+
public void addKey(String key, Boolean value) {
28+
keyStore.put(key, value);
29+
}
30+
31+
public Boolean getKey(String key) {
32+
return keyStore.get(key);
33+
}
34+
35+
public void removeKey(String key) {
36+
keyStore.remove(key);
37+
}
38+
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2024 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.it.testcontainers;
15+
16+
import io.dapr.workflows.WorkflowActivity;
17+
import io.dapr.workflows.WorkflowActivityContext;
18+
19+
public class TaskExecutionKeyActivity implements WorkflowActivity {
20+
21+
@Override
22+
public Object run(WorkflowActivityContext ctx) {
23+
TestWorkflowPayload workflowPayload = ctx.getInput(TestWorkflowPayload.class);
24+
KeyStore keyStore = KeyStore.getInstance();
25+
Boolean exists = keyStore.getKey(ctx.getTaskExecutionKey());
26+
if (!Boolean.TRUE.equals(exists)) {
27+
keyStore.addKey(ctx.getTaskExecutionKey(), true);
28+
workflowPayload.getPayloads().add("Execution key not found");
29+
throw new IllegalStateException("Task execution key not found");
30+
}
31+
workflowPayload.getPayloads().add("Execution key found");
32+
return workflowPayload;
33+
}
34+
35+
}

sdk-tests/src/test/java/io/dapr/it/testcontainers/TestDaprWorkflowsConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ public WorkflowRuntimeBuilder workflowRuntimeBuilder(
5656
WorkflowRuntimeBuilder builder = new WorkflowRuntimeBuilder(new Properties(overrides));
5757

5858
builder.registerWorkflow(TestWorkflow.class);
59+
builder.registerWorkflow(TestExecutionKeysWorkflow.class);
5960
builder.registerActivity(FirstActivity.class);
6061
builder.registerActivity(SecondActivity.class);
61-
62+
builder.registerActivity(TaskExecutionKeyActivity.class);
63+
64+
6265
return builder;
6366
}
6467
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.it.testcontainers;
15+
16+
import io.dapr.durabletask.Task;
17+
import io.dapr.workflows.Workflow;
18+
import io.dapr.workflows.WorkflowStub;
19+
import io.dapr.workflows.WorkflowTaskOptions;
20+
import io.dapr.workflows.WorkflowTaskRetryPolicy;
21+
22+
import java.time.Duration;
23+
24+
import org.slf4j.Logger;
25+
26+
public class TestExecutionKeysWorkflow implements Workflow {
27+
28+
@Override
29+
public WorkflowStub create() {
30+
return ctx -> {
31+
32+
Logger logger = ctx.getLogger();
33+
String instanceId = ctx.getInstanceId();
34+
logger.info("Starting Workflow: " + ctx.getName());
35+
logger.info("Instance ID: " + instanceId);
36+
logger.info("Current Orchestration Time: " + ctx.getCurrentInstant());
37+
38+
TestWorkflowPayload workflowPayload = ctx.getInput(TestWorkflowPayload.class);
39+
workflowPayload.setWorkflowId(instanceId);
40+
41+
WorkflowTaskOptions options = new WorkflowTaskOptions(WorkflowTaskRetryPolicy.newBuilder()
42+
.setMaxNumberOfAttempts(3)
43+
.setFirstRetryInterval(Duration.ofSeconds(1))
44+
.setMaxRetryInterval(Duration.ofSeconds(10))
45+
.setBackoffCoefficient(2.0)
46+
.setRetryTimeout(Duration.ofSeconds(50))
47+
.build());
48+
49+
50+
Task<TestWorkflowPayload> t = ctx.callActivity(TaskExecutionKeyActivity.class.getName(), workflowPayload, options,TestWorkflowPayload.class);
51+
52+
TestWorkflowPayload payloadAfterExecution = t.await();
53+
54+
ctx.complete(payloadAfterExecution);
55+
};
56+
}
57+
58+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.dapr.it.testcontainers;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Singleton class to hold workflow-related classes and their instances.
8+
*/
9+
public class WorkflowSingleton {
10+
private static WorkflowSingleton instance;
11+
private final List<String> payloads;
12+
private String workflowId;
13+
14+
private WorkflowSingleton() {
15+
this.payloads = new ArrayList<>();
16+
}
17+
18+
public static synchronized WorkflowSingleton getInstance() {
19+
if (instance == null) {
20+
instance = new WorkflowSingleton();
21+
}
22+
return instance;
23+
}
24+
25+
public List<String> getPayloads() {
26+
return payloads;
27+
}
28+
29+
public void addPayload(String payload) {
30+
payloads.add(payload);
31+
}
32+
33+
public String getWorkflowId() {
34+
return workflowId;
35+
}
36+
37+
public void setWorkflowId(String workflowId) {
38+
this.workflowId = workflowId;
39+
}
40+
41+
public void clear() {
42+
payloads.clear();
43+
workflowId = null;
44+
}
45+
}

sdk-workflows/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<dependency>
4848
<groupId>io.dapr</groupId>
4949
<artifactId>durabletask-client</artifactId>
50-
<version>1.5.2</version>
50+
<version>1.5.3-SNAPSHOT</version>
5151
</dependency>
5252
<!--
5353
manually declare durabletask-client's jackson dependencies

sdk-workflows/src/main/java/io/dapr/workflows/WorkflowActivityContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public interface WorkflowActivityContext {
1717

1818
String getName();
1919

20+
String getTaskExecutionKey();
21+
2022
<T> T getInput(Class<T> targetType);
2123

2224
}

sdk-workflows/src/main/java/io/dapr/workflows/runtime/DefaultWorkflowActivityContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public String getName() {
5656
public <T> T getInput(Class<T> targetType) {
5757
return this.innerContext.getInput(targetType);
5858
}
59+
60+
@Override
61+
public String getTaskExecutionKey() {
62+
return this.innerContext.getTaskExecutionKey();
63+
}
5964
}

0 commit comments

Comments
 (0)