Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more unit tests for event_handlers #1404

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading