-
Notifications
You must be signed in to change notification settings - Fork 36
/
limiter_test.go
172 lines (144 loc) · 3.48 KB
/
limiter_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
package limiter_test
import (
"errors"
"log"
"net/http"
"sync"
"sync/atomic"
"testing"
"github.com/korovkin/limiter"
. "github.com/onsi/gomega"
)
func TestExample(t *testing.T) {
RegisterTestingT(t)
t.Run("TestExample", func(*testing.T) {
x := int32(1000)
limit := limiter.NewConcurrencyLimiter(10)
for i := 0; i < 1000; i++ {
limit.Execute(func() {
// do some work:
atomic.AddInt32(&x, -1)
})
}
limit.WaitAndClose()
Expect(limit.GetNumInProgress()).To(BeEquivalentTo(0))
Expect(x).To(BeEquivalentTo(0))
})
}
func TestLimit(t *testing.T) {
RegisterTestingT(t)
t.Run("TestLimit", func(*testing.T) {
LIMIT := 10
N := 100
c := limiter.NewConcurrencyLimiter(LIMIT)
m := map[int]bool{}
lock := &sync.Mutex{}
max := int32(0)
for i := 0; i < N; i++ {
x := i
c.Execute(func() {
lock.Lock()
m[x] = true
currentMax := c.GetNumInProgress()
if currentMax >= max {
max = currentMax
}
lock.Unlock()
})
}
// wait until the above completes
c.WaitAndClose()
Expect(max).To(BeEquivalentTo(10))
Expect(len(m)).To(BeEquivalentTo(N))
Expect(c.GetNumInProgress()).To(BeEquivalentTo(0))
_, err := c.Execute(func() {
log.Println("more")
})
Expect(err).ToNot(BeNil())
})
}
func TestExecuteWithTicket(t *testing.T) {
RegisterTestingT(t)
t.Run("TestExecuteWithTicket", func(*testing.T) {
LIMIT := 10
N := 100
c := limiter.NewConcurrencyLimiter(LIMIT)
m := map[int]int{}
lock := &sync.Mutex{}
for i := 0; i < N; i++ {
c.ExecuteWithTicket(func(ticket int) {
lock.Lock()
m[ticket] += 1
Expect(ticket).To(BeNumerically("<", LIMIT))
lock.Unlock()
})
}
c.WaitAndClose()
sum := 0
for _, count := range m {
sum += count
}
Expect(sum).To(BeEquivalentTo(N))
Expect(c.GetNumInProgress()).To(BeEquivalentTo(0))
_, err := c.Execute(func() {
log.Println("more ...")
})
Expect(err).ToNot(BeNil())
})
}
func TestConcurrentIO(t *testing.T) {
RegisterTestingT(t)
t.Run("TestConcurrentIO", func(*testing.T) {
c := limiter.NewConcurrencyLimiter(10)
httpGoogle := int(0)
c.Execute(func() {
resp, err := http.Get("https://www.google.com/")
Expect(err).To(BeNil())
defer resp.Body.Close()
httpGoogle = resp.StatusCode
})
httpApple := int(0)
c.Execute(func() {
resp, err := http.Get("https://www.apple.com/")
Expect(err).To(BeNil())
defer resp.Body.Close()
httpApple = resp.StatusCode
})
c.WaitAndClose()
Expect(httpGoogle).To(BeEquivalentTo(200))
Expect(httpApple).To(BeEquivalentTo(200))
})
}
func TestConcurrently(t *testing.T) {
RegisterTestingT(t)
t.Run("TestConcurrently", func(*testing.T) {
a := errors.New("error a")
b := errors.New("error b")
completed := int32(0)
concurrently := limiter.NewConcurrencyLimiterForIO(limiter.DefaultConcurrencyLimitIO)
concurrently.Execute(func() {
atomic.AddInt32(&completed, 1)
// Do some really slow IO ...
// keep the error:
concurrently.FirstErrorStore(a)
})
concurrently.Execute(func() {
atomic.AddInt32(&completed, 1)
// Do some really slow IO ...
// keep the error:
concurrently.FirstErrorStore(b)
})
concurrently.WaitAndClose()
Expect(completed).To(BeEquivalentTo(2))
firstErr := concurrently.FirstErrorGet()
Expect(firstErr).ToNot(BeNil())
Expect(firstErr == a || firstErr == b).To(BeTrue())
})
}
func TestEmpty(t *testing.T) {
RegisterTestingT(t)
t.Run("TestEmpty", func(*testing.T) {
c := limiter.NewConcurrencyLimiter(10)
c.WaitAndClose()
})
}