-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapacitor_test.go
More file actions
207 lines (184 loc) · 4.89 KB
/
capacitor_test.go
File metadata and controls
207 lines (184 loc) · 4.89 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
package capacitor_test
import (
"errors"
"log/slog"
"testing"
"time"
"codeberg.org/matthew/capacitor"
"codeberg.org/matthew/capacitor/bucket/leaky"
"codeberg.org/matthew/capacitor/internal/testutil"
"github.com/google/go-cmp/cmp"
"github.com/valkey-io/valkey-go/mock"
"go.uber.org/mock/gomock"
)
func TestAttempt(t *testing.T) {
cfg := leaky.DefaultConfig()
cases := map[string]struct {
uid string
allowed bool
remaining int
retryAfter int
mockValkey bool
expectedResult capacitor.Result
expectedErr error
}{
"empty uid returns error": {
uid: "",
mockValkey: false,
expectedResult: capacitor.Result{},
expectedErr: capacitor.ErrEmptyUID,
},
"request allowed": {
uid: "user:1",
allowed: true,
remaining: 9,
retryAfter: 0,
mockValkey: true,
expectedResult: capacitor.Result{
Allowed: true,
Remaining: 9,
Limit: 20,
},
},
"request denied": {
uid: "user:1",
allowed: false,
remaining: 0,
retryAfter: 1,
mockValkey: true,
expectedResult: capacitor.Result{
Allowed: false,
Remaining: 0,
Limit: 20,
RetryAfter: 1 * time.Second,
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
client := mock.NewClient(ctrl)
if c.mockValkey {
client.EXPECT().
Do(gomock.Any(), gomock.Any()).
Return(mock.Result(mock.ValkeyArray(
mock.ValkeyInt64(testutil.Btoi(c.allowed)),
mock.ValkeyInt64(int64(c.remaining)),
mock.ValkeyInt64(int64(c.retryAfter)),
)))
}
lim := leaky.New(client, cfg)
actualRes, err := lim.Attempt(t.Context(), c.uid)
if !errors.Is(err, c.expectedErr) {
t.Fatalf("Attempt() error; got = %v, want = %v", err, c.expectedErr)
}
if diff := cmp.Diff(c.expectedResult, actualRes); diff != "" {
t.Errorf("capacitor.Result mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestAttempt_Fallback(t *testing.T) {
cfg := leaky.DefaultConfig()
cases := map[string]struct {
fallback capacitor.FallbackStrategy
expectedResult capacitor.Result
}{
"fail open on valkey error": {
fallback: capacitor.FallbackFailOpen,
expectedResult: capacitor.Result{
Allowed: true,
Remaining: 0,
Limit: 20,
},
},
"fail closed on valkey error": {
fallback: capacitor.FallbackFailClosed,
expectedResult: capacitor.Result{
Allowed: false,
Remaining: 0,
Limit: 20,
RetryAfter: 1 * time.Second,
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
client := mock.NewClient(ctrl)
client.EXPECT().
Do(gomock.Any(), gomock.Any()).
Return(mock.Result(mock.ValkeyError("ERR test error")))
lim := leaky.New(client, cfg,
capacitor.WithFallback(c.fallback),
capacitor.WithLogger(slog.Default()),
)
actualRes, err := lim.Attempt(t.Context(), "user:1")
if err == nil {
t.Fatal("expected error, got nil")
}
if diff := cmp.Diff(c.expectedResult, actualRes); diff != "" {
t.Errorf("capacitor.Result mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestAttempt_Metrics(t *testing.T) {
cfg := leaky.DefaultConfig()
cases := map[string]struct {
uid string
allowed bool
remaining int
retryAfter int
expectAttempts []string
expectDenied []string
expectLatencies int
}{
"allowed records attempt and latency": {
uid: "user:1",
allowed: true,
remaining: 9,
retryAfter: 0,
expectAttempts: []string{"user:1"},
expectDenied: nil,
expectLatencies: 1,
},
"denied records attempt, denied, and latency": {
uid: "user:2",
allowed: false,
remaining: 0,
retryAfter: 1,
expectAttempts: []string{"user:2"},
expectDenied: []string{"user:2"},
expectLatencies: 1,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
client := mock.NewClient(ctrl)
client.EXPECT().
Do(gomock.Any(), gomock.Any()).
Return(mock.Result(mock.ValkeyArray(
mock.ValkeyInt64(testutil.Btoi(c.allowed)),
mock.ValkeyInt64(int64(c.remaining)),
mock.ValkeyInt64(int64(c.retryAfter)),
)))
mMock := &testutil.MetricsMock{}
lim := leaky.New(client, cfg, capacitor.WithMetrics(mMock))
_, err := lim.Attempt(t.Context(), c.uid)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if diff := cmp.Diff(c.expectAttempts, mMock.Attempts); diff != "" {
t.Errorf("attempts mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(c.expectDenied, mMock.Denied); diff != "" {
t.Errorf("denied mismatch (-want +got):\n%s", diff)
}
if mMock.Latencies != c.expectLatencies {
t.Errorf("latencies = %d, want %d", mMock.Latencies, c.expectLatencies)
}
})
}
}