Skip to content

Commit 42e3e2e

Browse files
authored
Added tests for HistoryEventToString (#1383)
What changed? Added tests for HistoryEventToString. Seperated ot the getData to make testing simpler Why? Improve code coverage How did you test it? Potential risks
1 parent 2801925 commit 42e3e2e

File tree

2 files changed

+239
-26
lines changed

2 files changed

+239
-26
lines changed

internal/common/util/stringer.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,79 +78,82 @@ func valueToString(v reflect.Value) string {
7878

7979
// HistoryEventToString convert HistoryEvent to string
8080
func HistoryEventToString(e *s.HistoryEvent) string {
81-
var data interface{}
81+
data := getData(e)
82+
83+
return e.GetEventType().String() + ": " + anyToString(data)
84+
}
85+
86+
func getData(e *s.HistoryEvent) interface{} {
8287
switch e.GetEventType() {
8388
case s.EventTypeWorkflowExecutionStarted:
84-
data = e.WorkflowExecutionStartedEventAttributes
89+
return e.WorkflowExecutionStartedEventAttributes
8590

8691
case s.EventTypeWorkflowExecutionCompleted:
87-
data = e.WorkflowExecutionCompletedEventAttributes
92+
return e.WorkflowExecutionCompletedEventAttributes
8893

8994
case s.EventTypeWorkflowExecutionFailed:
90-
data = e.WorkflowExecutionFailedEventAttributes
95+
return e.WorkflowExecutionFailedEventAttributes
9196

9297
case s.EventTypeWorkflowExecutionTimedOut:
93-
data = e.WorkflowExecutionTimedOutEventAttributes
98+
return e.WorkflowExecutionTimedOutEventAttributes
9499

95100
case s.EventTypeDecisionTaskScheduled:
96-
data = e.DecisionTaskScheduledEventAttributes
101+
return e.DecisionTaskScheduledEventAttributes
97102

98103
case s.EventTypeDecisionTaskStarted:
99-
data = e.DecisionTaskStartedEventAttributes
104+
return e.DecisionTaskStartedEventAttributes
100105

101106
case s.EventTypeDecisionTaskCompleted:
102-
data = e.DecisionTaskCompletedEventAttributes
107+
return e.DecisionTaskCompletedEventAttributes
103108

104109
case s.EventTypeDecisionTaskTimedOut:
105-
data = e.DecisionTaskTimedOutEventAttributes
110+
return e.DecisionTaskTimedOutEventAttributes
106111

107112
case s.EventTypeActivityTaskScheduled:
108-
data = e.ActivityTaskScheduledEventAttributes
113+
return e.ActivityTaskScheduledEventAttributes
109114

110115
case s.EventTypeActivityTaskStarted:
111-
data = e.ActivityTaskStartedEventAttributes
116+
return e.ActivityTaskStartedEventAttributes
112117

113118
case s.EventTypeActivityTaskCompleted:
114-
data = e.ActivityTaskCompletedEventAttributes
119+
return e.ActivityTaskCompletedEventAttributes
115120

116121
case s.EventTypeActivityTaskFailed:
117-
data = e.ActivityTaskFailedEventAttributes
122+
return e.ActivityTaskFailedEventAttributes
118123

119124
case s.EventTypeActivityTaskTimedOut:
120-
data = e.ActivityTaskTimedOutEventAttributes
125+
return e.ActivityTaskTimedOutEventAttributes
121126

122127
case s.EventTypeActivityTaskCancelRequested:
123-
data = e.ActivityTaskCancelRequestedEventAttributes
128+
return e.ActivityTaskCancelRequestedEventAttributes
124129

125130
case s.EventTypeRequestCancelActivityTaskFailed:
126-
data = e.RequestCancelActivityTaskFailedEventAttributes
131+
return e.RequestCancelActivityTaskFailedEventAttributes
127132

128133
case s.EventTypeActivityTaskCanceled:
129-
data = e.ActivityTaskCanceledEventAttributes
134+
return e.ActivityTaskCanceledEventAttributes
130135

131136
case s.EventTypeTimerStarted:
132-
data = e.TimerStartedEventAttributes
137+
return e.TimerStartedEventAttributes
133138

134139
case s.EventTypeTimerFired:
135-
data = e.TimerFiredEventAttributes
140+
return e.TimerFiredEventAttributes
136141

137142
case s.EventTypeCancelTimerFailed:
138-
data = e.CancelTimerFailedEventAttributes
143+
return e.CancelTimerFailedEventAttributes
139144

140145
case s.EventTypeTimerCanceled:
141-
data = e.TimerCanceledEventAttributes
146+
return e.TimerCanceledEventAttributes
142147

143148
case s.EventTypeMarkerRecorded:
144-
data = e.MarkerRecordedEventAttributes
149+
return e.MarkerRecordedEventAttributes
145150

146151
case s.EventTypeWorkflowExecutionTerminated:
147-
data = e.WorkflowExecutionTerminatedEventAttributes
152+
return e.WorkflowExecutionTerminatedEventAttributes
148153

149154
default:
150-
data = e
155+
return e
151156
}
152-
153-
return e.GetEventType().String() + ": " + anyToString(data)
154157
}
155158

156159
// DecisionToString convert Decision to string

internal/common/util/stringer_test.go

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import (
2424
"reflect"
2525
"testing"
2626

27+
"github.com/stretchr/testify/assert"
2728
"github.com/stretchr/testify/require"
29+
"go.uber.org/thriftrw/ptr"
30+
31+
s "go.uber.org/cadence/.gen/go/shared"
2832
)
2933

3034
func Test_anyToString(t *testing.T) {
@@ -114,3 +118,209 @@ func Test_byteSliceToString(t *testing.T) {
114118

115119
require.Equal(t, "[len=3]", strVal2)
116120
}
121+
122+
func TestHistoryEventToString(t *testing.T) {
123+
event := &s.HistoryEvent{
124+
EventType: toPtr(s.EventTypeWorkflowExecutionStarted),
125+
WorkflowExecutionStartedEventAttributes: &s.WorkflowExecutionStartedEventAttributes{
126+
WorkflowType: &s.WorkflowType{
127+
Name: toPtr("test-workflow"),
128+
},
129+
TaskList: &s.TaskList{
130+
Name: toPtr("task-list"),
131+
},
132+
ExecutionStartToCloseTimeoutSeconds: ptr.Int32(60),
133+
},
134+
}
135+
136+
strVal := HistoryEventToString(event)
137+
138+
expected := `WorkflowExecutionStarted: (WorkflowType:(Name:test-workflow), TaskList:(Name:task-list), Input:[], ExecutionStartToCloseTimeoutSeconds:60, ContinuedFailureDetails:[], LastCompletionResult:[], PartitionConfig:map[])`
139+
140+
assert.Equal(t, expected, strVal)
141+
}
142+
143+
// This just tests that we pick the right attibutes to return
144+
// the other attributes will be nil
145+
func Test_getData(t *testing.T) {
146+
cases := []struct {
147+
event *s.HistoryEvent
148+
expected interface{}
149+
}{
150+
{
151+
event: &s.HistoryEvent{
152+
EventType: toPtr(s.EventTypeWorkflowExecutionStarted),
153+
WorkflowExecutionStartedEventAttributes: &s.WorkflowExecutionStartedEventAttributes{},
154+
},
155+
expected: &s.WorkflowExecutionStartedEventAttributes{},
156+
},
157+
{
158+
event: &s.HistoryEvent{
159+
EventType: toPtr(s.EventTypeWorkflowExecutionCompleted),
160+
WorkflowExecutionCompletedEventAttributes: &s.WorkflowExecutionCompletedEventAttributes{},
161+
},
162+
expected: &s.WorkflowExecutionCompletedEventAttributes{},
163+
},
164+
{
165+
event: &s.HistoryEvent{
166+
EventType: toPtr(s.EventTypeWorkflowExecutionFailed),
167+
WorkflowExecutionFailedEventAttributes: &s.WorkflowExecutionFailedEventAttributes{},
168+
},
169+
expected: &s.WorkflowExecutionFailedEventAttributes{},
170+
},
171+
{
172+
event: &s.HistoryEvent{
173+
EventType: toPtr(s.EventTypeWorkflowExecutionTimedOut),
174+
WorkflowExecutionTimedOutEventAttributes: &s.WorkflowExecutionTimedOutEventAttributes{},
175+
},
176+
expected: &s.WorkflowExecutionTimedOutEventAttributes{},
177+
},
178+
{
179+
event: &s.HistoryEvent{
180+
EventType: toPtr(s.EventTypeDecisionTaskScheduled),
181+
DecisionTaskScheduledEventAttributes: &s.DecisionTaskScheduledEventAttributes{},
182+
},
183+
expected: &s.DecisionTaskScheduledEventAttributes{},
184+
},
185+
{
186+
event: &s.HistoryEvent{
187+
EventType: toPtr(s.EventTypeDecisionTaskStarted),
188+
DecisionTaskStartedEventAttributes: &s.DecisionTaskStartedEventAttributes{},
189+
},
190+
expected: &s.DecisionTaskStartedEventAttributes{},
191+
},
192+
{
193+
event: &s.HistoryEvent{
194+
EventType: toPtr(s.EventTypeDecisionTaskCompleted),
195+
DecisionTaskCompletedEventAttributes: &s.DecisionTaskCompletedEventAttributes{},
196+
},
197+
expected: &s.DecisionTaskCompletedEventAttributes{},
198+
},
199+
{
200+
event: &s.HistoryEvent{
201+
EventType: toPtr(s.EventTypeDecisionTaskTimedOut),
202+
DecisionTaskTimedOutEventAttributes: &s.DecisionTaskTimedOutEventAttributes{},
203+
},
204+
expected: &s.DecisionTaskTimedOutEventAttributes{},
205+
},
206+
{
207+
event: &s.HistoryEvent{
208+
EventType: toPtr(s.EventTypeActivityTaskScheduled),
209+
ActivityTaskScheduledEventAttributes: &s.ActivityTaskScheduledEventAttributes{},
210+
},
211+
expected: &s.ActivityTaskScheduledEventAttributes{},
212+
},
213+
{
214+
event: &s.HistoryEvent{
215+
EventType: toPtr(s.EventTypeActivityTaskStarted),
216+
ActivityTaskStartedEventAttributes: &s.ActivityTaskStartedEventAttributes{},
217+
},
218+
expected: &s.ActivityTaskStartedEventAttributes{},
219+
},
220+
{
221+
event: &s.HistoryEvent{
222+
EventType: toPtr(s.EventTypeActivityTaskCompleted),
223+
ActivityTaskCompletedEventAttributes: &s.ActivityTaskCompletedEventAttributes{},
224+
},
225+
expected: &s.ActivityTaskCompletedEventAttributes{},
226+
},
227+
{
228+
event: &s.HistoryEvent{
229+
EventType: toPtr(s.EventTypeActivityTaskFailed),
230+
ActivityTaskFailedEventAttributes: &s.ActivityTaskFailedEventAttributes{},
231+
},
232+
expected: &s.ActivityTaskFailedEventAttributes{},
233+
},
234+
{
235+
event: &s.HistoryEvent{
236+
EventType: toPtr(s.EventTypeActivityTaskTimedOut),
237+
ActivityTaskTimedOutEventAttributes: &s.ActivityTaskTimedOutEventAttributes{},
238+
},
239+
expected: &s.ActivityTaskTimedOutEventAttributes{},
240+
},
241+
{
242+
event: &s.HistoryEvent{
243+
EventType: toPtr(s.EventTypeActivityTaskCancelRequested),
244+
ActivityTaskCancelRequestedEventAttributes: &s.ActivityTaskCancelRequestedEventAttributes{},
245+
},
246+
expected: &s.ActivityTaskCancelRequestedEventAttributes{},
247+
},
248+
{
249+
event: &s.HistoryEvent{
250+
EventType: toPtr(s.EventTypeRequestCancelActivityTaskFailed),
251+
RequestCancelActivityTaskFailedEventAttributes: &s.RequestCancelActivityTaskFailedEventAttributes{},
252+
},
253+
expected: &s.RequestCancelActivityTaskFailedEventAttributes{},
254+
},
255+
{
256+
event: &s.HistoryEvent{
257+
EventType: toPtr(s.EventTypeActivityTaskCanceled),
258+
ActivityTaskCanceledEventAttributes: &s.ActivityTaskCanceledEventAttributes{},
259+
},
260+
expected: &s.ActivityTaskCanceledEventAttributes{},
261+
},
262+
{
263+
event: &s.HistoryEvent{
264+
EventType: toPtr(s.EventTypeTimerStarted),
265+
TimerStartedEventAttributes: &s.TimerStartedEventAttributes{},
266+
},
267+
expected: &s.TimerStartedEventAttributes{},
268+
},
269+
{
270+
event: &s.HistoryEvent{
271+
EventType: toPtr(s.EventTypeTimerFired),
272+
TimerFiredEventAttributes: &s.TimerFiredEventAttributes{},
273+
},
274+
expected: &s.TimerFiredEventAttributes{},
275+
},
276+
{
277+
event: &s.HistoryEvent{
278+
EventType: toPtr(s.EventTypeCancelTimerFailed),
279+
CancelTimerFailedEventAttributes: &s.CancelTimerFailedEventAttributes{},
280+
},
281+
expected: &s.CancelTimerFailedEventAttributes{},
282+
},
283+
{
284+
event: &s.HistoryEvent{
285+
EventType: toPtr(s.EventTypeTimerCanceled),
286+
TimerCanceledEventAttributes: &s.TimerCanceledEventAttributes{},
287+
},
288+
expected: &s.TimerCanceledEventAttributes{},
289+
},
290+
{
291+
event: &s.HistoryEvent{
292+
EventType: toPtr(s.EventTypeMarkerRecorded),
293+
MarkerRecordedEventAttributes: &s.MarkerRecordedEventAttributes{},
294+
},
295+
expected: &s.MarkerRecordedEventAttributes{},
296+
},
297+
{
298+
event: &s.HistoryEvent{
299+
EventType: toPtr(s.EventTypeWorkflowExecutionTerminated),
300+
WorkflowExecutionTerminatedEventAttributes: &s.WorkflowExecutionTerminatedEventAttributes{},
301+
},
302+
expected: &s.WorkflowExecutionTerminatedEventAttributes{},
303+
},
304+
// In the default case, we should return the event itself
305+
{
306+
event: &s.HistoryEvent{
307+
EventType: toPtr(s.EventType(123456789)),
308+
},
309+
expected: &s.HistoryEvent{
310+
EventType: toPtr(s.EventType(123456789)),
311+
},
312+
},
313+
}
314+
315+
for _, tc := range cases {
316+
name, err := tc.event.GetEventType().MarshalText()
317+
require.NoError(t, err)
318+
t.Run(string(name), func(t *testing.T) {
319+
require.Equal(t, tc.expected, getData(tc.event))
320+
})
321+
}
322+
}
323+
324+
func toPtr[v any](x v) *v {
325+
return &x
326+
}

0 commit comments

Comments
 (0)