Skip to content

Commit a1e9c09

Browse files
Add test
1 parent 0fa11db commit a1e9c09

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowInternal.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,8 @@ static WorkflowOutboundCallsInterceptor getWorkflowOutboundInterceptor() {
845845
static SyncWorkflowContext getRootWorkflowContext() {
846846
// If we are in a query handler, we need to get the workflow context from the
847847
// QueryDispatcher, otherwise we get it from the current thread's internal context.
848-
// This is necessary because query handlers run in a different context than the main workflow threads.
848+
// This is necessary because query handlers run in a different context than the main workflow
849+
// threads.
849850
if (QueryDispatcher.isQueryHandler()) {
850851
return QueryDispatcher.getWorkflowContext();
851852
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.temporal.workflow.queryTests;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import io.temporal.client.WorkflowClient;
6+
import io.temporal.client.WorkflowStub;
7+
import io.temporal.testing.internal.SDKTestWorkflowRule;
8+
import io.temporal.workflow.Workflow;
9+
import io.temporal.workflow.WorkflowInfo;
10+
import io.temporal.workflow.WorkflowLocal;
11+
import io.temporal.workflow.shared.TestWorkflows;
12+
import java.time.Duration;
13+
import org.junit.Rule;
14+
import org.junit.Test;
15+
16+
public class WorkflowInfoAndLocalInQueryTest {
17+
18+
@Rule
19+
public SDKTestWorkflowRule testWorkflowRule =
20+
SDKTestWorkflowRule.newBuilder().setWorkflowTypes(TestWorkflow.class).build();
21+
22+
@Test
23+
public void queryReturnsInfoAndLocal() {
24+
TestWorkflows.TestWorkflowWithQuery workflowStub =
25+
testWorkflowRule.newWorkflowStub(TestWorkflows.TestWorkflowWithQuery.class);
26+
WorkflowClient.start(workflowStub::execute);
27+
28+
assertEquals("attempt=1 local=42", workflowStub.query());
29+
assertEquals("done", WorkflowStub.fromTyped(workflowStub).getResult(String.class));
30+
}
31+
32+
public static class TestWorkflow implements TestWorkflows.TestWorkflowWithQuery {
33+
34+
private final WorkflowLocal<Integer> local = WorkflowLocal.withCachedInitial(() -> 0);
35+
36+
@Override
37+
public String execute() {
38+
local.set(42);
39+
Workflow.sleep(Duration.ofSeconds(1));
40+
return "done";
41+
}
42+
43+
@Override
44+
public String query() {
45+
WorkflowInfo info = Workflow.getInfo();
46+
return "attempt=" + info.getAttempt() + " local=" + local.get();
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)