-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapacitor_test.go
More file actions
142 lines (125 loc) · 3.19 KB
/
Copy pathcapacitor_test.go
File metadata and controls
142 lines (125 loc) · 3.19 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
package capacitor_test
import (
"errors"
"log/slog"
"testing"
"time"
"codeberg.org/matthew/capacitor"
"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 := capacitor.NewLeakyBucketDefaultConfig()
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 := capacitor.NewLeakyBucket(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 := capacitor.NewLeakyBucketDefaultConfig()
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 := capacitor.NewLeakyBucket(
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)
}
})
}
}