-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
mutexes_test.go
217 lines (209 loc) · 3.92 KB
/
mutexes_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package sessions
import (
"strconv"
"sync"
"testing"
"time"
)
// Test mutex lock with immediate release.
func TestMutexesLockAndRelease(t *testing.T) {
m := newMutexes()
var done bool
go func() {
m.Lock("key")
m.Unlock("key")
done = true
}()
time.Sleep(10 * time.Millisecond)
if !done {
t.Error("Mutex still blocking despite release")
}
}
// Test mutex with two locks on the same key.
func TestMutexesTwoLocks(t *testing.T) {
m := newMutexes()
var (
result string
wg sync.WaitGroup
)
go0 := make(chan bool)
go1 := make(chan bool)
go2 := make(chan bool)
for i := 0; i < 5; i++ {
wg.Add(2)
go func() {
defer wg.Done()
m.Lock("key")
defer m.Unlock("key")
go0 <- true
<-go1
result += "1"
}()
go func() {
defer wg.Done()
<-go2
m.Lock("key")
defer m.Unlock("key")
result += "2"
}()
<-go0
go2 <- true
go1 <- true
wg.Wait()
}
if result != "1212121212" {
t.Errorf("Locking not as expected: %s", result)
}
}
// Test mutex with two locks on different keys.
func TestMutexesDifferentKeys(t *testing.T) {
m := newMutexes()
var (
result string
wg sync.WaitGroup
)
go0 := make(chan bool)
go1 := make(chan bool)
wg.Add(2)
go func() {
defer wg.Done()
m.Lock("key1")
defer m.Unlock("key1")
result += "1"
go0 <- true
}()
go func() {
defer wg.Done()
<-go1
m.Lock("key2")
defer m.Unlock("key2")
result += "2"
}()
<-go0
go1 <- true
wg.Wait()
if result != "12" {
t.Errorf("Locking not as expected: %s", result)
}
}
// Test mutex with many locks on the same key.
func TestMutexesMultipleLocks(t *testing.T) {
n := 10
m := newMutexes()
var (
result string
wg sync.WaitGroup
)
go0 := make(chan bool)
go1 := make(chan bool)
go2 := make(chan bool)
for i := 0; i < n; i++ {
wg.Add(1)
go func(nr int) {
defer wg.Done()
<-go1 // Will fire when go1 is closed.
m.Lock("key")
defer m.Unlock("key")
result += strconv.Itoa(nr)
}(i)
}
wg.Add(1)
go func() {
defer wg.Done()
m.Lock("key")
defer m.Unlock("key")
go0 <- true
<-go2
result += "F"
}()
<-go0
close(go1)
go2 <- true
wg.Wait()
t.Log(result)
if len(result) != n+1 {
t.Error("Some goroutines still haven't finished")
}
if result[0:1] != "F" {
t.Error("Locks were processed in the wrong order")
}
if m.getItem("key").locks != 0 {
t.Error("Locks are still held")
}
}
// Test mutexes by holding a long lock.
func TestMutexesLongLock(t *testing.T) {
m := newMutexes()
var (
mutex sync.Mutex
result string
wg sync.WaitGroup
sg sync.WaitGroup
)
go1 := make(chan bool)
for i := 0; i < 10; i++ {
wg.Add(1)
sg.Add(1)
go func(nr int) {
sg.Done()
defer wg.Done()
<-go1
m.Lock("key")
mutex.Lock()
result += strconv.Itoa(nr)
mutex.Unlock()
m.Unlock("key")
}(i)
}
sg.Wait()
m.Lock("key")
close(go1)
time.Sleep(time.Millisecond)
if len(result) > 0 {
t.Errorf("Some goroutines already wrote despite locks: %s", result)
}
m.Unlock("key")
wg.Wait()
if len(result) != 10 {
t.Errorf("Some goroutines did not write despite released lock: %s", result)
}
}
// Test mutex purging of stale items.
func TestMutexesStalePurge(t *testing.T) {
m := newMutexes()
mutexStaleMutexes = time.Millisecond
m.Lock("key1")
m.Unlock("key1")
m.Lock("key2")
m.Unlock("key2")
m.Lock("key3")
m.Unlock("key3")
if len(m.items) != 3 {
t.Error("Keys not found in items")
}
time.Sleep(3 * time.Millisecond)
m.purge <- struct{}{}
time.Sleep(3 * time.Millisecond)
if len(m.items) != 0 {
t.Error("Mutex map was not purged")
}
}
// Test mutex purging due to max size reached.
func TestMutexesSizePurge(t *testing.T) {
m := newMutexes()
m.Lock("key1")
m.Unlock("key1")
m.Lock("key2")
m.Unlock("key2")
m.Lock("key3")
m.Unlock("key3")
if len(m.items) != 3 {
t.Error("Keys not found in items")
}
mutexMaxCacheSize = 1
m.purge <- struct{}{}
time.Sleep(3 * time.Millisecond)
if len(m.items) != 1 {
t.Error("Mutex map was not purged")
}
}