-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcallback_internal_test.go
More file actions
254 lines (209 loc) · 7.52 KB
/
callback_internal_test.go
File metadata and controls
254 lines (209 loc) · 7.52 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package workflow
import (
"context"
"errors"
"io"
"testing"
"time"
"github.com/stretchr/testify/require"
clock_testing "k8s.io/utils/clock/testing"
"github.com/luno/workflow/internal/graph"
)
func TestProcessCallback(t *testing.T) {
ctx := t.Context()
w := &Workflow[string, testStatus]{
name: "example",
ctx: ctx,
clock: clock_testing.NewFakeClock(time.Date(2024, time.April, 19, 0, 0, 0, 0, time.UTC)),
statusGraph: graph.New(),
logger: &logger{},
runPool: newRunPool[string, testStatus](),
}
w.statusGraph.AddTransition(int(statusStart), int(statusEnd))
value := "data"
b, err := Marshal(&value)
require.NoError(t, err)
current := &Record{
WorkflowName: "example",
ForeignID: "32948623984623",
RunID: "JHFJDS-LSFKHJSLD-KSJDBLSL",
RunState: RunStateRunning,
Status: int(statusStart),
Object: b,
}
t.Run("Golden path callback - initiated", func(t *testing.T) {
calls := map[string]int{
"callbackFunc": 0,
"updater": 0,
"latestLookup": 0,
}
current := &Record{
WorkflowName: "example",
ForeignID: "32948623984623",
RunID: "JHFJDS-LSFKHJSLD-KSJDBLSL",
RunState: RunStateInitiated,
Status: int(statusStart),
Object: b,
}
callbackFn := CallbackFunc[string, testStatus](func(ctx context.Context, r *Run[string, testStatus], reader io.Reader) (testStatus, error) {
calls["callbackFunc"] += 1
*r.Object = "new data"
return statusEnd, nil
})
updater := func(ctx context.Context, current testStatus, next testStatus, record *Run[string, testStatus], workingVersion uint) error {
calls["updater"] += 1
require.Equal(t, "new data", *record.Object)
return nil
}
latestLookup := func(ctx context.Context, workflowName, foreignID string) (*Record, error) {
calls["latestLookup"] += 1
return current, nil
}
store := func(ctx context.Context, record *Record) error {
calls["store"] += 1
return nil
}
err := processCallback(ctx, w, testStatus(current.Status), callbackFn, current.ForeignID, nil, latestLookup, store, updater)
require.NoError(t, err)
expectedCalls := map[string]int{
"callbackFunc": 1,
"latestLookup": 1,
"updater": 1,
}
require.Equal(t, expectedCalls, calls)
})
t.Run("Golden path callback", func(t *testing.T) {
calls := map[string]int{
"callbackFunc": 0,
"updater": 0,
"latestLookup": 0,
}
callbackFn := CallbackFunc[string, testStatus](func(ctx context.Context, r *Run[string, testStatus], reader io.Reader) (testStatus, error) {
calls["callbackFunc"] += 1
*r.Object = "new data"
return statusEnd, nil
})
updater := func(ctx context.Context, current testStatus, next testStatus, record *Run[string, testStatus], workingVersion uint) error {
calls["updater"] += 1
require.Equal(t, "new data", *record.Object)
return nil
}
latestLookup := func(ctx context.Context, workflowName, foreignID string) (*Record, error) {
calls["latestLookup"] += 1
return current, nil
}
store := func(ctx context.Context, record *Record) error {
calls["store"] += 1
return nil
}
err := processCallback(ctx, w, testStatus(current.Status), callbackFn, current.ForeignID, nil, latestLookup, store, updater)
require.NoError(t, err)
expectedCalls := map[string]int{
"callbackFunc": 1,
"latestLookup": 1,
"updater": 1,
}
require.Equal(t, expectedCalls, calls)
})
t.Run("Skip consume", func(t *testing.T) {
calls := map[string]int{
"callbackFunc": 0,
"updater": 0,
"latestLookup": 0,
}
callbackFn := CallbackFunc[string, testStatus](func(ctx context.Context, r *Run[string, testStatus], reader io.Reader) (testStatus, error) {
calls["callbackFunc"] += 1
return testStatus(skipTypeDefault), nil
})
updater := func(ctx context.Context, current testStatus, next testStatus, record *Run[string, testStatus], workingVersion uint) error {
calls["updater"] += 1
return nil
}
latestLookup := func(ctx context.Context, workflowName, foreignID string) (*Record, error) {
calls["latestLookup"] += 1
return current, nil
}
store := func(ctx context.Context, record *Record) error {
calls["store"] += 1
return nil
}
err := processCallback(ctx, w, testStatus(current.Status), callbackFn, current.ForeignID, nil, latestLookup, store, updater)
require.NoError(t, err)
expectedCalls := map[string]int{
"callbackFunc": 1,
"latestLookup": 1,
"updater": 0,
}
require.Equal(t, expectedCalls, calls)
})
t.Run("Return on lookup error", func(t *testing.T) {
testErr := errors.New("test error")
latestLookup := func(ctx context.Context, workflowName, foreignID string) (*Record, error) {
return nil, testErr
}
err := processCallback(ctx, w, testStatus(current.Status), nil, current.ForeignID, nil, latestLookup, nil, nil)
require.Truef(t, errors.Is(err, testErr), "actual: %s", err.Error())
})
t.Run("Return on callbackFunc error", func(t *testing.T) {
latestLookup := func(ctx context.Context, workflowName, foreignID string) (*Record, error) {
return current, nil
}
callbackFn := CallbackFunc[string, testStatus](func(ctx context.Context, r *Run[string, testStatus], reader io.Reader) (testStatus, error) {
return 0, errors.New("test error")
})
err := processCallback(ctx, w, testStatus(current.Status), callbackFn, current.ForeignID, nil, latestLookup, nil, nil)
require.Equal(t, errors.New("test error"), err)
})
t.Run("Ignore if record is in different state", func(t *testing.T) {
currentRecord := &Record{
Status: int(statusMiddle),
}
latestLookup := func(ctx context.Context, workflowName, foreignID string) (*Record, error) {
return currentRecord, nil
}
err := processCallback(ctx, w, statusStart, nil, current.ForeignID, nil, latestLookup, nil, nil)
require.NoError(t, err)
})
t.Run("Skip if record is stopped (paused)", func(t *testing.T) {
stoppedRecord := &Record{
WorkflowName: "example",
ForeignID: "32948623984623",
RunID: "JHFJDS-LSFKHJSLD-KSJDBLSL",
RunState: RunStatePaused,
Status: int(statusStart),
Object: b,
}
callbackCalled := false
callbackFn := CallbackFunc[string, testStatus](func(ctx context.Context, r *Run[string, testStatus], reader io.Reader) (testStatus, error) {
callbackCalled = true
return statusEnd, nil
})
latestLookup := func(ctx context.Context, workflowName, foreignID string) (*Record, error) {
return stoppedRecord, nil
}
err := processCallback(ctx, w, statusStart, callbackFn, current.ForeignID, nil, latestLookup, nil, nil)
require.NoError(t, err)
require.False(t, callbackCalled, "callback should not be called for stopped records")
})
t.Run("Skip if record is stopped (cancelled)", func(t *testing.T) {
cancelledRecord := &Record{
WorkflowName: "example",
ForeignID: "32948623984623",
RunID: "JHFJDS-LSFKHJSLD-KSJDBLSL",
RunState: RunStateCancelled,
Status: int(statusStart),
Object: b,
}
callbackCalled := false
callbackFn := CallbackFunc[string, testStatus](func(ctx context.Context, r *Run[string, testStatus], reader io.Reader) (testStatus, error) {
callbackCalled = true
return statusEnd, nil
})
latestLookup := func(ctx context.Context, workflowName, foreignID string) (*Record, error) {
return cancelledRecord, nil
}
err := processCallback(ctx, w, statusStart, callbackFn, current.ForeignID, nil, latestLookup, nil, nil)
require.NoError(t, err)
require.False(t, callbackCalled, "callback should not be called for cancelled records")
})
}