Skip to content

Commit fabd87b

Browse files
authored
Add more unit tests for event_handlers (#1404)
* Add a few tests
1 parent 06afa1f commit fabd87b

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

internal/internal_event_handlers.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,6 @@ func (wc *workflowEnvironmentImpl) GenerateSequence() int32 {
463463
return result
464464
}
465465

466-
func (wc *workflowEnvironmentImpl) CreateNewDecision(decisionType m.DecisionType) *m.Decision {
467-
return &m.Decision{
468-
DecisionType: common.DecisionTypePtr(decisionType),
469-
}
470-
}
471-
472466
func (wc *workflowEnvironmentImpl) ExecuteActivity(parameters executeActivityParams, callback resultHandler) *activityInfo {
473467
scheduleTaskAttr := &m.ScheduleActivityTaskDecisionAttributes{}
474468
if parameters.ActivityID == nil || *parameters.ActivityID == "" {

internal/internal_event_handlers_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package internal
2323
import (
2424
"encoding/json"
2525
"testing"
26+
"time"
2627

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

@@ -68,6 +69,13 @@ func TestReplayAwareLogger(t *testing.T) {
6869
assert.NotContains(t, messages, "replay info")
6970
assert.Contains(t, messages, "normal2 info")
7071
assert.Contains(t, messages, "replay2 info")
72+
73+
isReplay = true
74+
enableLoggingInReplay = true
75+
parentCore := wrapLogger(&isReplay, &enableLoggingInReplay)
76+
wrappedCore := parentCore(core).With([]zapcore.Field{zap.String("key", "value")}).(*replayAwareZapCore)
77+
assert.Equal(t, wrappedCore.isReplay, &isReplay)
78+
assert.Equal(t, wrappedCore.enableLoggingInReplay, &enableLoggingInReplay)
7179
}
7280

7381
func testDecodeValueHelper(t *testing.T, env *workflowEnvironmentImpl) {
@@ -929,6 +937,50 @@ func TestEventHandler_handleMarkerRecorded_failures(t *testing.T) {
929937
}
930938
}
931939

940+
func TestWorkflowEnvironment_sessions(t *testing.T) {
941+
handler := testWorkflowExecutionEventHandler(t, newRegistry())
942+
testSession := &SessionInfo{
943+
SessionID: "test-session",
944+
HostName: "test-host",
945+
}
946+
handler.AddSession(testSession)
947+
list := handler.getOpenSessions()
948+
assert.Contains(t, list, testSession)
949+
handler.RemoveSession(testSession.SessionID)
950+
list = handler.getOpenSessions()
951+
assert.Empty(t, list)
952+
}
953+
954+
func TestWorkflowExecutionEnvironment_NewTimer_immediate_calls(t *testing.T) {
955+
t.Run("immediate call", func(t *testing.T) {
956+
handler := testWorkflowExecutionEventHandler(t, newRegistry())
957+
handlerCalled := false
958+
res := handler.NewTimer(0, func(result []byte, err error) {
959+
assert.NoError(t, err)
960+
handlerCalled = true
961+
})
962+
assert.True(t, handlerCalled, "handler must be called immediately")
963+
assert.Nil(t, res)
964+
})
965+
t.Run("negative duration", func(t *testing.T) {
966+
handler := testWorkflowExecutionEventHandler(t, newRegistry())
967+
handlerCalled := false
968+
res := handler.NewTimer(-2*time.Second, func(result []byte, err error) {
969+
handlerCalled = true
970+
assert.ErrorContains(t, err, "negative duration provided")
971+
})
972+
assert.Nil(t, res)
973+
assert.True(t, handlerCalled, "handler must be called immediately")
974+
})
975+
t.Run("timer cancellation", func(t *testing.T) {
976+
handler := testWorkflowExecutionEventHandler(t, newRegistry())
977+
timer := handler.NewTimer(time.Second, func(result []byte, err error) {
978+
assert.ErrorIs(t, err, ErrCanceled)
979+
})
980+
handler.RequestCancelTimer(timer.timerID)
981+
})
982+
}
983+
932984
func testWorkflowExecutionEventHandler(t *testing.T, registry *registry) *workflowExecutionEventHandlerImpl {
933985
return newWorkflowExecutionEventHandler(
934986
testWorkflowInfo,

0 commit comments

Comments
 (0)