Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class TestWorkflowExtension
private final String target;
private final boolean doNotStart;
private final long initialTimeMillis;
private final boolean useTimeskipping;

private final Set<Class<?>> supportedParameterTypes = new HashSet<>();
private boolean includesDynamicWorkflow;
Expand All @@ -110,6 +111,7 @@ private TestWorkflowExtension(Builder builder) {
target = builder.target;
doNotStart = builder.doNotStart;
initialTimeMillis = builder.initialTimeMillis;
useTimeskipping = builder.useTimeskipping;

supportedParameterTypes.add(TestWorkflowEnvironment.class);
supportedParameterTypes.add(WorkflowClient.class);
Expand Down Expand Up @@ -187,20 +189,15 @@ public Object resolveParameter(
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
public void beforeEach(ExtensionContext context) {
long currentInitialTimeMillis =
AnnotationSupport.findAnnotation(context.getElement(), WorkflowInitialTime.class)
.map(annotation -> Instant.parse(annotation.value()).toEpochMilli())
.orElse(initialTimeMillis);
TestEnvironmentOptions testOptions =
TestEnvironmentOptions.newBuilder()
.setWorkflowClientOptions(workflowClientOptions)
.setWorkerFactoryOptions(workerFactoryOptions)
.setUseExternalService(useExternalService)
.setTarget(target)
.setInitialTimeMillis(currentInitialTimeMillis)
.build();
TestWorkflowEnvironment testEnvironment = TestWorkflowEnvironment.newInstance(testOptions);

TestWorkflowEnvironment testEnvironment =
TestWorkflowEnvironment.newInstance(createTestEnvOptions(currentInitialTimeMillis));

String taskQueue =
String.format("WorkflowTest-%s-%s", context.getDisplayName(), context.getUniqueId());
Worker worker = testEnvironment.newWorker(taskQueue, workerOptions);
Expand All @@ -216,6 +213,17 @@ public void beforeEach(ExtensionContext context) throws Exception {
setWorkflowOptions(context, WorkflowOptions.newBuilder().setTaskQueue(taskQueue).build());
}

protected TestEnvironmentOptions createTestEnvOptions(long initialTimeMillis) {
return TestEnvironmentOptions.newBuilder()
.setWorkflowClientOptions(workflowClientOptions)
.setWorkerFactoryOptions(workerFactoryOptions)
.setUseExternalService(useExternalService)
.setUseTimeskipping(useTimeskipping)
.setTarget(target)
.setInitialTimeMillis(initialTimeMillis)
.build();
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
TestWorkflowEnvironment testEnvironment = getTestEnvironment(context);
Expand Down Expand Up @@ -274,6 +282,9 @@ public static class Builder {
private String target = null;
private boolean doNotStart = false;
private long initialTimeMillis;
// Default to TestEnvironmentOptions isUseTimeskipping
private boolean useTimeskipping =
TestEnvironmentOptions.getDefaultInstance().isUseTimeskipping();

private Builder() {}

Expand Down Expand Up @@ -397,6 +408,17 @@ public Builder setInitialTime(Instant initialTime) {
return this;
}

/**
* Sets TestEnvironmentOptions.setUseTimeskippings. If true, no actual wall-clock time will pass
* when a workflow sleeps or sets a timer.
*
* <p>Default is true
*/
public Builder setUseTimeskipping(boolean useTimeskipping) {
this.useTimeskipping = useTimeskipping;
return this;
}

public TestWorkflowExtension build() {
return new TestWorkflowExtension(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public class TestWorkflowRule implements TestRule {
private final WorkflowImplementationOptions workflowImplementationOptions;
private final WorkerFactoryOptions workerFactoryOptions;
private final WorkerOptions workerOptions;
private final WorkflowClientOptions clientOptions;
private final String target;
private boolean useTimeskipping;

private String taskQueue;
private final TestWorkflowEnvironment testEnvironment;
Expand Down Expand Up @@ -117,20 +120,26 @@ private TestWorkflowRule(Builder builder) {
? Timeout.seconds(builder.testTimeoutSeconds)
: null;

WorkflowClientOptions clientOptions =
clientOptions =
(builder.workflowClientOptions == null)
? WorkflowClientOptions.newBuilder().setNamespace(namespace).build()
: builder.workflowClientOptions.toBuilder().setNamespace(namespace).build();
TestEnvironmentOptions testOptions =
TestEnvironmentOptions.newBuilder()
.setWorkflowClientOptions(clientOptions)
.setWorkerFactoryOptions(workerFactoryOptions)
.setUseExternalService(useExternalService)
.setTarget(builder.target)
.setInitialTimeMillis(builder.initialTimeMillis)
.build();

testEnvironment = TestWorkflowEnvironment.newInstance(testOptions);
target = builder.target;
useTimeskipping = builder.useTimeskipping;

testEnvironment =
TestWorkflowEnvironment.newInstance(createTestEnvOptions(builder.initialTimeMillis));
}

protected TestEnvironmentOptions createTestEnvOptions(long initialTimeMillis) {
return TestEnvironmentOptions.newBuilder()
.setWorkflowClientOptions(clientOptions)
.setWorkerFactoryOptions(workerFactoryOptions)
.setUseExternalService(useExternalService)
.setUseTimeskipping(useTimeskipping)
.setTarget(target)
.setInitialTimeMillis(initialTimeMillis)
.build();
}

public static Builder newBuilder() {
Expand All @@ -144,6 +153,9 @@ public static class Builder {
private boolean useExternalService;
private boolean doNotStart;
private long initialTimeMillis;
// Default to TestEnvironmentOptions isUseTimeskipping
private boolean useTimeskipping =
TestEnvironmentOptions.getDefaultInstance().isUseTimeskipping();

private Class<?>[] workflowTypes;
private Object[] activityImplementations;
Expand Down Expand Up @@ -207,6 +219,17 @@ public Builder setUseExternalService(boolean useExternalService) {
return this;
}

/**
* Sets TestEnvironmentOptions.setUseTimeskippings. If true, no actual wall-clock time will pass
* when a workflow sleeps or sets a timer.
*
* <p>Default is true
*/
public Builder setUseTimeskipping(boolean useTimeskipping) {
this.useTimeskipping = useTimeskipping;
return this;
}

/**
* Optional parameter that defines an endpoint which will be used for the communication with
* standalone temporal service. Has no effect if {@link #setUseExternalService(boolean)} is set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ public Builder setDoNotStart(boolean doNotStart) {
return this;
}

public Builder setUseTimeskipping(boolean useTimeskipping) {
testWorkflowRuleBuilder.setUseTimeskipping(useTimeskipping);
return this;
}

public SDKTestWorkflowRule build() {
if (!workerFactoryOptionsAreSet) {
testWorkflowRuleBuilder.setWorkerFactoryOptions(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package io.temporal.testing;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class TestWorkflowExtensionTimeSkippingTest {
@Test
public void testCheckNoTimeSkipping() {
TestWorkflowExtension testWorkflow =
TestWorkflowExtension.newBuilder().setUseTimeskipping(false).build();

assertFalse(
testWorkflow.createTestEnvOptions(System.currentTimeMillis()).isUseTimeskipping(),
"We disabled the time skipping on the extension, so the TestEnvironmentOptions should have it off too");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package io.temporal.testing;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

public class TestWorkflowRuleTimeSkippingTest {

@Test
public void testWorkflowRuleTimeSkipping() {
TestWorkflowRule defaultTestWorkflowRule = TestWorkflowRule.newBuilder().build();
TestWorkflowRule noTimeSkippingWorkflowRule =
TestWorkflowRule.newBuilder().setUseTimeskipping(false).build();

assertTrue(
"By default time skipping should be on",
defaultTestWorkflowRule
.createTestEnvOptions(System.currentTimeMillis())
.isUseTimeskipping());
assertFalse(
"We disabled the time skipping on the rule, so the TestEnvironmentOptions should have it off too",
noTimeSkippingWorkflowRule
.createTestEnvOptions(System.currentTimeMillis())
.isUseTimeskipping());
}
}