-
Notifications
You must be signed in to change notification settings - Fork 313
/
limiter_barrier_test.go
79 lines (76 loc) · 1.52 KB
/
limiter_barrier_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
package main
import (
"sync"
"sync/atomic"
"testing"
)
func TestNoopLimiterCounterBarrierCombination(t *testing.T) {
expectations := []uint64{
1, 15, 50, 100, 150, 500, 1000, 1500, 5000,
}
done := make(chan struct{})
for _, count := range expectations {
b := newCountingCompletionBarrier(count)
var lim limiter = &nooplimiter{}
counter := uint64(0)
numParties := 10
var wg sync.WaitGroup
wg.Add(numParties)
for i := 0; i < numParties; i++ {
go func() {
defer wg.Done()
for b.tryGrabWork() {
lim.pace(done)
atomic.AddUint64(&counter, 1)
b.jobDone()
}
}()
}
wg.Wait()
if counter != count {
t.Error(count, counter)
}
}
}
func TestBucketLimiterCounterBarrierCombination(t *testing.T) {
expectations := []struct {
count, rate uint64
}{
{10, 100},
{10, 1000},
{100, 1000},
{100, 10000},
{1000, 10000},
{1000, 100000},
}
done := make(chan struct{})
var expWg sync.WaitGroup
expWg.Add(len(expectations))
for i := range expectations {
exp := expectations[i]
go func() {
defer expWg.Done()
b := newCountingCompletionBarrier(exp.count)
lim := newBucketLimiter(exp.rate)
counter := uint64(0)
numParties := 10
var wg sync.WaitGroup
wg.Add(numParties)
for i := 0; i < numParties; i++ {
go func() {
defer wg.Done()
for b.tryGrabWork() {
lim.pace(done)
atomic.AddUint64(&counter, 1)
b.jobDone()
}
}()
}
wg.Wait()
if counter != exp.count {
t.Error(exp.count, counter)
}
}()
}
expWg.Wait()
}