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
@@ -0,0 +1,124 @@
/*
* 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.workflow;

import io.temporal.client.WorkflowException;
import io.temporal.common.RetryOptions;
import io.temporal.failure.ApplicationFailure;
import io.temporal.internal.sync.DeterministicRunnerTest;
import io.temporal.worker.WorkflowImplementationOptions;
import io.temporal.workflow.shared.SDKTestWorkflowRule;
import io.temporal.workflow.shared.TestActivities;
import io.temporal.workflow.shared.TestWorkflows;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

public class AsyncRetryOptionsChangeTest {

private final TestActivities.TestActivitiesImpl activitiesImpl =
new TestActivities.TestActivitiesImpl(null);

@Rule
public SDKTestWorkflowRule testWorkflowRule =
SDKTestWorkflowRule.newBuilder()
.setWorkflowTypes(
WorkflowImplementationOptions.newBuilder()
.setFailWorkflowExceptionTypes(IllegalThreadStateException.class)
.build(),
TestAsyncRetryOptionsChangeWorkflow.class)
.setActivityImplementations(activitiesImpl)
.build();

/** @see DeterministicRunnerTest#testRetry() */
@Test
public void testAsyncRetryOptionsChange() {
TestWorkflows.TestWorkflow2 client =
testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflows.TestWorkflow2.class);
String result = null;
try {
result = client.execute(SDKTestWorkflowRule.useExternalService);
Assert.fail("unreachable");
} catch (WorkflowException e) {
Assert.assertTrue(e.getCause() instanceof ApplicationFailure);
Assert.assertEquals(
IllegalThreadStateException.class.getName(),
((ApplicationFailure) e.getCause()).getType());
Assert.assertEquals(
"message='simulated', type='java.lang.IllegalThreadStateException', nonRetryable=false",
e.getCause().getMessage());
}
Assert.assertNull(result);
List<String> trace = client.getTrace();
Assert.assertEquals(trace.toString(), 3, trace.size());
Assert.assertEquals("started", trace.get(0));
Assert.assertTrue(trace.get(1).startsWith("retry at "));
Assert.assertTrue(trace.get(2).startsWith("retry at "));
}

public static class TestAsyncRetryOptionsChangeWorkflow implements TestWorkflows.TestWorkflow2 {

private final List<String> trace = new ArrayList<>();

@Override
public String execute(boolean useExternalService) {
RetryOptions retryOptions;
if (Workflow.isReplaying()) {
retryOptions =
RetryOptions.newBuilder()
.setMaximumInterval(Duration.ofSeconds(1))
.setInitialInterval(Duration.ofSeconds(1))
.setMaximumAttempts(3)
.build();
} else {
retryOptions =
RetryOptions.newBuilder()
.setMaximumInterval(Duration.ofSeconds(1))
.setInitialInterval(Duration.ofSeconds(1))
.setMaximumAttempts(2)
.build();
}

trace.clear(); // clear because of replay
trace.add("started");
Async.retry(
retryOptions,
Optional.of(Duration.ofSeconds(2)),
() -> {
trace.add("retry at " + Workflow.currentTimeMillis());
return Workflow.newFailedPromise(new IllegalThreadStateException("simulated"));
})
.get();
trace.add("beforeSleep");
Workflow.sleep(60000);
trace.add("done");
return "";
}

@Override
public List<String> getTrace() {
return trace;
}
}
}
107 changes: 107 additions & 0 deletions temporal-sdk/src/test/java/io/temporal/workflow/AsyncRetryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.workflow;

import io.temporal.client.WorkflowException;
import io.temporal.common.RetryOptions;
import io.temporal.failure.ApplicationFailure;
import io.temporal.internal.sync.DeterministicRunnerTest;
import io.temporal.workflow.shared.SDKTestWorkflowRule;
import io.temporal.workflow.shared.TestActivities;
import io.temporal.workflow.shared.TestWorkflows;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

public class AsyncRetryTest {

private final TestActivities.TestActivitiesImpl activitiesImpl =
new TestActivities.TestActivitiesImpl(null);

@Rule
public SDKTestWorkflowRule testWorkflowRule =
SDKTestWorkflowRule.newBuilder()
.setWorkflowTypes(TestAsyncRetryWorkflowImpl.class)
.setActivityImplementations(activitiesImpl)
.build();

/** @see DeterministicRunnerTest#testRetry() */
@Test
public void testAsyncRetry() {
TestWorkflows.TestWorkflow2 client =
testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflows.TestWorkflow2.class);
String result = null;
try {
result = client.execute(SDKTestWorkflowRule.useExternalService);
Assert.fail("unreachable");
} catch (WorkflowException e) {
Assert.assertTrue(e.getCause() instanceof ApplicationFailure);
Assert.assertEquals("test", ((ApplicationFailure) e.getCause()).getType());
Assert.assertEquals(
"message='simulated', type='test', nonRetryable=false", e.getCause().getMessage());
}
Assert.assertNull(result);
List<String> trace = client.getTrace();
Assert.assertEquals(trace.toString(), 3, trace.size());
Assert.assertEquals("started", trace.get(0));
Assert.assertTrue(trace.get(1).startsWith("retry at "));
Assert.assertTrue(trace.get(2).startsWith("retry at "));
}

public static class TestAsyncRetryWorkflowImpl implements TestWorkflows.TestWorkflow2 {

private static final RetryOptions retryOptions =
RetryOptions.newBuilder()
.setInitialInterval(Duration.ofSeconds(1))
.setMaximumInterval(Duration.ofSeconds(1))
.setBackoffCoefficient(1)
.build();

private final List<String> trace = new ArrayList<>();

@Override
public String execute(boolean useExternalService) {
trace.clear(); // clear because of replay
trace.add("started");
Async.retry(
retryOptions,
Optional.of(Duration.ofSeconds(2)),
() -> {
trace.add("retry at " + Workflow.currentTimeMillis());
return Workflow.newFailedPromise(
ApplicationFailure.newFailure("simulated", "test"));
})
.get();
trace.add("beforeSleep");
Workflow.sleep(60000);
trace.add("done");
return "";
}

@Override
public List<String> getTrace() {
return trace;
}
}
}
77 changes: 77 additions & 0 deletions temporal-sdk/src/test/java/io/temporal/workflow/AwaitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.workflow;

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

import io.temporal.workflow.shared.SDKTestWorkflowRule;
import io.temporal.workflow.shared.TestActivities;
import io.temporal.workflow.shared.TestWorkflows;
import java.time.Duration;
import org.junit.Rule;
import org.junit.Test;

public class AwaitTest {

private final TestActivities.TestActivitiesImpl activitiesImpl =
new TestActivities.TestActivitiesImpl(null);

@Rule
public SDKTestWorkflowRule testWorkflowRule =
SDKTestWorkflowRule.newBuilder()
.setWorkflowTypes(TestAwait.class)
.setActivityImplementations(activitiesImpl)
.build();

@Test
public void testAwait() {
TestWorkflows.TestWorkflow1 workflowStub =
testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflows.TestWorkflow1.class);
String result = workflowStub.execute(testWorkflowRule.getTaskQueue());
assertEquals(" awoken i=1 loop i=1 awoken i=2 loop i=2 awoken i=3", result);
}

public static class TestAwait implements TestWorkflows.TestWorkflow1 {

private int i;
private int j;

@Override
public String execute(String taskQueue) {
StringBuilder result = new StringBuilder();
Async.procedure(
() -> {
while (true) {
Workflow.await(() -> i > j);
result.append(" awoken i=" + i);
j++;
}
});

for (i = 1; i < 3; i++) {
Workflow.await(() -> j >= i);
result.append(" loop i=" + i);
}
assertFalse(Workflow.await(Duration.ZERO, () -> false));
return result.toString();
}
}
}
Loading