Skip to content

Commit f0ebbe7

Browse files
authored
Expose turning off time skipping for WorkflowRule and WorkflowExtension (#967)
1 parent 297ccbc commit f0ebbe7

File tree

5 files changed

+153
-21
lines changed

5 files changed

+153
-21
lines changed

temporal-testing/src/main/java/io/temporal/testing/TestWorkflowExtension.java

+32-10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public class TestWorkflowExtension
9191
private final String target;
9292
private final boolean doNotStart;
9393
private final long initialTimeMillis;
94+
private final boolean useTimeskipping;
9495

9596
private final Set<Class<?>> supportedParameterTypes = new HashSet<>();
9697
private boolean includesDynamicWorkflow;
@@ -110,6 +111,7 @@ private TestWorkflowExtension(Builder builder) {
110111
target = builder.target;
111112
doNotStart = builder.doNotStart;
112113
initialTimeMillis = builder.initialTimeMillis;
114+
useTimeskipping = builder.useTimeskipping;
113115

114116
supportedParameterTypes.add(TestWorkflowEnvironment.class);
115117
supportedParameterTypes.add(WorkflowClient.class);
@@ -187,20 +189,15 @@ public Object resolveParameter(
187189
}
188190

189191
@Override
190-
public void beforeEach(ExtensionContext context) throws Exception {
192+
public void beforeEach(ExtensionContext context) {
191193
long currentInitialTimeMillis =
192194
AnnotationSupport.findAnnotation(context.getElement(), WorkflowInitialTime.class)
193195
.map(annotation -> Instant.parse(annotation.value()).toEpochMilli())
194196
.orElse(initialTimeMillis);
195-
TestEnvironmentOptions testOptions =
196-
TestEnvironmentOptions.newBuilder()
197-
.setWorkflowClientOptions(workflowClientOptions)
198-
.setWorkerFactoryOptions(workerFactoryOptions)
199-
.setUseExternalService(useExternalService)
200-
.setTarget(target)
201-
.setInitialTimeMillis(currentInitialTimeMillis)
202-
.build();
203-
TestWorkflowEnvironment testEnvironment = TestWorkflowEnvironment.newInstance(testOptions);
197+
198+
TestWorkflowEnvironment testEnvironment =
199+
TestWorkflowEnvironment.newInstance(createTestEnvOptions(currentInitialTimeMillis));
200+
204201
String taskQueue =
205202
String.format("WorkflowTest-%s-%s", context.getDisplayName(), context.getUniqueId());
206203
Worker worker = testEnvironment.newWorker(taskQueue, workerOptions);
@@ -216,6 +213,17 @@ public void beforeEach(ExtensionContext context) throws Exception {
216213
setWorkflowOptions(context, WorkflowOptions.newBuilder().setTaskQueue(taskQueue).build());
217214
}
218215

216+
protected TestEnvironmentOptions createTestEnvOptions(long initialTimeMillis) {
217+
return TestEnvironmentOptions.newBuilder()
218+
.setWorkflowClientOptions(workflowClientOptions)
219+
.setWorkerFactoryOptions(workerFactoryOptions)
220+
.setUseExternalService(useExternalService)
221+
.setUseTimeskipping(useTimeskipping)
222+
.setTarget(target)
223+
.setInitialTimeMillis(initialTimeMillis)
224+
.build();
225+
}
226+
219227
@Override
220228
public void afterEach(ExtensionContext context) throws Exception {
221229
TestWorkflowEnvironment testEnvironment = getTestEnvironment(context);
@@ -274,6 +282,9 @@ public static class Builder {
274282
private String target = null;
275283
private boolean doNotStart = false;
276284
private long initialTimeMillis;
285+
// Default to TestEnvironmentOptions isUseTimeskipping
286+
private boolean useTimeskipping =
287+
TestEnvironmentOptions.getDefaultInstance().isUseTimeskipping();
277288

278289
private Builder() {}
279290

@@ -397,6 +408,17 @@ public Builder setInitialTime(Instant initialTime) {
397408
return this;
398409
}
399410

411+
/**
412+
* Sets TestEnvironmentOptions.setUseTimeskippings. If true, no actual wall-clock time will pass
413+
* when a workflow sleeps or sets a timer.
414+
*
415+
* <p>Default is true
416+
*/
417+
public Builder setUseTimeskipping(boolean useTimeskipping) {
418+
this.useTimeskipping = useTimeskipping;
419+
return this;
420+
}
421+
400422
public TestWorkflowExtension build() {
401423
return new TestWorkflowExtension(this);
402424
}

temporal-testing/src/main/java/io/temporal/testing/TestWorkflowRule.java

+34-11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public class TestWorkflowRule implements TestRule {
8282
private final WorkflowImplementationOptions workflowImplementationOptions;
8383
private final WorkerFactoryOptions workerFactoryOptions;
8484
private final WorkerOptions workerOptions;
85+
private final WorkflowClientOptions clientOptions;
86+
private final String target;
87+
private boolean useTimeskipping;
8588

8689
private String taskQueue;
8790
private final TestWorkflowEnvironment testEnvironment;
@@ -117,20 +120,26 @@ private TestWorkflowRule(Builder builder) {
117120
? Timeout.seconds(builder.testTimeoutSeconds)
118121
: null;
119122

120-
WorkflowClientOptions clientOptions =
123+
clientOptions =
121124
(builder.workflowClientOptions == null)
122125
? WorkflowClientOptions.newBuilder().setNamespace(namespace).build()
123126
: builder.workflowClientOptions.toBuilder().setNamespace(namespace).build();
124-
TestEnvironmentOptions testOptions =
125-
TestEnvironmentOptions.newBuilder()
126-
.setWorkflowClientOptions(clientOptions)
127-
.setWorkerFactoryOptions(workerFactoryOptions)
128-
.setUseExternalService(useExternalService)
129-
.setTarget(builder.target)
130-
.setInitialTimeMillis(builder.initialTimeMillis)
131-
.build();
132-
133-
testEnvironment = TestWorkflowEnvironment.newInstance(testOptions);
127+
target = builder.target;
128+
useTimeskipping = builder.useTimeskipping;
129+
130+
testEnvironment =
131+
TestWorkflowEnvironment.newInstance(createTestEnvOptions(builder.initialTimeMillis));
132+
}
133+
134+
protected TestEnvironmentOptions createTestEnvOptions(long initialTimeMillis) {
135+
return TestEnvironmentOptions.newBuilder()
136+
.setWorkflowClientOptions(clientOptions)
137+
.setWorkerFactoryOptions(workerFactoryOptions)
138+
.setUseExternalService(useExternalService)
139+
.setUseTimeskipping(useTimeskipping)
140+
.setTarget(target)
141+
.setInitialTimeMillis(initialTimeMillis)
142+
.build();
134143
}
135144

136145
public static Builder newBuilder() {
@@ -144,6 +153,9 @@ public static class Builder {
144153
private boolean useExternalService;
145154
private boolean doNotStart;
146155
private long initialTimeMillis;
156+
// Default to TestEnvironmentOptions isUseTimeskipping
157+
private boolean useTimeskipping =
158+
TestEnvironmentOptions.getDefaultInstance().isUseTimeskipping();
147159

148160
private Class<?>[] workflowTypes;
149161
private Object[] activityImplementations;
@@ -207,6 +219,17 @@ public Builder setUseExternalService(boolean useExternalService) {
207219
return this;
208220
}
209221

222+
/**
223+
* Sets TestEnvironmentOptions.setUseTimeskippings. If true, no actual wall-clock time will pass
224+
* when a workflow sleeps or sets a timer.
225+
*
226+
* <p>Default is true
227+
*/
228+
public Builder setUseTimeskipping(boolean useTimeskipping) {
229+
this.useTimeskipping = useTimeskipping;
230+
return this;
231+
}
232+
210233
/**
211234
* Optional parameter that defines an endpoint which will be used for the communication with
212235
* standalone temporal service. Has no effect if {@link #setUseExternalService(boolean)} is set

temporal-testing/src/main/java/io/temporal/testing/internal/SDKTestWorkflowRule.java

+5
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ public Builder setDoNotStart(boolean doNotStart) {
188188
return this;
189189
}
190190

191+
public Builder setUseTimeskipping(boolean useTimeskipping) {
192+
testWorkflowRuleBuilder.setUseTimeskipping(useTimeskipping);
193+
return this;
194+
}
195+
191196
public SDKTestWorkflowRule build() {
192197
if (!workerFactoryOptionsAreSet) {
193198
testWorkflowRuleBuilder.setWorkerFactoryOptions(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.testing;
21+
22+
import static org.junit.jupiter.api.Assertions.*;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
public class TestWorkflowExtensionTimeSkippingTest {
27+
@Test
28+
public void testCheckNoTimeSkipping() {
29+
TestWorkflowExtension testWorkflow =
30+
TestWorkflowExtension.newBuilder().setUseTimeskipping(false).build();
31+
32+
assertFalse(
33+
testWorkflow.createTestEnvOptions(System.currentTimeMillis()).isUseTimeskipping(),
34+
"We disabled the time skipping on the extension, so the TestEnvironmentOptions should have it off too");
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.testing;
21+
22+
import static org.junit.Assert.assertFalse;
23+
import static org.junit.Assert.assertTrue;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
public class TestWorkflowRuleTimeSkippingTest {
28+
29+
@Test
30+
public void testWorkflowRuleTimeSkipping() {
31+
TestWorkflowRule defaultTestWorkflowRule = TestWorkflowRule.newBuilder().build();
32+
TestWorkflowRule noTimeSkippingWorkflowRule =
33+
TestWorkflowRule.newBuilder().setUseTimeskipping(false).build();
34+
35+
assertTrue(
36+
"By default time skipping should be on",
37+
defaultTestWorkflowRule
38+
.createTestEnvOptions(System.currentTimeMillis())
39+
.isUseTimeskipping());
40+
assertFalse(
41+
"We disabled the time skipping on the rule, so the TestEnvironmentOptions should have it off too",
42+
noTimeSkippingWorkflowRule
43+
.createTestEnvOptions(System.currentTimeMillis())
44+
.isUseTimeskipping());
45+
}
46+
}

0 commit comments

Comments
 (0)