Skip to content

Commit 39f8ec2

Browse files
committed
add test for workflowPollTask
1 parent 763527f commit 39f8ec2

File tree

1 file changed

+196
-1
lines changed

1 file changed

+196
-1
lines changed

src/test/java/com/uber/cadence/internal/worker/WorkflowPollTaskTest.java

Lines changed: 196 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,199 @@
1414
*/
1515
package com.uber.cadence.internal.worker;
1616

17-
public class WorkflowPollTaskTest {}
17+
import static com.uber.cadence.internal.metrics.MetricsTagValue.INTERNAL_SERVICE_ERROR;
18+
import static com.uber.cadence.internal.metrics.MetricsTagValue.SERVICE_BUSY;
19+
import static org.junit.Assert.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import com.google.common.collect.ImmutableMap;
23+
import com.uber.cadence.*;
24+
import com.uber.cadence.internal.metrics.MetricsTag;
25+
import com.uber.cadence.internal.metrics.MetricsType;
26+
import com.uber.cadence.serviceclient.IWorkflowService;
27+
import com.uber.m3.tally.Counter;
28+
import com.uber.m3.tally.Scope;
29+
import com.uber.m3.tally.Stopwatch;
30+
import com.uber.m3.tally.Timer;
31+
import com.uber.m3.util.Duration;
32+
import org.apache.thrift.TException;
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
36+
public class WorkflowPollTaskTest {
37+
38+
private IWorkflowService mockService;
39+
private Scope mockMetricScope;
40+
private WorkflowPollTask pollTask;
41+
42+
@Before
43+
public void setup() {
44+
mockService = mock(IWorkflowService.class);
45+
mockMetricScope = mock(Scope.class);
46+
47+
// Mock the Timer and Stopwatch
48+
Timer pollLatencyTimer = mock(Timer.class);
49+
Timer scheduledToStartLatencyTimer = mock(Timer.class);
50+
Stopwatch sw = mock(Stopwatch.class);
51+
52+
// Ensure timers and stopwatch are not null and return expected values
53+
when(mockMetricScope.timer(MetricsType.DECISION_POLL_LATENCY)).thenReturn(pollLatencyTimer);
54+
when(pollLatencyTimer.start()).thenReturn(sw);
55+
when(mockMetricScope.timer(MetricsType.DECISION_SCHEDULED_TO_START_LATENCY))
56+
.thenReturn(scheduledToStartLatencyTimer);
57+
doNothing().when(scheduledToStartLatencyTimer).record(any(Duration.class));
58+
59+
// Mock counters for different metrics
60+
Counter pollCounter = mock(Counter.class);
61+
Counter succeedCounter = mock(Counter.class);
62+
Counter noTaskCounter = mock(Counter.class);
63+
Counter failedCounter = mock(Counter.class);
64+
Counter transientFailedCounter = mock(Counter.class);
65+
66+
// Set up mockMetricScope to return these counters for specific metrics
67+
when(mockMetricScope.counter(MetricsType.DECISION_POLL_COUNTER)).thenReturn(pollCounter);
68+
when(mockMetricScope.counter(MetricsType.DECISION_POLL_NO_TASK_COUNTER))
69+
.thenReturn(noTaskCounter);
70+
when(mockMetricScope.counter(MetricsType.DECISION_POLL_FAILED_COUNTER))
71+
.thenReturn(failedCounter);
72+
when(mockMetricScope.counter(MetricsType.DECISION_POLL_TRANSIENT_FAILED_COUNTER))
73+
.thenReturn(transientFailedCounter);
74+
when(mockMetricScope.counter(MetricsType.DECISION_POLL_SUCCEED_COUNTER))
75+
.thenReturn(succeedCounter);
76+
77+
// Initialize pollTask with the mocked dependencies
78+
pollTask =
79+
new WorkflowPollTask(
80+
mockService,
81+
"test-domain",
82+
"test-taskList",
83+
TaskListKind.TASK_LIST_KIND_NORMAL,
84+
mockMetricScope,
85+
"test-identity");
86+
}
87+
88+
@Test
89+
public void testPollSuccess() throws TException {
90+
// Mock a successful response with all necessary fields
91+
WorkflowType workflowType = new WorkflowType().setName("testWorkflowType");
92+
93+
PollForDecisionTaskResponse response =
94+
new PollForDecisionTaskResponse()
95+
.setTaskToken("testToken".getBytes())
96+
.setWorkflowType(workflowType)
97+
.setScheduledTimestamp(1000L) // Ensure ScheduledTimestamp is non-null
98+
.setStartedTimestamp(2000L); // Ensure StartedTimestamp is non-null
99+
100+
when(mockService.PollForDecisionTask(any(PollForDecisionTaskRequest.class)))
101+
.thenReturn(response);
102+
103+
// Mock the timer and stopwatch behavior
104+
Timer pollLatencyTimer = mock(Timer.class);
105+
Timer scheduledToStartLatencyTimer = mock(Timer.class);
106+
Stopwatch sw = mock(Stopwatch.class);
107+
when(mockMetricScope.timer(MetricsType.DECISION_POLL_LATENCY)).thenReturn(pollLatencyTimer);
108+
when(pollLatencyTimer.start()).thenReturn(sw);
109+
110+
// Mock the tagged scope for workflow type
111+
Scope taggedScope = mock(Scope.class);
112+
when(mockMetricScope.tagged(ImmutableMap.of(MetricsTag.WORKFLOW_TYPE, "testWorkflowType")))
113+
.thenReturn(taggedScope);
114+
115+
// Ensure DECISION_SCHEDULED_TO_START_LATENCY timer in taggedScope is not null
116+
when(taggedScope.timer(MetricsType.DECISION_SCHEDULED_TO_START_LATENCY))
117+
.thenReturn(scheduledToStartLatencyTimer);
118+
doNothing().when(scheduledToStartLatencyTimer).record(any(Duration.class));
119+
120+
// Mock counters for DECISION_POLL_COUNTER and DECISION_POLL_SUCCEED_COUNTER
121+
Counter pollCounter = mock(Counter.class);
122+
Counter succeedCounter = mock(Counter.class);
123+
when(mockMetricScope.counter(MetricsType.DECISION_POLL_COUNTER)).thenReturn(pollCounter);
124+
when(taggedScope.counter(MetricsType.DECISION_POLL_SUCCEED_COUNTER)).thenReturn(succeedCounter);
125+
126+
PollForDecisionTaskResponse result = pollTask.poll();
127+
128+
// Verify that the result is not null and task token is as expected
129+
assertNotNull(result);
130+
assertArrayEquals("testToken".getBytes(), result.getTaskToken());
131+
132+
// Verify counter and timer behavior
133+
verify(pollCounter, times(1)).inc(1);
134+
verify(succeedCounter, times(1)).inc(1);
135+
verify(pollLatencyTimer, times(1)).start();
136+
verify(sw, times(1)).stop();
137+
138+
// Verify that record() on scheduledToStartLatencyTimer was called with correct duration
139+
Duration expectedDuration =
140+
Duration.ofNanos(result.getStartedTimestamp() - result.getScheduledTimestamp());
141+
verify(scheduledToStartLatencyTimer, times(1)).record(eq(expectedDuration));
142+
}
143+
144+
@Test(expected = InternalServiceError.class)
145+
public void testPollInternalServiceError() throws TException {
146+
when(mockService.PollForDecisionTask(any(PollForDecisionTaskRequest.class)))
147+
.thenThrow(new InternalServiceError());
148+
149+
Scope taggedScope = mock(Scope.class);
150+
Counter taggedCounter = mock(Counter.class);
151+
when(mockMetricScope.tagged(ImmutableMap.of(MetricsTag.CAUSE, INTERNAL_SERVICE_ERROR)))
152+
.thenReturn(taggedScope);
153+
when(taggedScope.counter(MetricsType.DECISION_POLL_TRANSIENT_FAILED_COUNTER))
154+
.thenReturn(taggedCounter);
155+
156+
try {
157+
pollTask.poll();
158+
} finally {
159+
verify(taggedCounter, times(1)).inc(1);
160+
}
161+
}
162+
163+
@Test(expected = ServiceBusyError.class)
164+
public void testPollServiceBusyError() throws TException {
165+
when(mockService.PollForDecisionTask(any(PollForDecisionTaskRequest.class)))
166+
.thenThrow(new ServiceBusyError());
167+
168+
Scope taggedScope = mock(Scope.class);
169+
Counter taggedCounter = mock(Counter.class);
170+
when(mockMetricScope.tagged(ImmutableMap.of(MetricsTag.CAUSE, SERVICE_BUSY)))
171+
.thenReturn(taggedScope);
172+
when(taggedScope.counter(MetricsType.DECISION_POLL_TRANSIENT_FAILED_COUNTER))
173+
.thenReturn(taggedCounter);
174+
175+
try {
176+
pollTask.poll();
177+
} finally {
178+
verify(taggedCounter, times(1)).inc(1);
179+
}
180+
}
181+
182+
@Test(expected = TException.class)
183+
public void testPollGeneralTException() throws TException {
184+
when(mockService.PollForDecisionTask(any(PollForDecisionTaskRequest.class)))
185+
.thenThrow(new TException());
186+
187+
Counter failedCounter = mock(Counter.class);
188+
when(mockMetricScope.counter(MetricsType.DECISION_POLL_FAILED_COUNTER))
189+
.thenReturn(failedCounter);
190+
191+
try {
192+
pollTask.poll();
193+
} finally {
194+
verify(failedCounter, times(1)).inc(1);
195+
}
196+
}
197+
198+
@Test
199+
public void testPollNoTask() throws TException {
200+
when(mockService.PollForDecisionTask(any(PollForDecisionTaskRequest.class)))
201+
.thenReturn(new PollForDecisionTaskResponse());
202+
203+
Counter noTaskCounter = mock(Counter.class);
204+
when(mockMetricScope.counter(MetricsType.DECISION_POLL_NO_TASK_COUNTER))
205+
.thenReturn(noTaskCounter);
206+
207+
PollForDecisionTaskResponse result = pollTask.poll();
208+
209+
assertNull(result);
210+
verify(noTaskCounter, times(1)).inc(1);
211+
}
212+
}

0 commit comments

Comments
 (0)