-
Notifications
You must be signed in to change notification settings - Fork 0
/
locker_test.go
160 lines (136 loc) · 2.62 KB
/
locker_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
package fcache
import (
"math/rand"
"sync"
"testing"
"time"
)
func TestLockerLock(t *testing.T) {
l := NewLocker()
l.Lock(1)
holder := l.locks[1]
if holder.users != 1 {
t.Fatalf("expected users to be 1, got :%d", holder.users)
}
chDone := make(chan struct{})
go func() {
l.Lock(1)
close(chDone)
}()
chWaiting := make(chan struct{})
go func() {
for range time.Tick(1 * time.Millisecond) {
if holder.users == 2 {
close(chWaiting)
break
}
}
}()
select {
case <-chWaiting:
case <-time.After(1 * time.Second):
t.Fatal("timed out waiting for lock users to be incremented")
}
select {
case <-chDone:
t.Fatal("lock should not have returned while it was still held")
default:
}
l.Unlock(1)
select {
case <-chDone:
case <-time.After(3 * time.Second):
t.Fatalf("lock should have completed")
}
if holder.users != 1 {
t.Fatalf("expected users to be 1, got: %d", holder.users)
}
}
func TestLockerUnlock(t *testing.T) {
l := NewLocker()
l.Lock(1)
l.Unlock(1)
chDone := make(chan struct{})
go func() {
l.Lock(1)
close(chDone)
}()
select {
case <-chDone:
case <-time.After(1 * time.Second):
t.Fatalf("lock should not be blocked")
}
}
func TestLockerConcurrency(t *testing.T) {
l := NewLocker()
expectedWrites := 0
writes := 0
var wg sync.WaitGroup
for i := 0; i <= 1000; i++ {
wg.Add(1)
r := rand.Intn(2)
if r == 0 {
expectedWrites += 1
}
go func(r int) {
defer wg.Done()
if r == 0 {
l.Lock(1)
writes += 1
defer l.Unlock(1)
} else {
l.RLock(1)
defer l.RUnlock(1)
}
s := time.Now()
for time.Now().Sub(s) < 2*time.Millisecond {
// busy waiting, with time.Sleep it takes way longer for some reason
}
}(r)
}
chDone := make(chan struct{})
go func() {
wg.Wait()
close(chDone)
}()
select {
case <-chDone:
case <-time.After(3 * time.Second):
t.Fatal("timeout waiting for locks to complete")
}
if holder, exists := l.locks[1]; exists {
t.Fatalf("lock should not exist: %v", holder)
}
if expectedWrites != writes {
t.Fatalf("expected %d writes but got %d", expectedWrites, writes)
}
}
func BenchmarkLocker(b *testing.B) {
l := NewLocker()
for i := 0; i < b.N; i++ {
l.Lock(1)
l.Unlock(1)
}
}
func BenchmarkLockerParallel(b *testing.B) {
l := NewLocker()
b.SetParallelism(128)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
l.Lock(1)
l.Unlock(1)
}
})
}
func BenchmarkLockerMoreKeys(b *testing.B) {
l := NewLocker()
b.SetParallelism(128)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
k := uint64(rand.Intn(64))
l.Lock(k)
time.Sleep(5 * time.Millisecond)
l.Unlock(k)
}
})
}