-
Notifications
You must be signed in to change notification settings - Fork 26
/
saga_test.go
274 lines (216 loc) · 8.8 KB
/
saga_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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package saga
import (
"context"
"errors"
"github.com/stretchr/testify/require"
"reflect"
"testing"
)
type mock struct {
callCounter int
err error
}
func (t *mock) f(ctx context.Context) error {
t.callCounter++
return t.err
}
func TestSuccessfullyExecTwoSteps(t *testing.T) {
s := NewSaga("err4")
m := &mock{}
m2 := &mock{}
comp := &mock{}
require.NoError(t, s.AddStep(&Step{Name: "first", Func: m.f, CompensateFunc: comp.f}))
require.NoError(t, s.AddStep(&Step{Name: "second", Func: m2.f, CompensateFunc: comp.f}))
c := NewCoordinator(context.Background(), context.Background(), s, New())
require.Nil(t, c.Play().ExecutionError)
require.Equal(t, m.callCounter, 1)
require.Equal(t, m2.callCounter, 1)
require.Equal(t, comp.callCounter, 0)
}
func TestSuccessfullyExecTwoSteps_WithCustomizeExecutionID(t *testing.T) {
s := NewSaga("err4")
m := &mock{}
m2 := &mock{}
comp := &mock{}
require.NoError(t, s.AddStep(&Step{Name: "first", Func: m.f, CompensateFunc: comp.f}))
require.NoError(t, s.AddStep(&Step{Name: "second", Func: m2.f, CompensateFunc: comp.f}))
logStore := New()
executionID := RandString()
c := NewCoordinator(context.Background(), context.Background(), s, logStore, executionID)
require.Nil(t, c.Play().ExecutionError)
require.Equal(t, m.callCounter, 1)
require.Equal(t, m2.callCounter, 1)
require.Equal(t, comp.callCounter, 0)
logs, err := logStore.GetAllLogsByExecutionID(executionID)
require.NoError(t, err)
for _, log := range logs {
require.Equal(t, log.ExecutionID, executionID)
}
}
func TestCompensateCalledWhenError(t *testing.T) {
s := NewSaga("err3")
m := &mock{err: errors.New("hello")}
comp := &mock{}
require.NoError(t, s.AddStep(&Step{Name: "single", Func: m.f, CompensateFunc: comp.f}))
c := NewCoordinator(context.Background(), context.Background(), s, New())
require.Error(t, c.Play().ExecutionError)
require.Equal(t, m.callCounter, 1)
require.Equal(t, comp.callCounter, 1)
}
func TestCompensateCalledTwiceForTwoSteps(t *testing.T) {
s := NewSaga("err2")
m := &mock{}
comp := &mock{}
m2 := &mock{err: errors.New("hello")}
require.NoError(t, s.AddStep(&Step{Name: "first", Func: m.f, CompensateFunc: comp.f}))
require.NoError(t, s.AddStep(&Step{Name: "second", Func: m2.f, CompensateFunc: comp.f}))
c := NewCoordinator(context.Background(), context.Background(), s, New())
c.Play()
require.Equal(t, m.callCounter, 1)
require.Equal(t, m2.callCounter, 1)
require.Equal(t, comp.callCounter, 2)
}
func TestCompensateOnlyExecutedSteps(t *testing.T) {
s := NewSaga("hello")
m := &mock{err: errors.New("hello")}
comp := &mock{}
m2 := &mock{}
require.NoError(t, s.AddStep(&Step{Name: "first", Func: m.f, CompensateFunc: comp.f}))
require.NoError(t, s.AddStep(&Step{Name: "second", Func: m2.f, CompensateFunc: comp.f}))
c := NewCoordinator(context.Background(), context.Background(), s, New())
c.Play()
require.Equal(t, m.callCounter, 1)
require.Equal(t, m2.callCounter, 0)
require.Equal(t, comp.callCounter, 1)
}
func TestReturnsError(t *testing.T) {
s := NewSaga("hello")
callCount1 := 0
callCount2 := 0
f1 := func(ctx context.Context) (string, error) {
callCount1++
return "hello", errors.New("some error")
}
f2 := func(ctx context.Context, s string) error {
callCount2++
require.Equal(t, "hello", s)
return nil
}
require.NoError(t, s.AddStep(&Step{Name: "first", Func: f1, CompensateFunc: f2}))
c := NewCoordinator(context.Background(), context.Background(), s, New())
err := c.Play()
require.EqualError(t, err.ExecutionError, "some error")
require.Equal(t, callCount1, 1)
require.Equal(t, callCount2, 1)
}
func TestReturnsErrorWithNilArgument(t *testing.T) {
s := NewSaga("hello")
callCount1 := 0
callCount2 := 0
f1 := func(ctx context.Context) ([]string, error) {
callCount1++
return nil, errors.New("some error")
}
f2 := func(ctx context.Context, s []string) error {
callCount2++
require.Nil(t, s)
return nil
}
require.NoError(t, s.AddStep(&Step{Name: "first", Func: f1, CompensateFunc: f2}))
c := NewCoordinator(context.Background(), context.Background(), s, New())
err := c.Play()
require.EqualError(t, err.ExecutionError, "some error")
require.Equal(t, callCount1, 1)
require.Equal(t, callCount2, 1)
}
func TestCompensateReturnsError(t *testing.T) {
s := NewSaga("hello")
errFunc := func(ctx context.Context) error {
return errors.New("some error")
}
errCompensateFirst := func(ctx context.Context) error {
return errors.New("compensate error 1")
}
errCompensateSecond := func(ctx context.Context) error {
return errors.New("compensate error 2")
}
require.NoError(t, s.AddStep(&Step{Name: "first", Func: (&mock{}).f, CompensateFunc: errCompensateFirst}))
require.NoError(t, s.AddStep(&Step{Name: "second", Func: errFunc, CompensateFunc: errCompensateSecond}))
logStore := New()
c := NewCoordinator(context.Background(), context.Background(), s, logStore)
result := c.Play()
require.EqualError(t, result.ExecutionError, "some error")
require.Len(t, result.CompensateErrors, 2)
require.EqualError(t, result.CompensateErrors[0], "compensate error 2")
require.EqualError(t, result.CompensateErrors[1], "compensate error 1")
logs, err := logStore.GetAllLogsByExecutionID(c.ExecutionID)
require.NoError(t, err)
require.Len(t, logs, 7)
require.Equal(t, logs[0].Type, LogTypeStartSaga)
require.Equal(t, logs[1].Type, LogTypeSagaStepExec)
require.Equal(t, logs[2].Type, LogTypeSagaStepExec)
require.Equal(t, logs[3].Type, LogTypeSagaAbort)
require.Equal(t, logs[4].Type, LogTypeSagaStepCompensate)
require.Equal(t, logs[5].Type, LogTypeSagaStepCompensate)
require.Equal(t, logs[6].Type, LogTypeSagaComplete)
_, err = logStore.GetAllLogsByExecutionID(RandString())
require.Error(t, err)
_, err = logStore.GetStepLogsToCompensate(RandString())
require.Error(t, err)
}
func TestAddStep(t *testing.T) {
s := NewSaga("hello")
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: "hello", CompensateFunc: (&mock{}).f}), "func field is not a func, but string")
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: (&mock{}).f, CompensateFunc: 25}), "func field is not a func, but int")
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: func() {}, CompensateFunc: (&mock{}).f}), "func must have strictly one parameter context.Context")
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: func(c int) {}, CompensateFunc: (&mock{}).f}), "func must have strictly one parameter context.Context")
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: func(ctx context.Context) {}, CompensateFunc: (&mock{}).f}), "func must have at least one out value of type error")
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: func(context.Context) int { return 10 }, CompensateFunc: (&mock{}).f}), "last out parameter of func must be of type error")
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: (&mock{}).f, CompensateFunc: func() {}}), "compensate must have at least one parameter context.Context")
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: (&mock{}).f, CompensateFunc: func(int) {}}), "first parameter of a compensate must be of type context.Context")
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: (&mock{}).f, CompensateFunc: func(context.Context) {}}), "compensate must must return single value of type error")
f1 := func(context.Context) (string, int, error) { return "123", 0, nil }
f2 := func(context.Context, int) error { return nil }
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: f1, CompensateFunc: f2}), "compensate in params not matched to func return values")
f3 := func(context.Context) (string, int, error) { return "123", 0, nil }
f4 := func(context.Context, string, string) error { return nil }
require.EqualError(t, s.AddStep(&Step{Name: "first", Func: f3, CompensateFunc: f4}), "param 1 not matched in func and compensate")
require.Panics(t, func() {
checkOK(false)
})
require.Panics(t, func() {
checkErr(errors.New("hello"))
})
}
type someStruct struct {
IntField int `json:"intField"`
StringField string `json:"stringField"`
}
func TestMarshalResp(t *testing.T) {
f := 10
s := "hello"
th := &someStruct{
IntField: 777,
StringField: "hi, there",
}
fourth := someStruct{
IntField: 8,
StringField: "hello, there",
}
resp := []reflect.Value{
reflect.ValueOf(f),
reflect.ValueOf(s),
reflect.ValueOf(th),
reflect.ValueOf(fourth),
}
payload, err := marshalResp(resp)
require.NoError(t, err)
require.Equal(t, `[10,"hello",{"intField":777,"stringField":"hi, there"},{"intField":8,"stringField":"hello, there"}]`, string(payload))
unm, err := unmarshalParams([]reflect.Type{}, payload)
require.NoError(t, err)
require.Len(t, unm, len(resp))
require.Equal(t, f, resp[0].Interface())
require.Equal(t, s, resp[1].Interface())
require.Equal(t, th, resp[2].Interface())
require.Equal(t, fourth, resp[3].Interface())
}