Skip to content

Commit 11bbbbe

Browse files
authored
add replayer test with worker interceptor (temporalio#747)
* add replayer test with worker interceptor Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * update Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> --------- Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent 84b4fee commit 11bbbbe

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package io.temporal.samples.interceptorreplaytest;
2+
3+
import static org.junit.Assert.fail;
4+
5+
import io.temporal.activity.ActivityInterface;
6+
import io.temporal.activity.ActivityOptions;
7+
import io.temporal.client.WorkflowOptions;
8+
import io.temporal.common.WorkflowExecutionHistory;
9+
import io.temporal.common.interceptors.*;
10+
import io.temporal.testing.TestWorkflowEnvironment;
11+
import io.temporal.testing.TestWorkflowExtension;
12+
import io.temporal.testing.WorkflowReplayer;
13+
import io.temporal.worker.Worker;
14+
import io.temporal.worker.WorkerFactoryOptions;
15+
import io.temporal.workflow.Workflow;
16+
import io.temporal.workflow.WorkflowInterface;
17+
import io.temporal.workflow.WorkflowMethod;
18+
import java.time.Duration;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.RegisterExtension;
21+
22+
public class InterceptorReplayTest {
23+
@RegisterExtension
24+
public static final TestWorkflowExtension testWorkflowExtension =
25+
TestWorkflowExtension.newBuilder()
26+
// Register workflow and activity impls
27+
.registerWorkflowImplementationTypes(TestWorkflowImpl.class)
28+
.setActivityImplementations(new TestActivitiesImpl())
29+
// Register worker interceptor
30+
.setWorkerFactoryOptions(
31+
WorkerFactoryOptions.newBuilder()
32+
.setWorkerInterceptors(new TestWorkerInterceptor())
33+
.build())
34+
.setDoNotStart(true)
35+
.build();
36+
37+
@Test
38+
public void testReplayWithInterceptors(TestWorkflowEnvironment testEnv, Worker worker) {
39+
// Run our test workflow. We need to set workflow id so can get history after
40+
testEnv.start();
41+
TestWorkflow workflow =
42+
testEnv
43+
.getWorkflowClient()
44+
.newWorkflowStub(
45+
TestWorkflow.class,
46+
WorkflowOptions.newBuilder()
47+
.setWorkflowId("test-workflow")
48+
.setTaskQueue(worker.getTaskQueue())
49+
.build());
50+
workflow.execute();
51+
52+
// Replay execution with history of just executed
53+
WorkflowExecutionHistory eventHistory =
54+
testEnv.getWorkflowClient().fetchHistory("test-workflow");
55+
56+
try {
57+
WorkflowReplayer.replayWorkflowExecution(eventHistory, worker);
58+
} catch (Exception e) {
59+
fail(e.getMessage());
60+
}
61+
testEnv.shutdown();
62+
63+
// Try replaying execution with test env where we dont have interceptors registered
64+
TestWorkflowEnvironment testEnv2 = TestWorkflowEnvironment.newInstance();
65+
Worker testEnv2Worker = testEnv2.newWorker("test-taskqueue");
66+
testEnv2Worker.registerWorkflowImplementationTypes(TestWorkflowImpl.class);
67+
testEnv2Worker.registerActivitiesImplementations(new TestActivitiesImpl());
68+
69+
testEnv2.start();
70+
71+
// Replay should fail with worker that does not have interceptor registered
72+
try {
73+
WorkflowReplayer.replayWorkflowExecution(eventHistory, testEnv2Worker);
74+
fail("This should have failed");
75+
} catch (Exception e) {
76+
System.out.println(e.getMessage());
77+
}
78+
79+
// But it should be fine with worker that does
80+
try {
81+
WorkflowReplayer.replayWorkflowExecution(eventHistory, worker);
82+
} catch (Exception e) {
83+
fail(e.getMessage());
84+
}
85+
86+
testEnv2.shutdown();
87+
}
88+
89+
// Test workflow and activities
90+
@WorkflowInterface
91+
public interface TestWorkflow {
92+
@WorkflowMethod
93+
void execute();
94+
}
95+
96+
public static class TestWorkflowImpl implements TestWorkflow {
97+
98+
TestActivities activities =
99+
Workflow.newActivityStub(
100+
TestActivities.class,
101+
ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build());
102+
103+
@Override
104+
public void execute() {
105+
activities.activityOne();
106+
}
107+
}
108+
109+
@ActivityInterface
110+
public interface TestActivities {
111+
void activityOne();
112+
113+
void activityTwo();
114+
115+
void activityThree();
116+
}
117+
118+
public static class TestActivitiesImpl implements TestActivities {
119+
@Override
120+
public void activityOne() {
121+
System.out.println("Activities one done");
122+
}
123+
124+
@Override
125+
public void activityTwo() {
126+
System.out.println("Activities two done");
127+
}
128+
129+
@Override
130+
public void activityThree() {
131+
System.out.println("Activities three done");
132+
}
133+
}
134+
135+
// Test worker and workflow interceptors
136+
public static class TestWorkerInterceptor extends WorkerInterceptorBase {
137+
@Override
138+
public WorkflowInboundCallsInterceptor interceptWorkflow(WorkflowInboundCallsInterceptor next) {
139+
return new TestWorkflowInboundCallsInterceptor(next);
140+
}
141+
}
142+
143+
public static class TestWorkflowInboundCallsInterceptor
144+
extends WorkflowInboundCallsInterceptorBase {
145+
TestActivities activities =
146+
Workflow.newActivityStub(
147+
TestActivities.class,
148+
ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build());
149+
150+
public TestWorkflowInboundCallsInterceptor(WorkflowInboundCallsInterceptor next) {
151+
super(next);
152+
}
153+
154+
@Override
155+
public void init(WorkflowOutboundCallsInterceptor outboundCalls) {
156+
super.init(new TestWorkflowOutboundCallsInterceptor(outboundCalls));
157+
}
158+
159+
@Override
160+
public WorkflowOutput execute(WorkflowInput input) {
161+
WorkflowOutput output = super.execute(input);
162+
// Run activity three before completing execution
163+
activities.activityThree();
164+
return output;
165+
}
166+
}
167+
168+
public static class TestWorkflowOutboundCallsInterceptor
169+
extends WorkflowOutboundCallsInterceptorBase {
170+
TestActivities activities =
171+
Workflow.newActivityStub(
172+
TestActivities.class,
173+
ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build());
174+
175+
public TestWorkflowOutboundCallsInterceptor(WorkflowOutboundCallsInterceptor next) {
176+
super(next);
177+
}
178+
179+
@Override
180+
public <R> ActivityOutput<R> executeActivity(ActivityInput<R> input) {
181+
ActivityOutput output = super.executeActivity(input);
182+
183+
// we only want to intercept ActivityOne here
184+
if (input.getActivityName().equals("ActivityOne")) {
185+
activities.activityTwo();
186+
}
187+
188+
return output;
189+
}
190+
}
191+
}

0 commit comments

Comments
 (0)