Skip to content

Commit d34347b

Browse files
committed
Added tests for HistoryEventToString
1 parent 8a62ac2 commit d34347b

File tree

2 files changed

+238
-26
lines changed

2 files changed

+238
-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: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import (
2424
"reflect"
2525
"testing"
2626

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

3033
func Test_byteSliceToString(t *testing.T) {
@@ -40,3 +43,209 @@ func Test_byteSliceToString(t *testing.T) {
4043

4144
require.Equal(t, "[len=3]", strVal2)
4245
}
46+
47+
func TestHistoryEventToString(t *testing.T) {
48+
event := &s.HistoryEvent{
49+
EventType: toPtr(s.EventTypeWorkflowExecutionStarted),
50+
WorkflowExecutionStartedEventAttributes: &s.WorkflowExecutionStartedEventAttributes{
51+
WorkflowType: &s.WorkflowType{
52+
Name: toPtr("test-workflow"),
53+
},
54+
TaskList: &s.TaskList{
55+
Name: toPtr("task-list"),
56+
},
57+
ExecutionStartToCloseTimeoutSeconds: ptr.Int32(60),
58+
},
59+
}
60+
61+
strVal := HistoryEventToString(event)
62+
63+
expected := `WorkflowExecutionStarted: (WorkflowType:(Name:test-workflow), TaskList:(Name:task-list), Input:[], ExecutionStartToCloseTimeoutSeconds:60, ContinuedFailureDetails:[], LastCompletionResult:[], PartitionConfig:map[])`
64+
65+
assert.Equal(t, expected, strVal)
66+
}
67+
68+
// This just tests that we pick the right attibutes to return
69+
// the other attributes will be nil
70+
func Test_getData(t *testing.T) {
71+
cases := []struct {
72+
event *s.HistoryEvent
73+
expected interface{}
74+
}{
75+
{
76+
event: &s.HistoryEvent{
77+
EventType: toPtr(s.EventTypeWorkflowExecutionStarted),
78+
WorkflowExecutionStartedEventAttributes: &s.WorkflowExecutionStartedEventAttributes{},
79+
},
80+
expected: &s.WorkflowExecutionStartedEventAttributes{},
81+
},
82+
{
83+
event: &s.HistoryEvent{
84+
EventType: toPtr(s.EventTypeWorkflowExecutionCompleted),
85+
WorkflowExecutionCompletedEventAttributes: &s.WorkflowExecutionCompletedEventAttributes{},
86+
},
87+
expected: &s.WorkflowExecutionCompletedEventAttributes{},
88+
},
89+
{
90+
event: &s.HistoryEvent{
91+
EventType: toPtr(s.EventTypeWorkflowExecutionFailed),
92+
WorkflowExecutionFailedEventAttributes: &s.WorkflowExecutionFailedEventAttributes{},
93+
},
94+
expected: &s.WorkflowExecutionFailedEventAttributes{},
95+
},
96+
{
97+
event: &s.HistoryEvent{
98+
EventType: toPtr(s.EventTypeWorkflowExecutionTimedOut),
99+
WorkflowExecutionTimedOutEventAttributes: &s.WorkflowExecutionTimedOutEventAttributes{},
100+
},
101+
expected: &s.WorkflowExecutionTimedOutEventAttributes{},
102+
},
103+
{
104+
event: &s.HistoryEvent{
105+
EventType: toPtr(s.EventTypeDecisionTaskScheduled),
106+
DecisionTaskScheduledEventAttributes: &s.DecisionTaskScheduledEventAttributes{},
107+
},
108+
expected: &s.DecisionTaskScheduledEventAttributes{},
109+
},
110+
{
111+
event: &s.HistoryEvent{
112+
EventType: toPtr(s.EventTypeDecisionTaskStarted),
113+
DecisionTaskStartedEventAttributes: &s.DecisionTaskStartedEventAttributes{},
114+
},
115+
expected: &s.DecisionTaskStartedEventAttributes{},
116+
},
117+
{
118+
event: &s.HistoryEvent{
119+
EventType: toPtr(s.EventTypeDecisionTaskCompleted),
120+
DecisionTaskCompletedEventAttributes: &s.DecisionTaskCompletedEventAttributes{},
121+
},
122+
expected: &s.DecisionTaskCompletedEventAttributes{},
123+
},
124+
{
125+
event: &s.HistoryEvent{
126+
EventType: toPtr(s.EventTypeDecisionTaskTimedOut),
127+
DecisionTaskTimedOutEventAttributes: &s.DecisionTaskTimedOutEventAttributes{},
128+
},
129+
expected: &s.DecisionTaskTimedOutEventAttributes{},
130+
},
131+
{
132+
event: &s.HistoryEvent{
133+
EventType: toPtr(s.EventTypeActivityTaskScheduled),
134+
ActivityTaskScheduledEventAttributes: &s.ActivityTaskScheduledEventAttributes{},
135+
},
136+
expected: &s.ActivityTaskScheduledEventAttributes{},
137+
},
138+
{
139+
event: &s.HistoryEvent{
140+
EventType: toPtr(s.EventTypeActivityTaskStarted),
141+
ActivityTaskStartedEventAttributes: &s.ActivityTaskStartedEventAttributes{},
142+
},
143+
expected: &s.ActivityTaskStartedEventAttributes{},
144+
},
145+
{
146+
event: &s.HistoryEvent{
147+
EventType: toPtr(s.EventTypeActivityTaskCompleted),
148+
ActivityTaskCompletedEventAttributes: &s.ActivityTaskCompletedEventAttributes{},
149+
},
150+
expected: &s.ActivityTaskCompletedEventAttributes{},
151+
},
152+
{
153+
event: &s.HistoryEvent{
154+
EventType: toPtr(s.EventTypeActivityTaskFailed),
155+
ActivityTaskFailedEventAttributes: &s.ActivityTaskFailedEventAttributes{},
156+
},
157+
expected: &s.ActivityTaskFailedEventAttributes{},
158+
},
159+
{
160+
event: &s.HistoryEvent{
161+
EventType: toPtr(s.EventTypeActivityTaskTimedOut),
162+
ActivityTaskTimedOutEventAttributes: &s.ActivityTaskTimedOutEventAttributes{},
163+
},
164+
expected: &s.ActivityTaskTimedOutEventAttributes{},
165+
},
166+
{
167+
event: &s.HistoryEvent{
168+
EventType: toPtr(s.EventTypeActivityTaskCancelRequested),
169+
ActivityTaskCancelRequestedEventAttributes: &s.ActivityTaskCancelRequestedEventAttributes{},
170+
},
171+
expected: &s.ActivityTaskCancelRequestedEventAttributes{},
172+
},
173+
{
174+
event: &s.HistoryEvent{
175+
EventType: toPtr(s.EventTypeRequestCancelActivityTaskFailed),
176+
RequestCancelActivityTaskFailedEventAttributes: &s.RequestCancelActivityTaskFailedEventAttributes{},
177+
},
178+
expected: &s.RequestCancelActivityTaskFailedEventAttributes{},
179+
},
180+
{
181+
event: &s.HistoryEvent{
182+
EventType: toPtr(s.EventTypeActivityTaskCanceled),
183+
ActivityTaskCanceledEventAttributes: &s.ActivityTaskCanceledEventAttributes{},
184+
},
185+
expected: &s.ActivityTaskCanceledEventAttributes{},
186+
},
187+
{
188+
event: &s.HistoryEvent{
189+
EventType: toPtr(s.EventTypeTimerStarted),
190+
TimerStartedEventAttributes: &s.TimerStartedEventAttributes{},
191+
},
192+
expected: &s.TimerStartedEventAttributes{},
193+
},
194+
{
195+
event: &s.HistoryEvent{
196+
EventType: toPtr(s.EventTypeTimerFired),
197+
TimerFiredEventAttributes: &s.TimerFiredEventAttributes{},
198+
},
199+
expected: &s.TimerFiredEventAttributes{},
200+
},
201+
{
202+
event: &s.HistoryEvent{
203+
EventType: toPtr(s.EventTypeCancelTimerFailed),
204+
CancelTimerFailedEventAttributes: &s.CancelTimerFailedEventAttributes{},
205+
},
206+
expected: &s.CancelTimerFailedEventAttributes{},
207+
},
208+
{
209+
event: &s.HistoryEvent{
210+
EventType: toPtr(s.EventTypeTimerCanceled),
211+
TimerCanceledEventAttributes: &s.TimerCanceledEventAttributes{},
212+
},
213+
expected: &s.TimerCanceledEventAttributes{},
214+
},
215+
{
216+
event: &s.HistoryEvent{
217+
EventType: toPtr(s.EventTypeMarkerRecorded),
218+
MarkerRecordedEventAttributes: &s.MarkerRecordedEventAttributes{},
219+
},
220+
expected: &s.MarkerRecordedEventAttributes{},
221+
},
222+
{
223+
event: &s.HistoryEvent{
224+
EventType: toPtr(s.EventTypeWorkflowExecutionTerminated),
225+
WorkflowExecutionTerminatedEventAttributes: &s.WorkflowExecutionTerminatedEventAttributes{},
226+
},
227+
expected: &s.WorkflowExecutionTerminatedEventAttributes{},
228+
},
229+
// In the default case, we should return the event itself
230+
{
231+
event: &s.HistoryEvent{
232+
EventType: toPtr(s.EventType(123456789)),
233+
},
234+
expected: &s.HistoryEvent{
235+
EventType: toPtr(s.EventType(123456789)),
236+
},
237+
},
238+
}
239+
240+
for _, tc := range cases {
241+
name, err := tc.event.GetEventType().MarshalText()
242+
require.NoError(t, err)
243+
t.Run(string(name), func(t *testing.T) {
244+
require.Equal(t, tc.expected, getData(tc.event))
245+
})
246+
}
247+
}
248+
249+
func toPtr[v any](x v) *v {
250+
return &x
251+
}

0 commit comments

Comments
 (0)