Skip to content

Commit 66013a1

Browse files
committed
Add unit tests for internal/internal_task_handlers.go
1 parent 89676d3 commit 66013a1

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

internal/internal_task_handlers_test.go

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,3 +1897,189 @@ func Test_IsSearchAttributesMatched(t *testing.T) {
18971897
})
18981898
}
18991899
}
1900+
1901+
func Test__GetWorkflowStartedEvent(t *testing.T) {
1902+
wfStartedEvent := createTestEventWorkflowExecutionStarted(1, &s.WorkflowExecutionStartedEventAttributes{TaskList: &s.TaskList{Name: common.StringPtr("tl1")}})
1903+
h := &history{workflowTask: &workflowTask{task: &s.PollForDecisionTaskResponse{History: &s.History{Events: []*s.HistoryEvent{wfStartedEvent}}}}}
1904+
result, err := h.GetWorkflowStartedEvent()
1905+
require.NoError(t, err)
1906+
require.Equal(t, wfStartedEvent, result)
1907+
1908+
emptyHistory := &history{workflowTask: &workflowTask{task: &s.PollForDecisionTaskResponse{History: &s.History{}}}}
1909+
result, err = emptyHistory.GetWorkflowStartedEvent()
1910+
require.ErrorContains(t, err, "unable to find WorkflowExecutionStartedEventAttributes")
1911+
require.Nil(t, result)
1912+
}
1913+
1914+
func Test__verifyAllEventsProcessed(t *testing.T) {
1915+
testCases := []struct {
1916+
name string
1917+
lastEventID int64
1918+
nextEventID int64
1919+
Message string
1920+
}{
1921+
{
1922+
name: "error",
1923+
lastEventID: 1,
1924+
nextEventID: 1,
1925+
Message: "history_events: premature end of stream",
1926+
},
1927+
{
1928+
name: "warn",
1929+
lastEventID: 1,
1930+
nextEventID: 3,
1931+
Message: "history_events: processed events past the expected lastEventID",
1932+
},
1933+
{
1934+
name: "success",
1935+
lastEventID: 1,
1936+
nextEventID: 2,
1937+
},
1938+
}
1939+
for _, testCase := range testCases {
1940+
t.Run(testCase.name, func(t *testing.T) {
1941+
obs, logs := observer.New(zap.WarnLevel)
1942+
logger := zap.New(obs)
1943+
h := &history{
1944+
lastEventID: testCase.lastEventID,
1945+
nextEventID: testCase.nextEventID,
1946+
eventsHandler: &workflowExecutionEventHandlerImpl{workflowEnvironmentImpl: &workflowEnvironmentImpl{logger: logger}}}
1947+
err := h.verifyAllEventsProcessed()
1948+
if testCase.name == "error" {
1949+
require.ErrorContains(t, err, testCase.Message)
1950+
} else if testCase.name == "warn" {
1951+
warnLogs := logs.FilterMessage(testCase.Message)
1952+
require.Len(t, warnLogs.All(), 1)
1953+
} else {
1954+
require.NoError(t, err)
1955+
}
1956+
})
1957+
}
1958+
1959+
}
1960+
1961+
func Test__workflowCategorizedByTimeout(t *testing.T) {
1962+
testCases := []struct {
1963+
timeout int32
1964+
expectedCategory string
1965+
}{
1966+
{
1967+
timeout: 1,
1968+
expectedCategory: "instant",
1969+
},
1970+
{
1971+
timeout: 1000,
1972+
expectedCategory: "short",
1973+
},
1974+
{
1975+
timeout: 2000,
1976+
expectedCategory: "intermediate",
1977+
},
1978+
{
1979+
timeout: 30000,
1980+
expectedCategory: "long",
1981+
},
1982+
}
1983+
for _, tt := range testCases {
1984+
t.Run(tt.expectedCategory, func(t *testing.T) {
1985+
wfContext := &workflowExecutionContextImpl{workflowInfo: &WorkflowInfo{ExecutionStartToCloseTimeoutSeconds: tt.timeout}}
1986+
require.Equal(t, tt.expectedCategory, workflowCategorizedByTimeout(wfContext))
1987+
})
1988+
}
1989+
}
1990+
1991+
func Test__SignalWorkflow(t *testing.T) {
1992+
mockCtrl := gomock.NewController(t)
1993+
mockService := workflowservicetest.NewMockClient(mockCtrl)
1994+
mockService.EXPECT().SignalWorkflowExecution(gomock.Any(), gomock.Any(), callOptions()...).Return(nil)
1995+
cadenceInvoker := &cadenceInvoker{
1996+
identity: "Test_Cadence_Invoker",
1997+
service: mockService,
1998+
taskToken: nil,
1999+
}
2000+
err := cadenceInvoker.SignalWorkflow(context.Background(), "test-domain", "test-workflow-id", "test-run-id", "test-signal-name", nil)
2001+
require.NoError(t, err)
2002+
}
2003+
2004+
func Test__getRetryBackoffWithNowTime(t *testing.T) {
2005+
now := time.Now()
2006+
testCases := []struct {
2007+
name string
2008+
maxAttempts int32
2009+
ExpInterval time.Duration
2010+
result time.Duration
2011+
attempt int32
2012+
errReason string
2013+
expireTime time.Time
2014+
initialInterval time.Duration
2015+
maxInterval time.Duration
2016+
}{
2017+
{
2018+
name: "no max attempts or expiration interval set",
2019+
maxAttempts: 0,
2020+
ExpInterval: 0,
2021+
result: noRetryBackoff,
2022+
},
2023+
{
2024+
name: "max attempts done",
2025+
maxAttempts: 5,
2026+
attempt: 5,
2027+
result: noRetryBackoff,
2028+
},
2029+
{
2030+
name: "non retryable error",
2031+
maxAttempts: 5,
2032+
attempt: 2,
2033+
errReason: "bad request",
2034+
initialInterval: time.Minute,
2035+
maxInterval: time.Minute,
2036+
result: noRetryBackoff,
2037+
},
2038+
{
2039+
name: "fallback to max interval when calculated backoff is 0",
2040+
maxAttempts: 5,
2041+
attempt: 2,
2042+
initialInterval: 0,
2043+
maxInterval: time.Minute,
2044+
result: time.Minute,
2045+
},
2046+
{
2047+
name: "fallback to no retry backoff when calculated backoff is 0 and max interval is not set",
2048+
maxAttempts: 5,
2049+
attempt: 2,
2050+
initialInterval: 0,
2051+
result: noRetryBackoff,
2052+
},
2053+
{
2054+
name: "expiry time reached",
2055+
maxAttempts: 5,
2056+
attempt: 2,
2057+
expireTime: now.Add(time.Second),
2058+
initialInterval: time.Minute,
2059+
maxInterval: time.Minute,
2060+
result: noRetryBackoff,
2061+
},
2062+
{
2063+
name: "retry after backoff",
2064+
maxAttempts: 5,
2065+
attempt: 2,
2066+
errReason: "timeout",
2067+
initialInterval: time.Minute,
2068+
maxInterval: time.Minute,
2069+
result: time.Minute,
2070+
},
2071+
}
2072+
for _, tt := range testCases {
2073+
t.Run(tt.name, func(t *testing.T) {
2074+
policy := &RetryPolicy{
2075+
MaximumAttempts: tt.maxAttempts,
2076+
ExpirationInterval: tt.ExpInterval,
2077+
BackoffCoefficient: 2,
2078+
NonRetriableErrorReasons: []string{"bad request"},
2079+
MaximumInterval: tt.maxInterval,
2080+
InitialInterval: tt.initialInterval,
2081+
}
2082+
require.Equal(t, tt.result, getRetryBackoffWithNowTime(policy, tt.attempt, tt.errReason, now, tt.expireTime))
2083+
})
2084+
}
2085+
}

0 commit comments

Comments
 (0)