-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkmutex_test.go
203 lines (181 loc) · 3.77 KB
/
kmutex_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package kmutex
import (
"sync"
"testing"
"time"
)
// Number of unique resources to access
const number = 100
func makeIds(count int) []int {
ids := make([]int, count)
for i := 0; i < count; i++ {
ids[i] = i
}
return ids
}
func TestKmutex(t *testing.T) {
km := New()
ids := makeIds(number)
resources := make([]int, number)
wg := sync.WaitGroup{}
lc := make(chan int)
uc := make(chan int)
// Start 10n goroutines accessing n resources 10 times each
for i := 0; i < 10*number; i++ {
wg.Add(1)
go func(k int) {
for j := 0; j < 10; j++ {
lc <- k
km.Lock(ids[k])
// read and write resource to check for race
resources[k] = resources[k] + 1
km.Unlock(ids[k])
uc <- k
}
wg.Done()
}(i % len(ids))
}
to := time.After(time.Second)
counts := make(map[int]int)
var lCount, ulCount int
loop:
for {
select {
case k := <-lc:
counts[k] = counts[k] + 1
lCount++
case k := <-uc:
counts[k] = counts[k] - 1
ulCount++
case <-to:
t.Fatal("timed out waiting for results")
break loop
}
expectCount := 100 * number
if lCount == expectCount && ulCount == expectCount {
// Have all results
break
}
}
for k, c := range counts {
if c != 0 {
t.Errorf("Key %d count != 0: %d\n", k, c)
}
}
wg.Wait()
}
func TestWithLock(t *testing.T) {
l := sync.Mutex{}
km := WithLock(&l)
ids := makeIds(number)
resources := make([]int, number)
wg := sync.WaitGroup{}
// Start 10n goroutines accessing n resources 10 times each
for i := 0; i < 10*number; i++ {
wg.Add(1)
go func(k int) {
for j := 0; j < 10; j++ {
km.Lock(ids[k])
// read and write resource to check for race
resources[k] = resources[k] + 1
km.Unlock(ids[k])
}
wg.Done()
}(i % len(ids))
}
wg.Wait()
// Verify correct hit count for each resource
// expecting: (10n hits / n resources) * 10 == 100 hits/resource
for i := range resources {
if resources[i] != 100 {
t.Errorf("resource-%d expected 100 hits, got %d", i, resources[i])
}
}
}
func TestLockerInterface(t *testing.T) {
km := New()
locker := km.Locker("TEST")
cond := sync.NewCond(locker)
if false {
cond.Wait()
}
}
func TestCondDeadlock(t *testing.T) {
l := sync.Mutex{}
km := WithLock(&l)
ids := makeIds(10)
timeout := time.NewTimer(time.Second)
defer timeout.Stop()
for checks := 0; checks < 5; checks++ {
done := make(chan struct{})
go func() {
for i := 0; i < len(ids); i++ {
km.Lock(ids[i])
}
close(done)
}()
select {
case <-done:
case <-timeout.C:
t.Fatal("timeout while locking all locks")
}
var wg, wgReady sync.WaitGroup
unlocked := make(chan int, len(ids))
for i := 0; i < len(ids); i++ {
wg.Add(1)
wgReady.Add(1)
go func(k int) {
wgReady.Done()
km.Lock(ids[k])
unlocked <- k
wg.Done()
}(i)
}
wgReady.Wait()
km.Unlock(ids[0])
select {
case u := <-unlocked:
if u != 0 {
t.Fatal("unlocked wrong key, expected 0 but got", u)
}
case <-timeout.C:
t.Fatal("timed out waiting for ids[0] to unlock")
}
if !timeout.Stop() {
<-timeout.C
}
for i := 1; i < len(ids); i++ {
km.Unlock(ids[i])
u := <-unlocked
if u != ids[i] {
t.Fatal("unlocked wrong key, expected", ids[i], "got", u)
}
}
for i := 0; i < len(ids); i++ {
km.Unlock(ids[i])
}
wg.Wait()
timeout.Reset(time.Second)
}
}
func BenchmarkKmutex1000(b *testing.B) {
km := New()
ids := makeIds(number)
resources := make([]int, number)
wg := sync.WaitGroup{}
// Start 1000 goroutines accessing 100 resources N times each
b.ResetTimer()
for i := 0; i < 1000; i++ {
wg.Add(1)
go func(k int) {
for j := 0; j < b.N; j++ {
km.Lock(ids[k])
// read and write resource to check for race
resources[k] = resources[k] + 1
km.Unlock(ids[k])
}
wg.Done()
}(i % len(ids))
}
wg.Wait()
}