-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_test.go
59 lines (55 loc) · 1.09 KB
/
test_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
package wfmutex
import (
"testing"
"time"
)
func TestWFMutex(t *testing.T) {
rw := new(WFMutex)
var tid uint64 = 42
ok, _ := rw.Lock()
if !ok {
t.Errorf("Could not lock\n")
}
if rw.Read()&LOCKED == 0 {
t.Errorf("Should be locked\n")
}
ok = rw.Unlock(tid)
if rw.Read()&LOCKED != 0 {
t.Errorf("Should be unlocked\n")
}
ngo := 10
locked := make([]int, ngo)
unlocked := make([]int, ngo)
for i := 0; i < ngo; i++ {
done := time.NewTimer(time.Duration(100 * time.Millisecond)).C
go func(id int, done <-chan time.Time) {
tid := uint64(id)
for {
select {
case <-done:
return
default:
ok, _ := rw.Lock()
if ok {
locked[id]++
x := rw.Read()
if x&LOCKED == 0 {
t.Errorf("I locked it! %x %x %x\n", id, x, rw.w)
}
rw.Unlock(tid)
unlocked[id]++
}
}
}
}(i, done)
}
total_locked := 0
total_unlocked := 0
for i := 0; i < ngo; i++ {
total_locked += locked[i]
total_unlocked += unlocked[i]
}
if total_locked != total_unlocked {
t.Errorf("Mismatched %v %v %x\n", total_locked, total_unlocked, rw.w)
}
}