-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpool_test.go
169 lines (130 loc) · 3 KB
/
pool_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
package main
import (
"fmt"
"math/rand"
"runtime"
"sync"
"testing"
"time"
)
type integrityTester struct {
sync.Mutex
lastValue uint64
lastTime time.Time
seen []uint64
}
func newIntegrityTester() *integrityTester {
return &integrityTester{
lastValue: 0,
lastTime: time.Now(),
seen: make([]uint64, 0),
}
}
func (it *integrityTester) check(val uint64) error {
now := time.Now()
it.Lock()
defer it.Unlock()
if val < it.lastValue {
// this is okay, as long as it's not more than a second in the past.
if it.lastTime.Before(now.Add(-1 * time.Second)) {
return fmt.Errorf("value %v before %v and more than a second in that past (%v)", val, it.lastValue, now.Sub(it.lastTime))
}
}
for _, i := range it.seen {
if i == val {
return fmt.Errorf("already seen %v", val)
}
}
it.lastValue = val
it.lastTime = now
it.seen = append(it.seen, val)
return nil
}
func newTestPool(delay time.Duration) *pool {
notify := make(chan uint64)
pool := newPool(notify)
i := uint64(1000)
var mutex sync.Mutex
go func() {
for size := range notify {
time.Sleep(delay)
mutex.Lock()
start := i
i = i + size
mutex.Unlock()
pool.addRange(newIDRange(start, start+size, ""))
}
}()
return pool
}
func getRange(start uint64, end uint64, pool *pool, t *testing.T) {
for i := uint64(start); i < end; i++ {
id := pool.getID()
if i != id {
t.Errorf("invalid id %v, expected %v", id, i)
}
}
}
func getTicker(delay int32, amount int) chan bool {
ticker := make(chan bool, amount)
// do this in it's own thread to mimic the real world a bit better.
go func() {
for i := 0; i < amount; i++ {
time.Sleep(time.Duration(rand.Int31n(delay)) * time.Millisecond)
ticker <- true
}
close(ticker)
}()
return ticker
}
func getAmount(amount int, pool *pool, t *testing.T) {
var last uint64
for range getTicker(25, amount) {
id := pool.getID()
if id <= last {
t.Error("got the same id twice!")
}
last = id
}
}
func TestAddAndConsumeRange(t *testing.T) {
pool := newTestPool(0)
pool.addRange(newIDRange(100, 110, ""))
getRange(100, 110, pool, t)
}
func TestAddAndConsumeMultipleRanges(t *testing.T) {
pool := newTestPool(0)
pool.addRange(newIDRange(100, 105, ""))
pool.addRange(newIDRange(150, 155, ""))
pool.addRange(newIDRange(175, 180, ""))
getRange(100, 105, pool, t)
getRange(150, 155, pool, t)
getRange(175, 180, pool, t)
}
func TestAutoAddRanges(t *testing.T) {
pool := newTestPool(100 * time.Millisecond)
// just go!
getAmount(1000, pool, t)
}
func TestMultipleThreads(t *testing.T) {
// just make super-sure we're in a multi-threaded environmet
runtime.GOMAXPROCS(runtime.NumCPU())
tester := newIntegrityTester()
pool := newTestPool(100)
threads := runtime.NumCPU()
done := make(chan bool)
run := func() {
for range getTicker(500, 50) {
if err := tester.check(pool.getID()); err != nil {
t.Error(err.Error())
}
}
done <- true
}
for i := 0; i < threads; i++ {
go run()
}
for i := 0; i < threads; i++ {
<-done
}
}