-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce_quota_usage_test.go
268 lines (201 loc) · 9.99 KB
/
reduce_quota_usage_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
package andromeda_test
import (
"context"
"errors"
"fmt"
"github.com/golang/mock/gomock"
"github.com/ramadani/andromeda"
"github.com/ramadani/andromeda/mocks"
"github.com/stretchr/testify/assert"
"testing"
)
func TestReduceQuotaUsage(t *testing.T) {
ctx := context.TODO()
mockCtrl := gomock.NewController(t)
mockCache := mocks.NewMockCache(mockCtrl)
mockGetQuotaUsageKey := mocks.NewMockGetQuotaKey(mockCtrl)
mockNext := mocks.NewMockUpdateQuotaUsage(mockCtrl)
option := andromeda.ReduceUsageOption{}
reduceQuotaUsage := andromeda.NewReduceQuotaUsage(mockCache, mockGetQuotaUsageKey, mockNext, option)
t.Run("ErrorGetQuotaUsageKey", func(t *testing.T) {
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
mockErr := errors.New("unexpected")
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return("", mockErr)
res, err := reduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Nil(t, res)
assert.EqualError(t, err, mockErr.Error())
})
t.Run("DoNextWhenQuotaNotFound", func(t *testing.T) {
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
mockRes := "result"
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return("", andromeda.ErrQuotaNotFound)
mockNext.EXPECT().Do(ctx, quotaUsageReq).Return(mockRes, nil)
res, err := reduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Equal(t, mockRes, res)
assert.Nil(t, err)
})
t.Run("DoNextWhenQuotaNotFoundWithFormat", func(t *testing.T) {
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
mockRes := "result"
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return("", fmt.Errorf("error: %w", andromeda.ErrQuotaNotFound))
mockNext.EXPECT().Do(ctx, quotaUsageReq).Return(mockRes, nil)
res, err := reduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Equal(t, mockRes, res)
assert.Nil(t, err)
})
t.Run("ErrorDecrementUsage", func(t *testing.T) {
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockErr := errors.New("unexpected")
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, quotaUsageReq.Usage).Return(int64(0), mockErr)
res, err := reduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Nil(t, res)
assert.Error(t, err)
assert.True(t, errors.Is(err, andromeda.ErrReduceQuotaUsage))
})
t.Run("ReverseQuotaUsageWhenTotalUsageLessThanZero", func(t *testing.T) {
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, quotaUsageReq.Usage).Return(int64(-1), nil)
mockCache.EXPECT().IncrBy(ctx, key, quotaUsageReq.Usage).Return(int64(999), nil)
res, err := reduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Nil(t, res)
assert.Error(t, err)
assert.True(t, errors.Is(err, andromeda.ErrInvalidMinQuotaUsage))
})
t.Run("ErrorReverseQuotaUsageWhenTotalUsageLessThanZero", func(t *testing.T) {
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockErr := errors.New("unexpected")
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, quotaUsageReq.Usage).Return(int64(-1), nil)
mockCache.EXPECT().IncrBy(ctx, key, quotaUsageReq.Usage).Return(int64(0), mockErr)
res, err := reduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Nil(t, res)
assert.Error(t, err)
assert.True(t, errors.Is(err, andromeda.ErrAddQuotaUsage))
})
t.Run("IncrementUsageWhenNextHasError", func(t *testing.T) {
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockErr := errors.New("unexpected")
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, quotaUsageReq.Usage).Return(int64(0), nil)
mockNext.EXPECT().Do(ctx, quotaUsageReq).Return(nil, mockErr)
mockCache.EXPECT().IncrBy(ctx, key, quotaUsageReq.Usage).Return(quotaUsageReq.Usage, nil)
res, err := reduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Nil(t, res)
assert.EqualError(t, err, mockErr.Error())
})
t.Run("ErrorIncrementUsageWhenNextHasError", func(t *testing.T) {
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockErr := errors.New("unexpected")
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, quotaUsageReq.Usage).Return(int64(0), nil)
mockNext.EXPECT().Do(ctx, quotaUsageReq).Return(nil, mockErr)
mockCache.EXPECT().IncrBy(ctx, key, quotaUsageReq.Usage).Return(int64(0), mockErr)
res, err := reduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Nil(t, res)
assert.Error(t, err)
assert.True(t, errors.Is(err, andromeda.ErrAddQuotaUsage))
})
t.Run("SucceedReduceQuotaUsage", func(t *testing.T) {
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockRes := "result"
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, quotaUsageReq.Usage).Return(int64(0), nil)
mockNext.EXPECT().Do(ctx, quotaUsageReq).Return(mockRes, nil)
res, err := reduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Equal(t, mockRes, res)
assert.Nil(t, err)
})
t.Run("SucceedReduceQuotaUsageWithModifiedUsage", func(t *testing.T) {
opt := andromeda.ReduceUsageOption{ModifiedUsage: 1}
newReduceQuotaUsage := andromeda.NewReduceQuotaUsage(mockCache, mockGetQuotaUsageKey, mockNext, opt)
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockRes := "result"
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, opt.ModifiedUsage).Return(int64(0), nil)
mockNext.EXPECT().Do(ctx, quotaUsageReq).Return(mockRes, nil)
res, err := newReduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Equal(t, mockRes, res)
assert.Nil(t, err)
})
t.Run("ListenOnError", func(t *testing.T) {
mockListener := mocks.NewMockUpdateQuotaUsageListener(mockCtrl)
opt := andromeda.ReduceUsageOption{Listener: mockListener}
newReduceQuotaUsage := andromeda.NewReduceQuotaUsage(mockCache, mockGetQuotaUsageKey, mockNext, opt)
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockErr := errors.New("unexpected")
theErr := fmt.Errorf("%w: %v", andromeda.ErrReduceQuotaUsage, mockErr)
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, quotaUsageReq.Usage).Return(int64(0), mockErr)
mockListener.EXPECT().OnError(ctx, quotaUsageReq, theErr)
res, err := newReduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Nil(t, res)
assert.Error(t, err)
assert.True(t, errors.Is(err, andromeda.ErrReduceQuotaUsage))
})
t.Run("NoListenOnNextError", func(t *testing.T) {
mockListener := mocks.NewMockUpdateQuotaUsageListener(mockCtrl)
opt := andromeda.ReduceUsageOption{Listener: mockListener}
newReduceQuotaUsage := andromeda.NewReduceQuotaUsage(mockCache, mockGetQuotaUsageKey, mockNext, opt)
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockErr := errors.New("unexpected")
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, quotaUsageReq.Usage).Return(int64(0), nil)
mockNext.EXPECT().Do(ctx, quotaUsageReq).Return(nil, mockErr)
mockCache.EXPECT().IncrBy(ctx, key, quotaUsageReq.Usage).Return(quotaUsageReq.Usage, nil)
res, err := newReduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Nil(t, res)
assert.EqualError(t, err, mockErr.Error())
})
t.Run("ListenOnSuccess", func(t *testing.T) {
mockListener := mocks.NewMockUpdateQuotaUsageListener(mockCtrl)
opt := andromeda.ReduceUsageOption{Listener: mockListener}
newReduceQuotaUsage := andromeda.NewReduceQuotaUsage(mockCache, mockGetQuotaUsageKey, mockNext, opt)
defer mockCtrl.Finish()
quotaUsageReq := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
quotaReq := &andromeda.QuotaRequest{QuotaID: quotaUsageReq.QuotaID, Data: quotaUsageReq.Data}
key := "key-123"
mockRes := "result"
mockGetQuotaUsageKey.EXPECT().Do(ctx, quotaReq).Return(key, nil)
mockCache.EXPECT().DecrBy(ctx, key, quotaUsageReq.Usage).Return(int64(10), nil)
mockNext.EXPECT().Do(ctx, quotaUsageReq).Return(mockRes, nil)
mockListener.EXPECT().OnSuccess(ctx, quotaUsageReq, int64(10))
res, err := newReduceQuotaUsage.Do(ctx, quotaUsageReq)
assert.Equal(t, mockRes, res)
assert.Nil(t, err)
})
}