-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmutex_test.go
More file actions
153 lines (139 loc) · 2.5 KB
/
Copy pathmutex_test.go
File metadata and controls
153 lines (139 loc) · 2.5 KB
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
package detectlock
import (
"sync"
"testing"
)
func BenchmarkMutex_Lock(b *testing.B) {
counter := 0
lockers := []*Mutex{
{}, {}, {}, {}, {}, {}, {}, {}, {}, {},
}
b.Run("EnableDebug", func(b *testing.B) {
EnableDebug()
b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
for _, locker := range lockers {
locker.Lock()
defer locker.Unlock()
}
counter++
}()
}
})
b.Run("DisableDebug", func(b *testing.B) {
DisableDebug()
b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
for _, locker := range lockers {
locker.Lock()
defer locker.Unlock()
}
counter++
}()
}
})
b.Run("sync.Mutex", func(b *testing.B) {
lockers := []*sync.Mutex{
{}, {}, {}, {}, {}, {}, {}, {}, {}, {},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
for _, locker := range lockers {
locker.Lock()
defer locker.Unlock()
}
counter++
}()
}
})
}
func BenchmarkMutex_TryLock(b *testing.B) {
counter := 0
lockers := []*Mutex{
{}, {}, {}, {}, {}, {}, {}, {}, {}, {},
}
b.Run("EnableDebug", func(b *testing.B) {
EnableDebug()
b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
for _, locker := range lockers {
locker.TryLock()
defer locker.Unlock()
}
counter++
}()
}
})
b.Run("DisableDebug", func(b *testing.B) {
DisableDebug()
b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
for _, locker := range lockers {
locker.TryLock()
defer locker.Unlock()
}
counter++
}()
}
})
b.Run("sync.Mutex", func(b *testing.B) {
lockers := []*sync.Mutex{
{}, {}, {}, {}, {}, {}, {}, {}, {}, {},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
for _, locker := range lockers {
locker.TryLock()
defer locker.Unlock()
}
counter++
}()
}
})
}
func TestMutex_Lock(t *testing.T) {
EnableDebug()
t.Run("lock once success", func(t *testing.T) {
l := &Mutex{}
l.Lock()
})
t.Run("lock twice fail", func(t *testing.T) {
l := &Mutex{}
l.Lock()
if l.TryLock() {
t.Fail()
}
})
}
func TestMutex_TryLock(t *testing.T) {
EnableDebug()
t.Run("try-lock once success", func(t *testing.T) {
l := &Mutex{}
if !l.TryLock() {
t.Fail()
}
})
t.Run("try-lock twice fail", func(t *testing.T) {
l := &Mutex{}
l.TryLock()
if l.TryLock() {
t.Fail()
}
})
}
func TestMutex_Unlock(t *testing.T) {
EnableDebug()
t.Run("unlock once success", func(t *testing.T) {
l := &Mutex{}
counter := 0
l.Lock()
counter++
l.Unlock()
})
}