Debian Codegraph @

drwmutex_test.go

raw symbols SCIP
1 /*
2 * Minio Cloud Storage, (C) 2016 Minio, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // GOMAXPROCS=10 go test
18
19 package dsync_test
20
21 import (
22 "fmt"
23 "runtime"
24 "sync"
25 "sync/atomic"
26 "testing"
27 "time"
28
29 . "github.com/minio/dsync"
30 )
31
32 func TestSimpleWriteLock(t *testing.T) {
33
34 drwm := NewDRWMutex("resource")
35
36 drwm.RLock()
37 // fmt.Println("1st read lock acquired, waiting...")
38
39 drwm.RLock()
40 // fmt.Println("2nd read lock acquired, waiting...")
41
42 go func() {
43 time.Sleep(1000 * time.Millisecond)
44 drwm.RUnlock()
45 // fmt.Println("1st read lock released, waiting...")
46 }()
47
48 go func() {
49 time.Sleep(2000 * time.Millisecond)
50 drwm.RUnlock()
51 // fmt.Println("2nd read lock released, waiting...")
52 }()
53
54 // fmt.Println("Trying to acquire write lock, waiting...")
55 drwm.Lock()
56
57 // fmt.Println("Write lock acquired, waiting...")
58 time.Sleep(2500 * time.Millisecond)
59
60 drwm.Unlock()
61 }
62
63 // Test cases below are copied 1 to 1 from sync/rwmutex_test.go (adapted to use DRWMutex)
64
65 // Borrowed from rwmutex_test.go
66 func parallelReader(m *DRWMutex, clocked, cunlock, cdone chan bool) {
67 m.RLock()
68 clocked <- true
69 <-cunlock
70 m.RUnlock()
71 cdone <- true
72 }
73
74 // Borrowed from rwmutex_test.go
75 func doTestParallelReaders(numReaders, gomaxprocs int) {
76 runtime.GOMAXPROCS(gomaxprocs)
77 m := NewDRWMutex("test-parallel")
78
79 clocked := make(chan bool)
80 cunlock := make(chan bool)
81 cdone := make(chan bool)
82 for i := 0; i < numReaders; i++ {
83 go parallelReader(m, clocked, cunlock, cdone)
84 }
85 // Wait for all parallel RLock()s to succeed.
86 for i := 0; i < numReaders; i++ {
87 <-clocked
88 }
89 for i := 0; i < numReaders; i++ {
90 cunlock <- true
91 }
92 // Wait for the goroutines to finish.
93 for i := 0; i < numReaders; i++ {
94 <-cdone
95 }
96 }
97
98 // Borrowed from rwmutex_test.go
99 func TestParallelReaders(t *testing.T) {
100 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1))
101 doTestParallelReaders(1, 4)
102 doTestParallelReaders(3, 4)
103 doTestParallelReaders(4, 2)
104 }
105
106 // Borrowed from rwmutex_test.go
107 func reader(rwm *DRWMutex, num_iterations int, activity *int32, cdone chan bool) {
108 for i := 0; i < num_iterations; i++ {
109 rwm.RLock()
110 n := atomic.AddInt32(activity, 1)
111 if n < 1 || n >= 10000 {
112 panic(fmt.Sprintf("wlock(%d)\n", n))
113 }
114 for i := 0; i < 100; i++ {
115 }
116 atomic.AddInt32(activity, -1)
117 rwm.RUnlock()
118 }
119 cdone <- true
120 }
121
122 // Borrowed from rwmutex_test.go
123 func writer(rwm *DRWMutex, num_iterations int, activity *int32, cdone chan bool) {
124 for i := 0; i < num_iterations; i++ {
125 rwm.Lock()
126 n := atomic.AddInt32(activity, 10000)
127 if n != 10000 {
128 panic(fmt.Sprintf("wlock(%d)\n", n))
129 }
130 for i := 0; i < 100; i++ {
131 }
132 atomic.AddInt32(activity, -10000)
133 rwm.Unlock()
134 }
135 cdone <- true
136 }
137
138 // Borrowed from rwmutex_test.go
139 func HammerRWMutex(gomaxprocs, numReaders, num_iterations int) {
140 runtime.GOMAXPROCS(gomaxprocs)
141 // Number of active readers + 10000 * number of active writers.
142 var activity int32
143 rwm := NewDRWMutex("test")
144 cdone := make(chan bool)
145 go writer(rwm, num_iterations, &activity, cdone)
146 var i int
147 for i = 0; i < numReaders/2; i++ {
148 go reader(rwm, num_iterations, &activity, cdone)
149 }
150 go writer(rwm, num_iterations, &activity, cdone)
151 for ; i < numReaders; i++ {
152 go reader(rwm, num_iterations, &activity, cdone)
153 }
154 // Wait for the 2 writers and all readers to finish.
155 for i := 0; i < 2+numReaders; i++ {
156 <-cdone
157 }
158 }
159
160 // Borrowed from rwmutex_test.go
161 func TestRWMutex(t *testing.T) {
162 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1))
163 n := 1000
164 if testing.Short() {
165 n = 5
166 }
167 HammerRWMutex(1, 1, n)
168 HammerRWMutex(1, 3, n)
169 HammerRWMutex(1, 10, n)
170 HammerRWMutex(4, 1, n)
171 HammerRWMutex(4, 3, n)
172 HammerRWMutex(4, 10, n)
173 HammerRWMutex(10, 1, n)
174 HammerRWMutex(10, 3, n)
175 HammerRWMutex(10, 10, n)
176 HammerRWMutex(10, 5, n)
177 }
178
179 // Borrowed from rwmutex_test.go
180 func TestDRLocker(t *testing.T) {
181 wl := NewDRWMutex("test")
182 var rl sync.Locker
183 wlocked := make(chan bool, 1)
184 rlocked := make(chan bool, 1)
185 rl = wl.DRLocker()
186 n := 10
187 go func() {
188 for i := 0; i < n; i++ {
189 rl.Lock()
190 rl.Lock()
191 rlocked <- true
192 wl.Lock()
193 wlocked <- true
194 }
195 }()
196 for i := 0; i < n; i++ {
197 <-rlocked
198 rl.Unlock()
199 select {
200 case <-wlocked:
201 t.Fatal("RLocker() didn't read-lock it")
202 default:
203 }
204 rl.Unlock()
205 <-wlocked
206 select {
207 case <-rlocked:
208 t.Fatal("RLocker() didn't respect the write lock")
209 default:
210 }
211 wl.Unlock()
212 }
213 }
214
215 // Borrowed from rwmutex_test.go
216 func TestUnlockPanic(t *testing.T) {
217 defer func() {
218 if recover() == nil {
219 t.Fatalf("unlock of unlocked RWMutex did not panic")
220 }
221 }()
222 mu := NewDRWMutex("test")
223 mu.Unlock()
224 }
225
226 // Borrowed from rwmutex_test.go
227 func TestUnlockPanic2(t *testing.T) {
228 defer func() {
229 if recover() == nil {
230 t.Fatalf("unlock of unlocked RWMutex did not panic")
231 }
232 }()
233 mu := NewDRWMutex("test-unlock-panic-2")
234 mu.RLock()
235 mu.Unlock()
236 }
237
238 // Borrowed from rwmutex_test.go
239 func TestRUnlockPanic(t *testing.T) {
240 defer func() {
241 if recover() == nil {
242 t.Fatalf("read unlock of unlocked RWMutex did not panic")
243 }
244 }()
245 mu := NewDRWMutex("test")
246 mu.RUnlock()
247 }
248
249 // Borrowed from rwmutex_test.go
250 func TestRUnlockPanic2(t *testing.T) {
251 defer func() {
252 if recover() == nil {
253 t.Fatalf("read unlock of unlocked RWMutex did not panic")
254 }
255 }()
256 mu := NewDRWMutex("test-runlock-panic-2")
257 mu.Lock()
258 mu.RUnlock()
259 }
260
261 // Borrowed from rwmutex_test.go
262 func benchmarkRWMutex(b *testing.B, localWork, writeRatio int) {
263 rwm := NewDRWMutex("test")
264 b.RunParallel(func(pb *testing.PB) {
265 foo := 0
266 for pb.Next() {
267 foo++
268 if foo%writeRatio == 0 {
269 rwm.Lock()
270 rwm.Unlock()
271 } else {
272 rwm.RLock()
273 for i := 0; i != localWork; i += 1 {
274 foo *= 2
275 foo /= 2
276 }
277 rwm.RUnlock()
278 }
279 }
280 _ = foo
281 })
282 }
283
284 // Borrowed from rwmutex_test.go
285 func BenchmarkRWMutexWrite100(b *testing.B) {
286 benchmarkRWMutex(b, 0, 100)
287 }
288
289 // Borrowed from rwmutex_test.go
290 func BenchmarkRWMutexWrite10(b *testing.B) {
291 benchmarkRWMutex(b, 0, 10)
292 }
293
294 // Borrowed from rwmutex_test.go
295 func BenchmarkRWMutexWorkWrite100(b *testing.B) {
296 benchmarkRWMutex(b, 100, 100)
297 }
298
299 // Borrowed from rwmutex_test.go
300 func BenchmarkRWMutexWorkWrite10(b *testing.B) {
301 benchmarkRWMutex(b, 100, 10)
302 }