Skip to content

Commit 1c30adb

Browse files
authored
Flaky test: testChildWorkflowWithCronSchedule (#610)
* Added a map that records lastCompletionResult for every run to solve flakiness
1 parent 99e7103 commit 1c30adb

4 files changed

Lines changed: 21 additions & 21 deletions

File tree

temporal-sdk/src/test/java/io/temporal/workflow/WorkflowWithCronScheduleTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.temporal.workflow.shared.SDKTestWorkflowRule;
3030
import io.temporal.workflow.shared.TestWorkflowWithCronScheduleImpl;
3131
import java.time.Duration;
32+
import java.util.Map;
3233
import org.junit.Rule;
3334
import org.junit.Test;
3435
import org.junit.rules.TestName;
@@ -70,7 +71,9 @@ public void testWorkflowWithCronSchedule() {
7071
}
7172

7273
// Run 3 failed. So on run 4 we get the last completion result from run 2.
73-
assertEquals("run 2", TestWorkflowWithCronScheduleImpl.lastCompletionResult);
74+
Map<Integer, String> lastCompletionResults =
75+
TestWorkflowWithCronScheduleImpl.lastCompletionResults.get(testName.getMethodName());
76+
assertEquals("run 2", lastCompletionResults.get(4));
7477
// The last failure ought to be the one from run 3
7578
assertTrue(TestWorkflowWithCronScheduleImpl.lastFail.isPresent());
7679
assertTrue(

temporal-sdk/src/test/java/io/temporal/workflow/childWorkflowTests/ChildWorkflowWithCronScheduleTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.temporal.workflow.shared.TestWorkflows.TestWorkflow1;
3333
import io.temporal.workflow.shared.TestWorkflows.TestWorkflowWithCronSchedule;
3434
import java.time.Duration;
35+
import java.util.Map;
3536
import org.junit.Rule;
3637
import org.junit.Test;
3738
import org.junit.rules.TestName;
@@ -56,11 +57,7 @@ public void testChildWorkflowWithCronSchedule() {
5657
testWorkflowRule
5758
.getWorkflowClient()
5859
.newUntypedWorkflowStub(
59-
"TestWorkflow1",
60-
newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue())
61-
.toBuilder()
62-
.setWorkflowRunTimeout(Duration.ofHours(10))
63-
.build());
60+
"TestWorkflow1", newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue()));
6461
client.start(testName.getMethodName());
6562
testWorkflowRule.getTestEnvironment().sleep(Duration.ofHours(3));
6663
client.cancel();
@@ -73,16 +70,18 @@ public void testChildWorkflowWithCronSchedule() {
7370
}
7471

7572
// Run 3 failed. So on run 4 we get the last completion result from run 2.
76-
assertEquals("run 2", TestWorkflowWithCronScheduleImpl.lastCompletionResult);
73+
Map<Integer, String> lastCompletionResults =
74+
TestWorkflowWithCronScheduleImpl.lastCompletionResults.get(testName.getMethodName());
75+
assertEquals("run 2", lastCompletionResults.get(4));
7776
}
7877

7978
public static class TestCronParentWorkflow implements TestWorkflow1 {
8079
private final TestWorkflowWithCronSchedule cronChild =
8180
Workflow.newChildWorkflowStub(TestWorkflowWithCronSchedule.class);
8281

8382
@Override
84-
public String execute(String taskQueue) {
85-
return cronChild.execute(taskQueue);
83+
public String execute(String testName) {
84+
return cronChild.execute(testName);
8685
}
8786
}
8887
}

temporal-sdk/src/test/java/io/temporal/workflow/shared/TestWorkflowWithCronScheduleImpl.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.temporal.workflow.shared.TestWorkflows.TestWorkflowWithCronSchedule;
2626
import java.text.SimpleDateFormat;
2727
import java.util.Date;
28+
import java.util.HashMap;
2829
import java.util.Map;
2930
import java.util.Optional;
3031
import java.util.concurrent.ConcurrentHashMap;
@@ -34,7 +35,8 @@
3435
public class TestWorkflowWithCronScheduleImpl implements TestWorkflowWithCronSchedule {
3536

3637
public static final Map<String, AtomicInteger> retryCount = new ConcurrentHashMap<>();
37-
public static String lastCompletionResult;
38+
public static final Map<String, Map<Integer, String>> lastCompletionResults =
39+
new ConcurrentHashMap<>();
3840
public static Optional<Exception> lastFail;
3941

4042
@Override
@@ -46,23 +48,19 @@ public String execute(String testName) {
4648
return null;
4749
}
4850

49-
lastCompletionResult = Workflow.getLastCompletionResult(String.class);
5051
lastFail = Workflow.getPreviousRunFailure();
52+
int count = retryCount.computeIfAbsent(testName, k -> new AtomicInteger()).incrementAndGet();
53+
lastCompletionResults
54+
.computeIfAbsent(testName, k -> new HashMap<>())
55+
.put(count, Workflow.getLastCompletionResult(String.class));
5156

52-
AtomicInteger count = retryCount.get(testName);
53-
if (count == null) {
54-
count = new AtomicInteger();
55-
retryCount.put(testName, count);
56-
}
57-
int c = count.incrementAndGet();
58-
59-
if (c == 3) {
57+
if (count == 3) {
6058
throw ApplicationFailure.newFailure("simulated error", "test");
6159
}
6260

6361
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss.SSS");
6462
Date now = new Date(Workflow.currentTimeMillis());
6563
log.debug("TestWorkflowWithCronScheduleImpl run at " + sdf.format(now));
66-
return "run " + c;
64+
return "run " + count;
6765
}
6866
}

temporal-testing/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dependencies {
1212

1313
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
1414
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.5'
15-
testRuntimeOnly group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.31'
15+
testRuntimeOnly group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.32'
1616
}
1717

1818
task testServiceServer(type: CreateStartScripts) {

0 commit comments

Comments
 (0)