Skip to content

Commit 2aed030

Browse files
committed
Refactor Test environment initialization to CadenceTestRule from WorkflowTest.
WorkflowTest is currently 6,000 lines long and has nearly every test related to end to end client behavior. It provides the rather neat behavior that it supports running against both an instance of Cadence running in Docker and against the test version. It's additionally parameterized to run the entire test suite with or without sticky execution enabled. Due to the complexity in handling both environments, adding yet another test to WorkflowTest has always been the easiest option for developers. To allow for tests to easily be split into other files, extract the core functionality to a Junit test rule that can easily be reused by additional tests. With the exception of testSignalCrossDomainExternalWorkflow and the replay tests that don't use the test environment, all tests have been left in WorkflowTest to be split out later.
1 parent 760f880 commit 2aed030

15 files changed

+929
-604
lines changed

src/test/java/com/uber/cadence/RegisterTestDomain.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.uber.cadence;
22

3-
import static com.uber.cadence.workflow.WorkflowTest.DOMAIN;
4-
import static com.uber.cadence.workflow.WorkflowTest.DOMAIN2;
3+
import static com.uber.cadence.testUtils.TestEnvironment.DOMAIN;
4+
import static com.uber.cadence.testUtils.TestEnvironment.DOMAIN2;
55

66
import com.uber.cadence.serviceclient.ClientOptions;
77
import com.uber.cadence.serviceclient.IWorkflowService;
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package com.uber.cadence.testUtils;
2+
3+
import com.uber.cadence.FeatureFlags;
4+
import com.uber.cadence.client.WorkflowClient;
5+
import com.uber.cadence.client.WorkflowClientOptions;
6+
import com.uber.cadence.internal.worker.PollerOptions;
7+
import com.uber.cadence.serviceclient.ClientOptions;
8+
import com.uber.cadence.serviceclient.IWorkflowService;
9+
import com.uber.cadence.serviceclient.WorkflowServiceTChannel;
10+
import com.uber.cadence.testing.TestEnvironmentOptions;
11+
import com.uber.cadence.testing.TestWorkflowEnvironment;
12+
import com.uber.cadence.worker.Worker;
13+
import com.uber.cadence.worker.WorkerFactory;
14+
import com.uber.cadence.worker.WorkerFactoryOptions;
15+
import com.uber.cadence.worker.WorkerOptions;
16+
import com.uber.cadence.workflow.interceptors.TracingWorkflowInterceptorFactory;
17+
import java.time.Duration;
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.concurrent.ExecutionException;
23+
import java.util.concurrent.ScheduledExecutorService;
24+
import java.util.concurrent.ScheduledFuture;
25+
import java.util.concurrent.ScheduledThreadPoolExecutor;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.function.Function;
28+
29+
public class CadenceTestContext {
30+
31+
private final Map<String, Worker> workers = new HashMap<>();
32+
private List<ScheduledFuture<?>> delayedCallbacks = new ArrayList<>();
33+
private final IWorkflowService wfService;
34+
private final WorkflowClient workflowClient;
35+
private final String defaultTaskList;
36+
private final TracingWorkflowInterceptorFactory tracer;
37+
private WorkerFactory workerFactory;
38+
// Real Service only
39+
private ScheduledExecutorService scheduledExecutor;
40+
// Test service only
41+
private TestWorkflowEnvironment testEnvironment;
42+
43+
private CadenceTestContext(
44+
WorkflowClient workflowClient,
45+
String defaultTaskList,
46+
TracingWorkflowInterceptorFactory tracer,
47+
WorkerFactory workerFactory,
48+
ScheduledExecutorService scheduledExecutor,
49+
TestWorkflowEnvironment testEnvironment) {
50+
this.wfService = workflowClient.getService();
51+
this.workflowClient = workflowClient;
52+
this.defaultTaskList = defaultTaskList;
53+
this.tracer = tracer;
54+
this.workerFactory = workerFactory;
55+
this.scheduledExecutor = scheduledExecutor;
56+
this.testEnvironment = testEnvironment;
57+
}
58+
59+
public Worker getDefaultWorker() {
60+
return getOrCreateWorker(getDefaultTaskList());
61+
}
62+
63+
public Worker getOrCreateWorker(String taskList) {
64+
return workers.computeIfAbsent(taskList, this::createWorker);
65+
}
66+
67+
public void start() {
68+
if (isRealService()) {
69+
workerFactory.start();
70+
} else {
71+
testEnvironment.start();
72+
}
73+
}
74+
75+
public void stop() {
76+
if (!workerFactory.isStarted() || workerFactory.isTerminated() || workerFactory.isShutdown()) {
77+
return;
78+
}
79+
if (isRealService()) {
80+
workerFactory.shutdown();
81+
workerFactory.awaitTermination(1, TimeUnit.SECONDS);
82+
for (ScheduledFuture<?> result : delayedCallbacks) {
83+
if (result.isDone() && !result.isCancelled()) {
84+
try {
85+
result.get();
86+
} catch (InterruptedException e) {
87+
} catch (ExecutionException e) {
88+
if (e.getCause() instanceof AssertionError) {
89+
throw (AssertionError) e.getCause();
90+
} else {
91+
throw new RuntimeException("Failed to complete callback", e.getCause());
92+
}
93+
}
94+
}
95+
}
96+
wfService.close();
97+
} else {
98+
testEnvironment.shutdown();
99+
testEnvironment.awaitTermination(1, TimeUnit.SECONDS);
100+
}
101+
if (tracer != null) {
102+
tracer.assertExpected();
103+
}
104+
}
105+
106+
public void suspendPolling() {
107+
workerFactory.suspendPolling();
108+
}
109+
110+
public void resumePolling() {
111+
workerFactory.resumePolling();
112+
}
113+
114+
public void registerDelayedCallback(Duration delay, Runnable r) {
115+
if (isRealService()) {
116+
ScheduledFuture<?> result =
117+
scheduledExecutor.schedule(r, delay.toMillis(), TimeUnit.MILLISECONDS);
118+
delayedCallbacks.add(result);
119+
} else {
120+
testEnvironment.registerDelayedCallback(delay, r);
121+
}
122+
}
123+
124+
public void sleep(Duration d) {
125+
if (isRealService()) {
126+
try {
127+
Thread.sleep(d.toMillis());
128+
} catch (InterruptedException e) {
129+
throw new RuntimeException("Interrupted", e);
130+
}
131+
} else {
132+
testEnvironment.sleep(d);
133+
}
134+
}
135+
136+
public long currentTimeMillis() {
137+
if (isRealService()) {
138+
return System.currentTimeMillis();
139+
} else {
140+
return testEnvironment.currentTimeMillis();
141+
}
142+
}
143+
144+
public String getDefaultTaskList() {
145+
return defaultTaskList;
146+
}
147+
148+
public WorkflowClient getWorkflowClient() {
149+
return workflowClient;
150+
}
151+
152+
public WorkflowClient createWorkflowClient(WorkflowClientOptions options) {
153+
if (isRealService()) {
154+
return WorkflowClient.newInstance(getWorkflowClient().getService(), options);
155+
} else {
156+
return testEnvironment.newWorkflowClient(options);
157+
}
158+
}
159+
160+
public TracingWorkflowInterceptorFactory getTracer() {
161+
return tracer;
162+
}
163+
164+
private boolean isRealService() {
165+
return testEnvironment == null;
166+
}
167+
168+
private Worker createWorker(String taskList) {
169+
if (isRealService()) {
170+
return workerFactory.newWorker(
171+
taskList,
172+
WorkerOptions.newBuilder()
173+
.setActivityPollerOptions(PollerOptions.newBuilder().setPollThreadCount(5).build())
174+
.setMaxConcurrentActivityExecutionSize(1000)
175+
.setInterceptorFactory(tracer)
176+
.build());
177+
} else {
178+
return testEnvironment.newWorker(taskList);
179+
}
180+
}
181+
182+
public static CadenceTestContext forTestService(
183+
Function<TestEnvironmentOptions, TestWorkflowEnvironment> envFactory,
184+
WorkflowClientOptions clientOptions,
185+
String defaultTaskList,
186+
WorkerFactoryOptions workerFactoryOptions) {
187+
TracingWorkflowInterceptorFactory tracer = new TracingWorkflowInterceptorFactory();
188+
189+
TestEnvironmentOptions testOptions =
190+
new TestEnvironmentOptions.Builder()
191+
.setWorkflowClientOptions(clientOptions)
192+
.setInterceptorFactory(tracer)
193+
.setWorkerFactoryOptions(workerFactoryOptions)
194+
.build();
195+
TestWorkflowEnvironment testEnvironment = envFactory.apply(testOptions);
196+
return new CadenceTestContext(
197+
testEnvironment.newWorkflowClient(),
198+
defaultTaskList,
199+
tracer,
200+
testEnvironment.getWorkerFactory(),
201+
null,
202+
testEnvironment);
203+
}
204+
205+
public static CadenceTestContext forRealService(
206+
WorkflowClientOptions clientOptions,
207+
String defaultTaskList,
208+
WorkerFactoryOptions workerFactoryOptions) {
209+
TracingWorkflowInterceptorFactory tracer = new TracingWorkflowInterceptorFactory();
210+
211+
IWorkflowService wfService =
212+
new WorkflowServiceTChannel(
213+
ClientOptions.newBuilder()
214+
.setFeatureFlags(
215+
new FeatureFlags().setWorkflowExecutionAlreadyCompletedErrorEnabled(true))
216+
.build());
217+
WorkflowClient workflowClient = WorkflowClient.newInstance(wfService, clientOptions);
218+
WorkerFactory workerFactory = new WorkerFactory(workflowClient, workerFactoryOptions);
219+
ScheduledExecutorService scheduledExecutor = new ScheduledThreadPoolExecutor(1);
220+
return new CadenceTestContext(
221+
workflowClient, defaultTaskList, tracer, workerFactory, scheduledExecutor, null);
222+
}
223+
}

0 commit comments

Comments
 (0)