-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckpoint_test.go
More file actions
177 lines (157 loc) · 4.75 KB
/
checkpoint_test.go
File metadata and controls
177 lines (157 loc) · 4.75 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
package checkpoint
import (
"errors"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestCallbacks(t *testing.T) {
t.Run("Custom working flow", func(t *testing.T) {
var beforeReq, afterReq, successReq, failureReq int
callbacks := Callbacks{
BeforeRequest: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
beforeReq++
},
AfterRequest: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
afterReq++
},
OnSuccess: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
successReq++
},
OnFailure: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
failureReq++
},
}
cp := NewCheckpoint(&callbacks)
cp.Execute(func() (interface{}, error) {
return nil, nil
})
assert.Equal(t, 1, beforeReq)
assert.Equal(t, 1, afterReq)
assert.Equal(t, 1, successReq)
assert.Equal(t, 0, failureReq)
cp.Execute(func() (interface{}, error) {
return nil, nil
})
cp.Execute(func() (interface{}, error) {
return nil, nil
})
cp.Execute(func() (interface{}, error) {
return nil, nil
})
assert.Equal(t, 4, beforeReq)
assert.Equal(t, 4, afterReq)
assert.Equal(t, 4, successReq)
assert.Equal(t, 0, failureReq)
})
t.Run("Custom failing flow", func(t *testing.T) {
var beforeReq, afterReq, successReq, failureReq int
callbacks := Callbacks{
BeforeRequest: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
beforeReq++
},
AfterRequest: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
afterReq++
},
OnSuccess: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
successReq++
},
OnFailure: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
failureReq++
},
}
cp := NewCheckpoint(&callbacks)
cp.Execute(func() (interface{}, error) {
return nil, errors.New("failing")
})
assert.Equal(t, 1, beforeReq)
assert.Equal(t, 1, afterReq)
assert.Equal(t, 0, successReq)
assert.Equal(t, 1, failureReq)
cp.Execute(func() (interface{}, error) {
return nil, errors.New("failing")
})
cp.Execute(func() (interface{}, error) {
return nil, errors.New("failing")
})
cp.Execute(func() (interface{}, error) {
return nil, errors.New("failing")
})
assert.Equal(t, 4, beforeReq)
assert.Equal(t, 4, afterReq)
assert.Equal(t, 0, successReq)
assert.Equal(t, 4, failureReq)
})
t.Run("Custom global flow", func(t *testing.T) {
var beforeReq, afterReq, successReq, failureReq int
callbacks := Callbacks{
BeforeRequest: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
beforeReq++
},
AfterRequest: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
afterReq++
},
OnSuccess: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
successReq++
},
OnFailure: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
failureReq++
},
}
cp := NewCheckpoint(&callbacks)
for i := 0; i < 20; i++ {
cp.Execute(func() (interface{}, error) {
return nil, errors.New("failing")
})
}
for i := 0; i < 10; i++ {
cp.Execute(func() (interface{}, error) {
return nil, nil
})
}
assert.Equal(t, 30, beforeReq)
assert.Equal(t, 30, afterReq)
assert.Equal(t, 10, successReq)
assert.Equal(t, 20, failureReq)
})
}
func TestValuesScope(t *testing.T) {
t.Run("Test working flow", func(t *testing.T) {
id := uuid.New()
callbacks := Callbacks{
BeforeRequest: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
if val, ok := globalValues[id.String()]; !ok {
globalValues[id.String()] = 1
} else {
globalValues[id.String()] = val.(int) + 1
}
assert.Empty(t, reqValues)
reqValues[id.String()] = "Local values"
},
AfterRequest: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
assert.NotEmpty(t, reqValues)
assert.Equal(t, reqValues[id.String()], "Local values")
},
OnSuccess: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
assert.NotEmpty(t, reqValues)
assert.Equal(t, reqValues[id.String()], "Local values")
},
OnFailure: func(globalValues map[string]interface{}, reqValues map[string]interface{}) {
},
}
cp := NewCheckpoint(&callbacks)
cp.Execute(func() (interface{}, error) {
return nil, nil
})
cp.Execute(func() (interface{}, error) {
return nil, nil
})
cp.Execute(func() (interface{}, error) {
return nil, nil
})
cp.Execute(func() (interface{}, error) {
return nil, nil
})
assert.Equal(t, cp.Values[id.String()], 4)
})
}