-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex_test.go
137 lines (119 loc) · 2.41 KB
/
mutex_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
package sync
import (
"context"
"sync"
"testing"
)
func TestLockingMutex(t *testing.T) {
var m Mutex
m.Lock()
}
func TestLockingAndUnlockingMutex(t *testing.T) {
var m Mutex
m.Lock()
m.Unlock()
}
func TestUnlockingUnlockedMutexPanics(t *testing.T) {
var panicked bool
defer func() {
if !panicked {
t.Error("expected function to panic")
}
}()
defer func() {
if r := recover(); r != nil {
panicked = true
}
}()
var m Mutex
m.Unlock()
}
// TestForMutexRaceConditions is a catch-all test that makes sure we don't
// deadlock in some weird edge case.
func TestForMutexRaceConditions(t *testing.T) {
nthreads := 3
niterations := 10000
var done sync.WaitGroup
var m Mutex
var counter int
for i := 0; i < nthreads; i++ {
done.Add(1)
go func() {
for i := 0; i < niterations; i++ {
m.Lock()
counter++
m.Unlock()
}
done.Done()
}()
}
done.Wait()
if expected := nthreads * niterations; counter != expected {
t.Error("wrong value of counter. Was:", counter, "Expected:", expected)
}
}
func TestMutexLockDeadlineError(t *testing.T) {
var m Mutex
m.Lock()
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := m.LockWithContext(ctx)
if expected := context.Canceled; err != expected {
t.Error("Unexpected error. Was:", err, "Expected:", expected)
}
}
func TestMutexLockDeadlineDoesntLock(t *testing.T) {
var m Mutex
ctx, cancel := context.WithCancel(context.Background())
cancel()
_ = m.LockWithContext(ctx)
var panicked bool
defer func() {
if !panicked {
t.Error("expected function to panic")
}
}()
defer func() {
if r := recover(); r != nil {
panicked = true
}
}()
m.Unlock()
}
func TestMutexLockWithContextGenerallyHasNoError(t *testing.T) {
var m Mutex
err := m.LockWithContext(context.Background())
if err != nil {
t.Error("expected no error")
}
}
func TestLockingAndUnlockingMutexWithContext(t *testing.T) {
var m Mutex
m.LockWithContext(context.Background())
m.Unlock()
}
func BenchmarkMutexLockUnlock(b *testing.B) {
var m Mutex
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Lock()
m.Unlock()
}
}
func BenchmarkStandardMutexLockUnlock(b *testing.B) {
var m sync.Mutex
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Lock()
m.Unlock()
}
}
func BenchmarkMutexLockWithContextUnlock(b *testing.B) {
var m Mutex
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.LockWithContext(ctx)
m.Unlock()
}
}