-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrency_test.go
More file actions
75 lines (64 loc) · 1.72 KB
/
concurrency_test.go
File metadata and controls
75 lines (64 loc) · 1.72 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
package main
import (
"sync"
"sync/atomic"
"testing"
"time"
)
func TestConcurrency(t *testing.T) {
const (
limit = 5
multiplier = 4
sleepFor = time.Second / 10 // 1/10th of a second should be plenty
expectedElapse = 2 * sleepFor * multiplier
)
var (
concurrent int32
wg sync.WaitGroup
)
cc := newConcurrencyController(limit)
startTime := time.Now()
if len(cc.tokens) != limit {
t.Error("New controller has wrong limit of", len(cc.tokens), "expected", limit)
}
if cap(cc.tokens) != limit {
t.Error("New controller has wrong capacity of", cap(cc.tokens), "expected", limit)
}
for ix := range limit * multiplier {
wg.Add(1)
go func(myx int) {
cc.start()
time.Sleep(sleepFor)
max := atomic.AddInt32(&concurrent, 1)
// fmt.Println(myx, "Active for", max, "after", time.Now().Sub(startTime).Seconds())
if max > limit {
t.Error("Max Inc", max, "GT limit", limit)
}
time.Sleep(sleepFor)
max = atomic.AddInt32(&concurrent, -1)
if max > limit {
t.Error("Max Dec", max, "GT limit", limit)
}
cc.done()
wg.Done()
}(ix)
}
time.Sleep(sleepFor) // All tokens should be consumed after this
l := len(cc.tokens)
if l != 0 {
t.Error(l, "tokens available, expected zero")
}
wg.Wait()
if len(cc.tokens) != cap(cc.tokens) {
t.Error("All tokens should have been returned", len(cc.tokens), cap(cc.tokens))
}
// Should have waited for at least expectedElapse time
dur := time.Now().Sub(startTime)
if dur < expectedElapse {
t.Error("Expected to wait for at least", expectedElapse, "not", dur)
}
// Should have also exhausted all tokens at some point
if !cc.limitReached() {
t.Error("Expected to consume all tokens at some stage during test")
}
}