Skip to content

Commit 21400a7

Browse files
authored
fix bugs that cause time skipping to not work in certain cases (#162)
* fix bugs that cause time skipping to not work in certain cases * address review comments * reduce timeout for WorkflowTest
1 parent eff4f13 commit 21400a7

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

src/main/java/com/uber/cadence/internal/testservice/RequestContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,15 @@ void unlockTimer() {
106106
timerLocks--;
107107
}
108108

109-
public int getTimerLocks() {
109+
int getTimerLocks() {
110110
return timerLocks;
111111
}
112112

113+
void clearTimersAndLocks() {
114+
timerLocks = 0;
115+
timers.clear();
116+
}
117+
113118
long currentTimeInNanoseconds() {
114119
return clock.getAsLong() * NANOS_PER_MILLIS;
115120
}

src/main/java/com/uber/cadence/internal/testservice/TestWorkflowMutableStateImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void apply(RequestContext ctx)
133133
private final Map<String, StateMachine<TimerData>> timers = new HashMap<>();
134134
private final Map<String, StateMachine<SignalExternalData>> externalSignals = new HashMap<>();
135135
private StateMachine<WorkflowData> workflow;
136-
private StateMachine<DecisionTaskData> decision;
136+
private volatile StateMachine<DecisionTaskData> decision;
137137
private long lastNonFailedDecisionStartEventId;
138138
private final Map<String, CompletableFuture<QueryWorkflowResponse>> queries =
139139
new ConcurrentHashMap<>();
@@ -182,6 +182,7 @@ private void update(boolean completeDecisionUpdate, UpdateProcedure updater)
182182
if (concurrentDecision && workflow.getState() != State.TIMED_OUT) {
183183
concurrentToDecision.add(ctx);
184184
ctx.fireCallbacks(0);
185+
store.applyTimersAndLocks(ctx);
185186
} else {
186187
nextEventId = ctx.commitChanges(store);
187188
}
@@ -606,6 +607,7 @@ public void childWorklfowFailed(String activityId, ChildWorkflowExecutionFailedE
606607
child.action(StateMachines.Action.FAIL, ctx, a, 0);
607608
childWorkflows.remove(a.getInitiatedEventId());
608609
scheduleDecision(ctx);
610+
ctx.unlockTimer();
609611
});
610612
}
611613

@@ -619,6 +621,7 @@ public void childWorklfowTimedOut(
619621
child.action(Action.TIME_OUT, ctx, a.getTimeoutType(), 0);
620622
childWorkflows.remove(a.getInitiatedEventId());
621623
scheduleDecision(ctx);
624+
ctx.unlockTimer();
622625
});
623626
}
624627

@@ -646,6 +649,7 @@ public void childWorkflowCompleted(
646649
child.action(StateMachines.Action.COMPLETE, ctx, a, 0);
647650
childWorkflows.remove(a.getInitiatedEventId());
648651
scheduleDecision(ctx);
652+
ctx.unlockTimer();
649653
});
650654
}
651655

@@ -659,6 +663,7 @@ public void childWorkflowCanceled(
659663
child.action(StateMachines.Action.CANCEL, ctx, a, 0);
660664
childWorkflows.remove(a.getInitiatedEventId());
661665
scheduleDecision(ctx);
666+
ctx.unlockTimer();
662667
});
663668
}
664669

src/main/java/com/uber/cadence/internal/testservice/TestWorkflowStore.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ public PollForActivityTaskResponse getTask() {
137137

138138
long save(RequestContext requestContext) throws InternalServiceError, EntityNotExistsError;
139139

140+
void applyTimersAndLocks(RequestContext ctx);
141+
140142
void registerDelayedCallback(Duration delay, Runnable r);
141143

142144
PollForDecisionTaskResponse pollForDecisionTask(PollForDecisionTaskRequest pollRequest)

src/main/java/com/uber/cadence/internal/testservice/TestWorkflowStoreImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,25 @@ public long save(RequestContext ctx) throws InternalServiceError, EntityNotExist
219219
return result;
220220
}
221221

222+
@Override
223+
public void applyTimersAndLocks(RequestContext ctx) {
224+
lock.lock();
225+
try {
226+
timerService.updateLocks(ctx.getTimerLocks());
227+
} finally {
228+
lock.unlock();
229+
}
230+
231+
List<Timer> timers = ctx.getTimers();
232+
if (timers != null) {
233+
for (Timer t : timers) {
234+
timerService.schedule(Duration.ofSeconds(t.getDelaySeconds()), t.getCallback());
235+
}
236+
}
237+
238+
ctx.clearTimersAndLocks();
239+
}
240+
222241
@Override
223242
public void registerDelayedCallback(Duration delay, Runnable r) {
224243
timerService.schedule(delay, r);

src/test/java/com/uber/cadence/workflow/WorkflowTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static Object[] data() {
123123

124124
@Rule
125125
public Timeout globalTimeout =
126-
Timeout.seconds(DEBUGGER_TIMEOUTS ? 500 : (skipDockerService ? 5 : 20));
126+
Timeout.seconds(DEBUGGER_TIMEOUTS ? 500 : (skipDockerService ? 3 : 20));
127127

128128
@Rule
129129
public TestWatcher watchman =

0 commit comments

Comments
 (0)