-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_gorm_test.go
161 lines (149 loc) · 3.64 KB
/
driver_gorm_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
package ratelimiter
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
func TestGormForUpdate(t *testing.T) {
ctx := context.Background()
driver, err := InitGormDriver(ctx, db)
if err != nil {
t.Fatal(err)
}
limiter := New(driver)
key := "TestGormForUpdate"
durationPerToken := 100 * time.Millisecond
burst := 10
{
r, err := limiter.Reserve(ctx, &ReserveRequest{
Key: key,
DurationPerToken: durationPerToken,
Burst: burst,
Tokens: 1,
MaxFutureReserve: 0,
})
require.NoError(t, err)
require.True(t, r.OK)
}
sig := make(chan struct{})
var a, b, c, d, e time.Time
var nowB, nowE time.Time
var errG errgroup.Group
errG.Go(func() error {
a = time.Now()
ctx := context.WithValue(ctx, ctxKeyAfterQuery{}, func(kv kvWrapper) {
nowB = kv.Now
b = time.Now()
// make another goroutine to continue after for update
close(sig)
// test whether the second goroutine can continue before the sleep done
time.Sleep(time.Second)
d = time.Now()
})
r, err := limiter.Reserve(ctx, &ReserveRequest{
Key: key,
DurationPerToken: durationPerToken,
Burst: burst,
Tokens: 5,
MaxFutureReserve: 0,
})
if err != nil {
return err
}
require.True(t, r.OK)
return nil
})
errG.Go(func() error {
<-sig
c = time.Now()
ctx := context.WithValue(ctx, ctxKeyAfterQuery{}, func(kv kvWrapper) {
nowE = kv.Now // need to ensure now is the time after blocking
e = time.Now()
})
r, err := limiter.Reserve(ctx, &ReserveRequest{
Key: key,
DurationPerToken: durationPerToken,
Burst: burst,
Tokens: 3,
MaxFutureReserve: 0,
})
if err != nil {
return err
}
require.True(t, r.OK)
return nil
})
if err := errG.Wait(); err != nil {
t.Fatal(err)
}
t.Logf("a: %v", a)
t.Logf("b: %v", b)
t.Logf("nowB: %v", nowB)
t.Logf("c: %v", c)
t.Logf("d: %v", d)
t.Logf("e: %v", e)
t.Logf("nowE: %v", nowE)
require.Truef(t, a.Before(b), "a: %v, b: %v", a, b)
require.Truef(t, b.Before(c), "b: %v, c: %v", b, c)
require.Truef(t, c.Before(d), "c: %v, d: %v", c, d)
require.Truef(t, d.Before(e), "d: %v, e: %v", d, e)
require.Truef(t, b.Sub(nowB) < 100*time.Millisecond, "b: %v, nowB: %v", b, nowB)
// ensure now is the time after blocking
require.Truef(t, e.Sub(nowE) < 100*time.Millisecond, "e: %v, nowE: %v", e, nowE)
}
func TestGormDuplicateCreate(t *testing.T) {
ctx := context.Background()
driver, err := InitGormDriver(ctx, db)
if err != nil {
t.Fatal(err)
}
limiter := New(driver)
key := "TestGormDuplicateCreate"
durationPerToken := 100 * time.Millisecond
burst := 10
sig := make(chan struct{})
var afterQueryCount atomic.Int64
afterQuery := func(kv kvWrapper) {
if afterQueryCount.Add(1) == 2 {
close(sig)
}
<-sig
}
var errG errgroup.Group
errG.Go(func() error {
ctx := context.WithValue(ctx, ctxKeyAfterQuery{}, afterQuery)
r, err := limiter.Reserve(ctx, &ReserveRequest{
Key: key,
DurationPerToken: durationPerToken,
Burst: burst,
Tokens: 5,
MaxFutureReserve: 0,
})
if err != nil {
return err
}
require.True(t, r.OK)
return nil
})
errG.Go(func() error {
ctx := context.WithValue(ctx, ctxKeyAfterQuery{}, afterQuery)
r, err := limiter.Reserve(ctx, &ReserveRequest{
Key: key,
DurationPerToken: durationPerToken,
Burst: burst,
Tokens: 5,
MaxFutureReserve: 0,
})
if err != nil {
return err
}
require.True(t, r.OK)
return nil
})
if err := errG.Wait(); err != nil {
t.Fatal(err)
}
}