Skip to content

Commit 46f9e19

Browse files
committed
chore(worker): add unit test for decision iterator
Signed-off-by: Shijie Sheng <[email protected]>
1 parent a623429 commit 46f9e19

File tree

1 file changed

+310
-0
lines changed

1 file changed

+310
-0
lines changed
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.internal.replay;
19+
20+
import static org.junit.Assert.*;
21+
import static org.mockito.Mockito.*;
22+
23+
import com.uber.cadence.EventType;
24+
import com.uber.cadence.HistoryEvent;
25+
import com.uber.cadence.MarkerRecordedEventAttributes;
26+
import com.uber.cadence.internal.worker.DecisionTaskWithHistoryIterator;
27+
import java.util.Arrays;
28+
import java.util.Collections;
29+
import java.util.List;
30+
import java.util.concurrent.TimeUnit;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.mockito.Mock;
35+
import org.mockito.junit.MockitoJUnitRunner;
36+
37+
@RunWith(MockitoJUnitRunner.class)
38+
public class DecisionEventsIteratorTest {
39+
40+
@Mock private DecisionTaskWithHistoryIterator mockDecisionTaskWithHistoryIterator;
41+
42+
private static final long REPLAY_TIME_MILLIS = 1000L;
43+
private static final long EVENT_TIMESTAMP_NANOS =
44+
TimeUnit.MILLISECONDS.toNanos(REPLAY_TIME_MILLIS);
45+
46+
@Before
47+
public void setUp() {
48+
// Default setup - can be overridden in individual tests
49+
}
50+
51+
@Test
52+
public void testConstructor() {
53+
// Arrange
54+
List<HistoryEvent> events = Collections.emptyList();
55+
when(mockDecisionTaskWithHistoryIterator.getHistory()).thenReturn(events.iterator());
56+
57+
// Act
58+
HistoryHelper.DecisionEventsIterator iterator =
59+
new HistoryHelper.DecisionEventsIterator(
60+
mockDecisionTaskWithHistoryIterator, REPLAY_TIME_MILLIS);
61+
62+
// Assert
63+
assertNotNull(iterator);
64+
assertFalse(iterator.hasNext());
65+
}
66+
67+
@Test
68+
public void testHasNextWithEmptyHistory() {
69+
// Arrange
70+
List<HistoryEvent> events = Collections.emptyList();
71+
when(mockDecisionTaskWithHistoryIterator.getHistory()).thenReturn(events.iterator());
72+
73+
// Act
74+
HistoryHelper.DecisionEventsIterator iterator =
75+
new HistoryHelper.DecisionEventsIterator(
76+
mockDecisionTaskWithHistoryIterator, REPLAY_TIME_MILLIS);
77+
78+
// Assert
79+
assertFalse(iterator.hasNext());
80+
}
81+
82+
@Test
83+
public void testWorkflowStartedOnly() {
84+
// Arrange
85+
List<HistoryEvent> events =
86+
Arrays.asList(
87+
createHistoryEvent(1, EventType.WorkflowExecutionStarted, EVENT_TIMESTAMP_NANOS),
88+
createHistoryEvent(2, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
89+
createHistoryEvent(3, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS));
90+
when(mockDecisionTaskWithHistoryIterator.getHistory()).thenReturn(events.iterator());
91+
92+
// Act
93+
HistoryHelper.DecisionEventsIterator iterator =
94+
new HistoryHelper.DecisionEventsIterator(
95+
mockDecisionTaskWithHistoryIterator, REPLAY_TIME_MILLIS);
96+
97+
// Assert
98+
assertTrue(iterator.hasNext());
99+
100+
HistoryHelper.DecisionEvents decisionEvents = iterator.next();
101+
assertNotNull(decisionEvents);
102+
assertEquals(2, decisionEvents.getEvents().size());
103+
assertEquals(0, decisionEvents.getDecisionEvents().size());
104+
assertFalse(decisionEvents.isReplay());
105+
assertEquals(REPLAY_TIME_MILLIS, decisionEvents.getReplayCurrentTimeMilliseconds());
106+
assertEquals(5, decisionEvents.getNextDecisionEventId());
107+
}
108+
109+
@Test
110+
public void testWorkflowWithActivityScheduled() {
111+
// Arrange
112+
List<HistoryEvent> events =
113+
Arrays.asList(
114+
createHistoryEvent(1, EventType.WorkflowExecutionStarted, EVENT_TIMESTAMP_NANOS),
115+
createHistoryEvent(2, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
116+
createHistoryEvent(3, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS),
117+
createHistoryEvent(4, EventType.DecisionTaskCompleted, EVENT_TIMESTAMP_NANOS),
118+
createHistoryEvent(5, EventType.ActivityTaskScheduled, EVENT_TIMESTAMP_NANOS));
119+
when(mockDecisionTaskWithHistoryIterator.getHistory()).thenReturn(events.iterator());
120+
121+
// Act
122+
HistoryHelper.DecisionEventsIterator iterator =
123+
new HistoryHelper.DecisionEventsIterator(
124+
mockDecisionTaskWithHistoryIterator, REPLAY_TIME_MILLIS);
125+
HistoryHelper.DecisionEvents decisionEvents = iterator.next();
126+
127+
// Assert
128+
assertNotNull(decisionEvents);
129+
assertEquals(
130+
2, decisionEvents.getEvents().size()); // WorkflowExecutionStarted and DecisionTaskScheduled
131+
assertEquals(1, decisionEvents.getDecisionEvents().size()); // ActivityTaskScheduled
132+
assertTrue(decisionEvents.isReplay());
133+
assertEquals(REPLAY_TIME_MILLIS, decisionEvents.getReplayCurrentTimeMilliseconds());
134+
assertEquals(5, decisionEvents.getNextDecisionEventId());
135+
}
136+
137+
@Test
138+
public void testWorkflowWithActivityCompleted() {
139+
// Arrange - Sticky workers scenario
140+
List<HistoryEvent> events =
141+
Arrays.asList(
142+
createHistoryEvent(1, EventType.WorkflowExecutionStarted, EVENT_TIMESTAMP_NANOS),
143+
createHistoryEvent(2, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
144+
createHistoryEvent(3, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS),
145+
createHistoryEvent(4, EventType.DecisionTaskCompleted, EVENT_TIMESTAMP_NANOS),
146+
createHistoryEvent(5, EventType.ActivityTaskScheduled, EVENT_TIMESTAMP_NANOS),
147+
createHistoryEvent(6, EventType.ActivityTaskStarted, EVENT_TIMESTAMP_NANOS),
148+
createHistoryEvent(7, EventType.ActivityTaskCompleted, EVENT_TIMESTAMP_NANOS),
149+
createHistoryEvent(8, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
150+
createHistoryEvent(9, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS));
151+
when(mockDecisionTaskWithHistoryIterator.getHistory()).thenReturn(events.iterator());
152+
153+
// Act
154+
HistoryHelper.DecisionEventsIterator iterator =
155+
new HistoryHelper.DecisionEventsIterator(
156+
mockDecisionTaskWithHistoryIterator, REPLAY_TIME_MILLIS);
157+
158+
159+
// first decision batch
160+
HistoryHelper.DecisionEvents decisionEvents = iterator.next();
161+
assertNotNull(decisionEvents);
162+
assertEquals(2, decisionEvents.getEvents().size());
163+
assertEquals(1, decisionEvents.getDecisionEvents().size());
164+
assertTrue(decisionEvents.isReplay());
165+
assertEquals(5, decisionEvents.getNextDecisionEventId());
166+
167+
// second decision batch
168+
decisionEvents = iterator.next();
169+
assertFalse(iterator.hasNext());
170+
assertEquals(3, decisionEvents.getEvents().size());
171+
assertEquals(0, decisionEvents.getDecisionEvents().size());
172+
assertFalse(decisionEvents.isReplay());
173+
assertEquals(REPLAY_TIME_MILLIS, decisionEvents.getReplayCurrentTimeMilliseconds());
174+
assertEquals(11, decisionEvents.getNextDecisionEventId());
175+
}
176+
177+
@Test
178+
public void testCompletedWorkflow() {
179+
// Arrange - Non-replay scenario
180+
List<HistoryEvent> events =
181+
Arrays.asList(
182+
createHistoryEvent(1, EventType.WorkflowExecutionStarted, EVENT_TIMESTAMP_NANOS),
183+
createHistoryEvent(2, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
184+
createHistoryEvent(3, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS),
185+
createHistoryEvent(4, EventType.DecisionTaskCompleted, EVENT_TIMESTAMP_NANOS),
186+
createHistoryEvent(5, EventType.ActivityTaskScheduled, EVENT_TIMESTAMP_NANOS),
187+
createHistoryEvent(6, EventType.ActivityTaskStarted, EVENT_TIMESTAMP_NANOS),
188+
createHistoryEvent(7, EventType.ActivityTaskCompleted, EVENT_TIMESTAMP_NANOS),
189+
createHistoryEvent(8, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
190+
createHistoryEvent(9, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS),
191+
createHistoryEvent(10, EventType.DecisionTaskCompleted, EVENT_TIMESTAMP_NANOS),
192+
createHistoryEvent(11, EventType.WorkflowExecutionCompleted, EVENT_TIMESTAMP_NANOS));
193+
when(mockDecisionTaskWithHistoryIterator.getHistory()).thenReturn(events.iterator());
194+
195+
// Act
196+
HistoryHelper.DecisionEventsIterator iterator =
197+
new HistoryHelper.DecisionEventsIterator(
198+
mockDecisionTaskWithHistoryIterator, REPLAY_TIME_MILLIS);
199+
200+
// first decision batch
201+
HistoryHelper.DecisionEvents decisionEvents = iterator.next();
202+
assertNotNull(decisionEvents);
203+
assertEquals(2, decisionEvents.getEvents().size());
204+
assertEquals(1, decisionEvents.getDecisionEvents().size());
205+
assertTrue(decisionEvents.isReplay());
206+
assertEquals(5, decisionEvents.getNextDecisionEventId());
207+
208+
// second decision batch
209+
decisionEvents = iterator.next();
210+
assertFalse(iterator.hasNext());
211+
assertEquals(3, decisionEvents.getEvents().size());
212+
assertEquals(1, decisionEvents.getDecisionEvents().size());
213+
assertTrue(decisionEvents.isReplay());
214+
assertEquals(REPLAY_TIME_MILLIS, decisionEvents.getReplayCurrentTimeMilliseconds());
215+
assertEquals(11, decisionEvents.getNextDecisionEventId());
216+
}
217+
218+
@Test
219+
public void testNextWithDecisionTaskTimedOut() {
220+
// Arrange
221+
List<HistoryEvent> events =
222+
Arrays.asList(
223+
createHistoryEvent(1, EventType.WorkflowExecutionStarted, EVENT_TIMESTAMP_NANOS),
224+
createHistoryEvent(2, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
225+
createHistoryEvent(3, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS),
226+
createHistoryEvent(4, EventType.DecisionTaskTimedOut, EVENT_TIMESTAMP_NANOS),
227+
createHistoryEvent(5, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
228+
createHistoryEvent(6, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS),
229+
createHistoryEvent(7, EventType.DecisionTaskCompleted, EVENT_TIMESTAMP_NANOS));
230+
when(mockDecisionTaskWithHistoryIterator.getHistory()).thenReturn(events.iterator());
231+
232+
// Act
233+
HistoryHelper.DecisionEventsIterator iterator =
234+
new HistoryHelper.DecisionEventsIterator(
235+
mockDecisionTaskWithHistoryIterator, REPLAY_TIME_MILLIS);
236+
HistoryHelper.DecisionEvents decisionEvents = iterator.next();
237+
238+
// Assert
239+
assertNotNull(decisionEvents);
240+
assertEquals(
241+
4, decisionEvents.getEvents().size()); // All events before the second DecisionTaskStarted
242+
assertEquals(0, decisionEvents.getDecisionEvents().size());
243+
assertTrue(decisionEvents.isReplay());
244+
assertEquals(8, decisionEvents.getNextDecisionEventId());
245+
}
246+
247+
@Test
248+
public void testNextWithDecisionTaskFailed() {
249+
// Arrange
250+
List<HistoryEvent> events =
251+
Arrays.asList(
252+
createHistoryEvent(1, EventType.WorkflowExecutionStarted, EVENT_TIMESTAMP_NANOS),
253+
createHistoryEvent(2, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
254+
createHistoryEvent(3, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS),
255+
createHistoryEvent(4, EventType.DecisionTaskFailed, EVENT_TIMESTAMP_NANOS),
256+
createHistoryEvent(5, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
257+
createHistoryEvent(6, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS),
258+
createHistoryEvent(7, EventType.DecisionTaskCompleted, EVENT_TIMESTAMP_NANOS));
259+
when(mockDecisionTaskWithHistoryIterator.getHistory()).thenReturn(events.iterator());
260+
261+
// Act
262+
HistoryHelper.DecisionEventsIterator iterator =
263+
new HistoryHelper.DecisionEventsIterator(
264+
mockDecisionTaskWithHistoryIterator, REPLAY_TIME_MILLIS);
265+
HistoryHelper.DecisionEvents decisionEvents = iterator.next();
266+
267+
// Assert
268+
assertNotNull(decisionEvents);
269+
assertEquals(4, decisionEvents.getEvents().size());
270+
assertEquals(0, decisionEvents.getDecisionEvents().size());
271+
assertTrue(decisionEvents.isReplay());
272+
assertEquals(8, decisionEvents.getNextDecisionEventId());
273+
}
274+
275+
@Test
276+
public void testNextWithMarkerRecordedEvents() {
277+
// Arrange
278+
HistoryEvent markerEvent =
279+
createHistoryEvent(5, EventType.MarkerRecorded, EVENT_TIMESTAMP_NANOS);
280+
markerEvent.setMarkerRecordedEventAttributes(new MarkerRecordedEventAttributes());
281+
282+
List<HistoryEvent> events =
283+
Arrays.asList(
284+
createHistoryEvent(1, EventType.WorkflowExecutionStarted, EVENT_TIMESTAMP_NANOS),
285+
createHistoryEvent(2, EventType.DecisionTaskScheduled, EVENT_TIMESTAMP_NANOS),
286+
createHistoryEvent(3, EventType.DecisionTaskStarted, EVENT_TIMESTAMP_NANOS),
287+
createHistoryEvent(4, EventType.DecisionTaskCompleted, EVENT_TIMESTAMP_NANOS),
288+
markerEvent);
289+
when(mockDecisionTaskWithHistoryIterator.getHistory()).thenReturn(events.iterator());
290+
291+
// Act
292+
HistoryHelper.DecisionEventsIterator iterator =
293+
new HistoryHelper.DecisionEventsIterator(
294+
mockDecisionTaskWithHistoryIterator, REPLAY_TIME_MILLIS);
295+
HistoryHelper.DecisionEvents decisionEvents = iterator.next();
296+
297+
// Assert
298+
assertNotNull(decisionEvents);
299+
assertEquals(1, decisionEvents.getMarkers().size());
300+
assertEquals(markerEvent, decisionEvents.getMarkers().get(0));
301+
}
302+
303+
private HistoryEvent createHistoryEvent(long eventId, EventType eventType, long timestamp) {
304+
HistoryEvent event = new HistoryEvent();
305+
event.setEventId(eventId);
306+
event.setEventType(eventType);
307+
event.setTimestamp(timestamp);
308+
return event;
309+
}
310+
}

0 commit comments

Comments
 (0)