Skip to content

Commit

Permalink
Added tests for HistoryEventToString (#1383)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jakobht authored Nov 5, 2024
1 parent 2801925 commit 42e3e2e
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 26 deletions.
55 changes: 29 additions & 26 deletions internal/common/util/stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,79 +78,82 @@ func valueToString(v reflect.Value) string {

// HistoryEventToString convert HistoryEvent to string
func HistoryEventToString(e *s.HistoryEvent) string {
var data interface{}
data := getData(e)

return e.GetEventType().String() + ": " + anyToString(data)
}

func getData(e *s.HistoryEvent) interface{} {
switch e.GetEventType() {
case s.EventTypeWorkflowExecutionStarted:
data = e.WorkflowExecutionStartedEventAttributes
return e.WorkflowExecutionStartedEventAttributes

case s.EventTypeWorkflowExecutionCompleted:
data = e.WorkflowExecutionCompletedEventAttributes
return e.WorkflowExecutionCompletedEventAttributes

case s.EventTypeWorkflowExecutionFailed:
data = e.WorkflowExecutionFailedEventAttributes
return e.WorkflowExecutionFailedEventAttributes

case s.EventTypeWorkflowExecutionTimedOut:
data = e.WorkflowExecutionTimedOutEventAttributes
return e.WorkflowExecutionTimedOutEventAttributes

case s.EventTypeDecisionTaskScheduled:
data = e.DecisionTaskScheduledEventAttributes
return e.DecisionTaskScheduledEventAttributes

case s.EventTypeDecisionTaskStarted:
data = e.DecisionTaskStartedEventAttributes
return e.DecisionTaskStartedEventAttributes

case s.EventTypeDecisionTaskCompleted:
data = e.DecisionTaskCompletedEventAttributes
return e.DecisionTaskCompletedEventAttributes

case s.EventTypeDecisionTaskTimedOut:
data = e.DecisionTaskTimedOutEventAttributes
return e.DecisionTaskTimedOutEventAttributes

case s.EventTypeActivityTaskScheduled:
data = e.ActivityTaskScheduledEventAttributes
return e.ActivityTaskScheduledEventAttributes

case s.EventTypeActivityTaskStarted:
data = e.ActivityTaskStartedEventAttributes
return e.ActivityTaskStartedEventAttributes

case s.EventTypeActivityTaskCompleted:
data = e.ActivityTaskCompletedEventAttributes
return e.ActivityTaskCompletedEventAttributes

case s.EventTypeActivityTaskFailed:
data = e.ActivityTaskFailedEventAttributes
return e.ActivityTaskFailedEventAttributes

case s.EventTypeActivityTaskTimedOut:
data = e.ActivityTaskTimedOutEventAttributes
return e.ActivityTaskTimedOutEventAttributes

case s.EventTypeActivityTaskCancelRequested:
data = e.ActivityTaskCancelRequestedEventAttributes
return e.ActivityTaskCancelRequestedEventAttributes

case s.EventTypeRequestCancelActivityTaskFailed:
data = e.RequestCancelActivityTaskFailedEventAttributes
return e.RequestCancelActivityTaskFailedEventAttributes

case s.EventTypeActivityTaskCanceled:
data = e.ActivityTaskCanceledEventAttributes
return e.ActivityTaskCanceledEventAttributes

case s.EventTypeTimerStarted:
data = e.TimerStartedEventAttributes
return e.TimerStartedEventAttributes

case s.EventTypeTimerFired:
data = e.TimerFiredEventAttributes
return e.TimerFiredEventAttributes

case s.EventTypeCancelTimerFailed:
data = e.CancelTimerFailedEventAttributes
return e.CancelTimerFailedEventAttributes

case s.EventTypeTimerCanceled:
data = e.TimerCanceledEventAttributes
return e.TimerCanceledEventAttributes

case s.EventTypeMarkerRecorded:
data = e.MarkerRecordedEventAttributes
return e.MarkerRecordedEventAttributes

case s.EventTypeWorkflowExecutionTerminated:
data = e.WorkflowExecutionTerminatedEventAttributes
return e.WorkflowExecutionTerminatedEventAttributes

default:
data = e
return e
}

return e.GetEventType().String() + ": " + anyToString(data)
}

// DecisionToString convert Decision to string
Expand Down
210 changes: 210 additions & 0 deletions internal/common/util/stringer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/thriftrw/ptr"

s "go.uber.org/cadence/.gen/go/shared"
)

func Test_anyToString(t *testing.T) {
Expand Down Expand Up @@ -114,3 +118,209 @@ func Test_byteSliceToString(t *testing.T) {

require.Equal(t, "[len=3]", strVal2)
}

func TestHistoryEventToString(t *testing.T) {
event := &s.HistoryEvent{
EventType: toPtr(s.EventTypeWorkflowExecutionStarted),
WorkflowExecutionStartedEventAttributes: &s.WorkflowExecutionStartedEventAttributes{
WorkflowType: &s.WorkflowType{
Name: toPtr("test-workflow"),
},
TaskList: &s.TaskList{
Name: toPtr("task-list"),
},
ExecutionStartToCloseTimeoutSeconds: ptr.Int32(60),
},
}

strVal := HistoryEventToString(event)

expected := `WorkflowExecutionStarted: (WorkflowType:(Name:test-workflow), TaskList:(Name:task-list), Input:[], ExecutionStartToCloseTimeoutSeconds:60, ContinuedFailureDetails:[], LastCompletionResult:[], PartitionConfig:map[])`

assert.Equal(t, expected, strVal)
}

// This just tests that we pick the right attibutes to return
// the other attributes will be nil
func Test_getData(t *testing.T) {
cases := []struct {
event *s.HistoryEvent
expected interface{}
}{
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeWorkflowExecutionStarted),
WorkflowExecutionStartedEventAttributes: &s.WorkflowExecutionStartedEventAttributes{},
},
expected: &s.WorkflowExecutionStartedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeWorkflowExecutionCompleted),
WorkflowExecutionCompletedEventAttributes: &s.WorkflowExecutionCompletedEventAttributes{},
},
expected: &s.WorkflowExecutionCompletedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeWorkflowExecutionFailed),
WorkflowExecutionFailedEventAttributes: &s.WorkflowExecutionFailedEventAttributes{},
},
expected: &s.WorkflowExecutionFailedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeWorkflowExecutionTimedOut),
WorkflowExecutionTimedOutEventAttributes: &s.WorkflowExecutionTimedOutEventAttributes{},
},
expected: &s.WorkflowExecutionTimedOutEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeDecisionTaskScheduled),
DecisionTaskScheduledEventAttributes: &s.DecisionTaskScheduledEventAttributes{},
},
expected: &s.DecisionTaskScheduledEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeDecisionTaskStarted),
DecisionTaskStartedEventAttributes: &s.DecisionTaskStartedEventAttributes{},
},
expected: &s.DecisionTaskStartedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeDecisionTaskCompleted),
DecisionTaskCompletedEventAttributes: &s.DecisionTaskCompletedEventAttributes{},
},
expected: &s.DecisionTaskCompletedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeDecisionTaskTimedOut),
DecisionTaskTimedOutEventAttributes: &s.DecisionTaskTimedOutEventAttributes{},
},
expected: &s.DecisionTaskTimedOutEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeActivityTaskScheduled),
ActivityTaskScheduledEventAttributes: &s.ActivityTaskScheduledEventAttributes{},
},
expected: &s.ActivityTaskScheduledEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeActivityTaskStarted),
ActivityTaskStartedEventAttributes: &s.ActivityTaskStartedEventAttributes{},
},
expected: &s.ActivityTaskStartedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeActivityTaskCompleted),
ActivityTaskCompletedEventAttributes: &s.ActivityTaskCompletedEventAttributes{},
},
expected: &s.ActivityTaskCompletedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeActivityTaskFailed),
ActivityTaskFailedEventAttributes: &s.ActivityTaskFailedEventAttributes{},
},
expected: &s.ActivityTaskFailedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeActivityTaskTimedOut),
ActivityTaskTimedOutEventAttributes: &s.ActivityTaskTimedOutEventAttributes{},
},
expected: &s.ActivityTaskTimedOutEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeActivityTaskCancelRequested),
ActivityTaskCancelRequestedEventAttributes: &s.ActivityTaskCancelRequestedEventAttributes{},
},
expected: &s.ActivityTaskCancelRequestedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeRequestCancelActivityTaskFailed),
RequestCancelActivityTaskFailedEventAttributes: &s.RequestCancelActivityTaskFailedEventAttributes{},
},
expected: &s.RequestCancelActivityTaskFailedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeActivityTaskCanceled),
ActivityTaskCanceledEventAttributes: &s.ActivityTaskCanceledEventAttributes{},
},
expected: &s.ActivityTaskCanceledEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeTimerStarted),
TimerStartedEventAttributes: &s.TimerStartedEventAttributes{},
},
expected: &s.TimerStartedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeTimerFired),
TimerFiredEventAttributes: &s.TimerFiredEventAttributes{},
},
expected: &s.TimerFiredEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeCancelTimerFailed),
CancelTimerFailedEventAttributes: &s.CancelTimerFailedEventAttributes{},
},
expected: &s.CancelTimerFailedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeTimerCanceled),
TimerCanceledEventAttributes: &s.TimerCanceledEventAttributes{},
},
expected: &s.TimerCanceledEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeMarkerRecorded),
MarkerRecordedEventAttributes: &s.MarkerRecordedEventAttributes{},
},
expected: &s.MarkerRecordedEventAttributes{},
},
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventTypeWorkflowExecutionTerminated),
WorkflowExecutionTerminatedEventAttributes: &s.WorkflowExecutionTerminatedEventAttributes{},
},
expected: &s.WorkflowExecutionTerminatedEventAttributes{},
},
// In the default case, we should return the event itself
{
event: &s.HistoryEvent{
EventType: toPtr(s.EventType(123456789)),
},
expected: &s.HistoryEvent{
EventType: toPtr(s.EventType(123456789)),
},
},
}

for _, tc := range cases {
name, err := tc.event.GetEventType().MarshalText()
require.NoError(t, err)
t.Run(string(name), func(t *testing.T) {
require.Equal(t, tc.expected, getData(tc.event))
})
}
}

func toPtr[v any](x v) *v {
return &x
}

0 comments on commit 42e3e2e

Please sign in to comment.