-
Notifications
You must be signed in to change notification settings - Fork 8
/
timeout_internal_test.go
205 lines (192 loc) · 5.72 KB
/
timeout_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package workflow
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
clock_testing "k8s.io/utils/clock/testing"
"github.com/luno/workflow/internal/errorcounter"
)
func TestProcessTimeout(t *testing.T) {
ctx := context.Background()
counter := errorcounter.New()
processName := "processName"
testErr := errors.New("test error")
w := &Workflow[string, testStatus]{
Name: "example",
ctx: ctx,
clock: clock_testing.NewFakeClock(time.Date(2024, time.April, 19, 0, 0, 0, 0, time.UTC)),
errorCounter: counter,
logger: &logger{},
}
value := "data"
b, err := Marshal(&value)
require.Nil(t, err)
type calls struct {
updater func(ctx context.Context, current testStatus, next testStatus, record *Run[string, testStatus]) error
store func(ctx context.Context, record *Record) error
timeoutFunc func(ctx context.Context, r *Run[string, testStatus], now time.Time) (testStatus, error)
completeFunc func(ctx context.Context, id int64) error
}
type caller func(call map[string]int) calls
testCases := []struct {
name string
caller caller
timeout timeout[string, testStatus]
record *Record
currentErrCount int
expectedCalls map[string]int
}{
{
name: "Golden path consume - initiated",
caller: func(call map[string]int) calls {
return calls{
updater: func(ctx context.Context, current testStatus, next testStatus, record *Run[string, testStatus]) error {
call["updater"] += 1
require.Equal(t, "new data", *record.Object)
return nil
},
store: func(ctx context.Context, record *Record) error {
call["store"] += 1
return nil
},
timeoutFunc: func(ctx context.Context, r *Run[string, testStatus], now time.Time) (testStatus, error) {
call["timeout/TimeoutFunc"] += 1
*r.Object = "new data"
return statusEnd, nil
},
completeFunc: func(ctx context.Context, id int64) error {
call["complete"] += 1
return nil
},
}
},
record: &Record{
WorkflowName: "example",
ForeignID: "32948623984623",
RunID: "JHFJDS-LSFKHJSLD-KSJDBLSL",
RunState: RunStateInitiated,
Status: int(statusStart),
Object: b,
},
expectedCalls: map[string]int{
"timeout/TimeoutFunc": 1,
"updater": 1,
"complete": 1,
},
},
{
name: "Golden path consume - running",
caller: func(call map[string]int) calls {
return calls{
updater: func(ctx context.Context, current testStatus, next testStatus, record *Run[string, testStatus]) error {
call["updater"] += 1
require.Equal(t, "new data", *record.Object)
return nil
},
store: func(ctx context.Context, record *Record) error {
call["store"] += 1
return nil
},
timeoutFunc: func(ctx context.Context, r *Run[string, testStatus], now time.Time) (testStatus, error) {
call["timeout/TimeoutFunc"] += 1
*r.Object = "new data"
return statusEnd, nil
},
completeFunc: func(ctx context.Context, id int64) error {
call["complete"] += 1
return nil
},
}
},
record: &Record{
WorkflowName: "example",
ForeignID: "32948623984623",
RunID: "JHFJDS-LSFKHJSLD-KSJDBLSL",
RunState: RunStateRunning,
Status: int(statusStart),
Object: b,
},
expectedCalls: map[string]int{
"timeout/TimeoutFunc": 1,
"updater": 1,
"complete": 1,
},
},
{
name: "Skip consume",
caller: func(call map[string]int) calls {
return calls{
timeoutFunc: func(ctx context.Context, r *Run[string, testStatus], now time.Time) (testStatus, error) {
call["timeout/TimeoutFunc"] += 1
*r.Object = "new data"
return testStatus(SkipTypeDefault), nil
},
}
},
record: &Record{
WorkflowName: "example",
ForeignID: "32948623984623",
RunID: "JHFJDS-LSFKHJSLD-KSJDBLSL",
RunState: RunStateRunning,
Status: int(statusStart),
Object: b,
},
expectedCalls: map[string]int{
"timeout/TimeoutFunc": 1,
},
},
{
name: "Pause record when meeting error count",
caller: func(call map[string]int) calls {
return calls{
timeoutFunc: func(ctx context.Context, r *Run[string, testStatus], now time.Time) (testStatus, error) {
call["timeout/TimeoutFunc"] += 1
return 0, testErr
},
store: func(ctx context.Context, record *Record) error {
call["store"] += 1
require.Equal(t, record.RunState, RunStatePaused)
return nil
},
}
},
record: &Record{
WorkflowName: "example",
ForeignID: "32948623984623",
RunID: "JHFJDS-LSFKHJSLD-KSJDBLSL",
RunState: RunStateRunning,
Status: int(statusStart),
Object: b,
},
currentErrCount: 1,
expectedCalls: map[string]int{
"timeout/TimeoutFunc": 1,
"store": 1,
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
counter.Clear(testErr, processName, tc.record.RunID)
for range tc.currentErrCount {
counter.Add(testErr, processName, tc.record.RunID)
}
calls := map[string]int{}
timeout := timeout[string, testStatus]{
TimeoutFunc: tc.caller(calls).timeoutFunc,
}
tr := TimeoutRecord{
ID: 1,
WorkflowName: tc.record.WorkflowName,
ForeignID: tc.record.ForeignID,
RunID: tc.record.RunID,
Status: tc.record.Status,
}
err := processTimeout(ctx, w, timeout, tc.record, tr, tc.caller(calls).completeFunc, tc.caller(calls).store, tc.caller(calls).updater, processName, 1)
require.Nil(t, err)
require.Equal(t, tc.expectedCalls, calls)
})
}
}