-
Notifications
You must be signed in to change notification settings - Fork 0
/
rw_lock_test.go
53 lines (34 loc) · 1015 Bytes
/
rw_lock_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
// Tests for RW-Lock
package main
import (
"sync"
"testing"
"time"
)
func threadReader(wg *sync.WaitGroup, lock *ReadWriteLock, msWaitBefore int, msWaitRead int) {
time.Sleep(time.Duration(msWaitBefore) * time.Millisecond)
lock.StartRead()
time.Sleep(time.Duration(msWaitRead) * time.Millisecond)
lock.EndRead()
wg.Done()
}
func threadWriter(wg *sync.WaitGroup, lock *ReadWriteLock, msWaitBefore int, msWaitWrite int, msWaitSave int) {
time.Sleep(time.Duration(msWaitBefore) * time.Millisecond)
lock.RequestWrite()
time.Sleep(time.Duration(msWaitWrite) * time.Millisecond)
lock.StartWrite()
time.Sleep(time.Duration(msWaitSave) * time.Millisecond)
lock.EndWrite()
wg.Done()
}
func TestReadWriteLock(t *testing.T) {
var wg sync.WaitGroup
lock := CreateReadWriteLock()
wg.Add(5)
go threadReader(&wg, lock, 10, 50)
go threadReader(&wg, lock, 10, 50)
go threadReader(&wg, lock, 10, 50)
go threadWriter(&wg, lock, 20, 20, 50)
go threadWriter(&wg, lock, 20, 20, 50)
wg.Wait()
}