Skip to content

Commit

Permalink
Add more unit tests for event_handlers (#1404)
Browse files Browse the repository at this point in the history
* Add a few tests
  • Loading branch information
3vilhamster authored Nov 21, 2024
1 parent 06afa1f commit fabd87b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
6 changes: 0 additions & 6 deletions internal/internal_event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,6 @@ func (wc *workflowEnvironmentImpl) GenerateSequence() int32 {
return result
}

func (wc *workflowEnvironmentImpl) CreateNewDecision(decisionType m.DecisionType) *m.Decision {
return &m.Decision{
DecisionType: common.DecisionTypePtr(decisionType),
}
}

func (wc *workflowEnvironmentImpl) ExecuteActivity(parameters executeActivityParams, callback resultHandler) *activityInfo {
scheduleTaskAttr := &m.ScheduleActivityTaskDecisionAttributes{}
if parameters.ActivityID == nil || *parameters.ActivityID == "" {
Expand Down
52 changes: 52 additions & 0 deletions internal/internal_event_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package internal
import (
"encoding/json"
"testing"
"time"

"go.uber.org/cadence/internal/common/testlogger"

Expand Down Expand Up @@ -68,6 +69,13 @@ func TestReplayAwareLogger(t *testing.T) {
assert.NotContains(t, messages, "replay info")
assert.Contains(t, messages, "normal2 info")
assert.Contains(t, messages, "replay2 info")

isReplay = true
enableLoggingInReplay = true
parentCore := wrapLogger(&isReplay, &enableLoggingInReplay)
wrappedCore := parentCore(core).With([]zapcore.Field{zap.String("key", "value")}).(*replayAwareZapCore)
assert.Equal(t, wrappedCore.isReplay, &isReplay)
assert.Equal(t, wrappedCore.enableLoggingInReplay, &enableLoggingInReplay)
}

func testDecodeValueHelper(t *testing.T, env *workflowEnvironmentImpl) {
Expand Down Expand Up @@ -929,6 +937,50 @@ func TestEventHandler_handleMarkerRecorded_failures(t *testing.T) {
}
}

func TestWorkflowEnvironment_sessions(t *testing.T) {
handler := testWorkflowExecutionEventHandler(t, newRegistry())
testSession := &SessionInfo{
SessionID: "test-session",
HostName: "test-host",
}
handler.AddSession(testSession)
list := handler.getOpenSessions()
assert.Contains(t, list, testSession)
handler.RemoveSession(testSession.SessionID)
list = handler.getOpenSessions()
assert.Empty(t, list)
}

func TestWorkflowExecutionEnvironment_NewTimer_immediate_calls(t *testing.T) {
t.Run("immediate call", func(t *testing.T) {
handler := testWorkflowExecutionEventHandler(t, newRegistry())
handlerCalled := false
res := handler.NewTimer(0, func(result []byte, err error) {
assert.NoError(t, err)
handlerCalled = true
})
assert.True(t, handlerCalled, "handler must be called immediately")
assert.Nil(t, res)
})
t.Run("negative duration", func(t *testing.T) {
handler := testWorkflowExecutionEventHandler(t, newRegistry())
handlerCalled := false
res := handler.NewTimer(-2*time.Second, func(result []byte, err error) {
handlerCalled = true
assert.ErrorContains(t, err, "negative duration provided")
})
assert.Nil(t, res)
assert.True(t, handlerCalled, "handler must be called immediately")
})
t.Run("timer cancellation", func(t *testing.T) {
handler := testWorkflowExecutionEventHandler(t, newRegistry())
timer := handler.NewTimer(time.Second, func(result []byte, err error) {
assert.ErrorIs(t, err, ErrCanceled)
})
handler.RequestCancelTimer(timer.timerID)
})
}

func testWorkflowExecutionEventHandler(t *testing.T, registry *registry) *workflowExecutionEventHandlerImpl {
return newWorkflowExecutionEventHandler(
testWorkflowInfo,
Expand Down

0 comments on commit fabd87b

Please sign in to comment.