-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathquota_test.go
More file actions
206 lines (188 loc) · 5.08 KB
/
Copy pathquota_test.go
File metadata and controls
206 lines (188 loc) · 5.08 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
package model
import (
"fmt"
"sync"
"sync/atomic"
"testing"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var quotaTestSeq atomic.Int64
func setupQuotaTestDB(t *testing.T) {
t.Helper()
dsn := fmt.Sprintf("file:quota-%s-%d?mode=memory&cache=shared&_busy_timeout=5000", t.Name(), quotaTestSeq.Add(1))
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := db.AutoMigrate(&User{}, &Token{}); err != nil {
t.Fatalf("migrate: %v", err)
}
DB = db
}
func createTestUser(t *testing.T, quota int64) *User {
t.Helper()
seq := quotaTestSeq.Add(1)
user := &User{
Username: fmt.Sprintf("u%d", seq),
Password: "password1234",
Quota: quota,
Status: UserStatusEnabled,
Role: RoleCommonUser,
AccessToken: fmt.Sprintf("%032d", seq),
AffCode: fmt.Sprintf("a%d", seq),
}
if err := DB.Create(user).Error; err != nil {
t.Fatalf("create user: %v", err)
}
return user
}
func createTestToken(t *testing.T, userId int, remain int64) *Token {
t.Helper()
seq := quotaTestSeq.Add(1)
token := &Token{
UserId: userId,
Key: fmt.Sprintf("%048d", seq),
Name: "quota-test",
Status: TokenStatusEnabled,
RemainQuota: remain,
ExpiredTime: -1,
}
if err := DB.Create(token).Error; err != nil {
t.Fatalf("create token: %v", err)
}
return token
}
func TestDecreaseUserQuotaSequentialOversellDoesNotGoNegative(t *testing.T) {
setupQuotaTestDB(t)
user := createTestUser(t, 100)
if err := decreaseUserQuota(user.Id, 60); err != nil {
t.Fatalf("first decrease: %v", err)
}
err := decreaseUserQuota(user.Id, 60)
remaining, qErr := GetUserQuota(user.Id)
if qErr != nil {
t.Fatalf("GetUserQuota: %v", qErr)
}
if remaining < 0 {
t.Fatalf("user quota went negative: %d", remaining)
}
if err == nil {
t.Fatalf("expected oversell to fail, remaining=%d", remaining)
}
if remaining != 40 {
t.Fatalf("remaining quota = %d, want 40", remaining)
}
}
func TestDecreaseTokenQuotaSequentialOversellDoesNotGoNegative(t *testing.T) {
setupQuotaTestDB(t)
user := createTestUser(t, 1000)
token := createTestToken(t, user.Id, 100)
if err := decreaseTokenQuota(token.Id, 60); err != nil {
t.Fatalf("first decrease: %v", err)
}
err := decreaseTokenQuota(token.Id, 60)
got, gErr := GetTokenById(token.Id)
if gErr != nil {
t.Fatalf("GetTokenById: %v", gErr)
}
if got.RemainQuota < 0 {
t.Fatalf("token remain_quota went negative: %d", got.RemainQuota)
}
if err == nil {
t.Fatalf("expected oversell to fail, remain_quota=%d", got.RemainQuota)
}
if got.RemainQuota != 40 {
t.Fatalf("remain_quota = %d, want 40", got.RemainQuota)
}
if got.UsedQuota != 60 {
t.Fatalf("used_quota = %d, want 60", got.UsedQuota)
}
}
func TestDecreaseUserQuotaExactDrainThenReject(t *testing.T) {
setupQuotaTestDB(t)
user := createTestUser(t, 100)
if err := decreaseUserQuota(user.Id, 100); err != nil {
t.Fatalf("exact drain: %v", err)
}
err := decreaseUserQuota(user.Id, 1)
remaining, qErr := GetUserQuota(user.Id)
if qErr != nil {
t.Fatalf("GetUserQuota: %v", qErr)
}
if remaining != 0 {
t.Fatalf("remaining quota = %d, want 0", remaining)
}
if err == nil {
t.Fatal("expected decrease below zero to fail")
}
}
func TestDecreaseUserQuotaConcurrentOversellDoesNotGoNegative(t *testing.T) {
setupQuotaTestDB(t)
user := createTestUser(t, 100)
const workers = 10
const each = int64(60)
var success atomic.Int64
var wg sync.WaitGroup
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
if err := decreaseUserQuota(user.Id, each); err == nil {
success.Add(1)
}
}()
}
wg.Wait()
remaining, err := GetUserQuota(user.Id)
if err != nil {
t.Fatalf("GetUserQuota: %v", err)
}
if remaining < 0 {
t.Fatalf("user quota went negative under concurrent decrease: %d (successes=%d)", remaining, success.Load())
}
if success.Load() != 1 {
t.Fatalf("successful decreases = %d, want 1; remaining=%d", success.Load(), remaining)
}
if remaining != 40 {
t.Fatalf("remaining quota = %d, want 40", remaining)
}
}
func TestDecreaseTokenQuotaConcurrentOversellDoesNotGoNegative(t *testing.T) {
setupQuotaTestDB(t)
user := createTestUser(t, 1000)
token := createTestToken(t, user.Id, 100)
const workers = 10
const each = int64(60)
var success atomic.Int64
var wg sync.WaitGroup
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
defer wg.Done()
if err := decreaseTokenQuota(token.Id, each); err == nil {
success.Add(1)
}
}()
}
wg.Wait()
got, err := GetTokenById(token.Id)
if err != nil {
t.Fatalf("GetTokenById: %v", err)
}
if got.RemainQuota < 0 {
t.Fatalf("token remain_quota went negative under concurrent decrease: %d (successes=%d)", got.RemainQuota, success.Load())
}
if success.Load() != 1 {
t.Fatalf("successful decreases = %d, want 1; remain_quota=%d", success.Load(), got.RemainQuota)
}
if got.RemainQuota != 40 {
t.Fatalf("remain_quota = %d, want 40", got.RemainQuota)
}
if got.UsedQuota != 60 {
t.Fatalf("used_quota = %d, want 60", got.UsedQuota)
}
}