forked from JasonYuan/cmutex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwmutex_test.go
159 lines (147 loc) · 2.96 KB
/
rwmutex_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cmutex
import (
"context"
"github.com/stretchr/testify/assert"
"math/rand"
"sync"
"testing"
"time"
)
var cRWMutex = NewRWMutex()
var rWMutex = sync.RWMutex{}
func TestBasicRWMutex1(t *testing.T) {
m := NewRWMutex()
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()
assert.Nil(t, m.RLock(ctx))
assert.Nil(t, m.RLock(ctx))
assert.True(t, m.Lock(ctx) != nil)
ctx, cancel = context.WithTimeout(context.TODO(), time.Second)
m.RUnlock()
m.RUnlock()
assert.Nil(t, m.Lock(ctx))
assert.True(t, m.RLock(ctx) != nil)
assert.True(t, m.Lock(ctx) != nil)
m.Unlock()
}
func TestRwMutex_TryLock(t *testing.T) {
m := NewRWMutex()
assert.Nil(t, m.RLock(context.TODO()))
assert.True(t, m.TryRLock())
assert.False(t, m.TryLock())
m.RUnlock()
m.RUnlock()
assert.True(t, m.TryLock())
assert.False(t, m.TryRLock())
}
var (
readCount = 100
writeCount = 100
sleepTime = 100 * time.Millisecond
totalWriteTime = time.Duration(writeCount) * sleepTime
)
func TestRWMutex1(t *testing.T) {
start := time.Now()
m := NewRWMutex()
value := 0
wg := sync.WaitGroup{}
for i := 0; i < writeCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
assert.Nil(t, m.Lock(context.TODO()))
defer func() {
m.Unlock()
}()
assert.Equal(t, value, 0)
value = 1
time.Sleep(sleepTime)
value = 0
}()
}
for i := 0; i < readCount; i++ {
wg.Add(1)
go func() {
time.Sleep(time.Duration(rand.Int63n(int64(sleepTime))))
defer wg.Done()
assert.Nil(t, m.RLock(context.TODO()))
//noinspection GoUnhandledErrorResult
defer func() {
m.RUnlock()
}()
assert.Equal(t, value, 0)
time.Sleep(sleepTime)
assert.Equal(t, value, 0)
}()
}
wg.Wait()
assert.True(t, time.Now().Sub(start) >= totalWriteTime)
}
func BenchmarkCRLock(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = cRWMutex.RLock(context.Background())
cRWMutex.RUnlock()
}
}
func BenchmarkRLock(b *testing.B) {
for i := 0; i < b.N; i++ {
rWMutex.RLock()
rWMutex.RUnlock()
}
}
func BenchmarkCLock(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = cRWMutex.Lock(context.Background())
cRWMutex.Unlock()
}
}
func BenchmarkLock(b *testing.B) {
for i := 0; i < b.N; i++ {
rWMutex.Lock()
rWMutex.Unlock()
}
}
func BenchmarkCRace(b *testing.B) {
for i := 0; i < b.N; i++ {
wg := sync.WaitGroup{}
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
_ = cRWMutex.RLock(context.Background())
cRWMutex.RUnlock()
wg.Done()
}()
}
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
_ = cRWMutex.Lock(context.Background())
cRWMutex.Unlock()
wg.Done()
}()
}
wg.Wait()
}
}
func BenchmarkRace(b *testing.B) {
for i := 0; i < b.N; i++ {
wg := sync.WaitGroup{}
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
rWMutex.RLock()
rWMutex.RUnlock()
wg.Done()
}()
}
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
rWMutex.Lock()
rWMutex.Unlock()
wg.Done()
}()
}
wg.Wait()
}
}