-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimistic_lock_test.go
157 lines (137 loc) · 2.76 KB
/
optimistic_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
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
//go:build !race
// https://github.com/dshulyak/art
package art
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestOlock(t *testing.T) {
t.Run("concurrent readers", func(t *testing.T) {
var lock olock
version, restart := lock.RLock()
require.Empty(t, version)
require.False(t, restart)
version, restart = lock.RLock()
require.Empty(t, version)
require.False(t, restart)
})
t.Run("reader invalidated", func(t *testing.T) {
var lock olock
version, restart := lock.RLock()
require.Empty(t, version)
require.False(t, restart)
lock.Lock()
restart = lock.RUnlock(version, nil)
require.True(t, restart)
})
t.Run("writer blocks reader", func(t *testing.T) {
var lock olock
lock.Lock()
versionc := make(chan uint64, 1)
go func() {
version, restart := lock.RLock()
require.False(t, restart)
versionc <- version
}()
select {
case <-time.After(100 * time.Millisecond):
case <-versionc:
require.FailNow(t, "reader must be blocked")
}
lock.Unlock()
select {
case <-time.After(100 * time.Millisecond):
require.FailNow(t, "reader must succeed when writer unlocks")
case version := <-versionc:
// 4 - +2 locked, +2 unlocked
require.Equal(t, uint64(4), version)
}
})
t.Run("read obsolete", func(t *testing.T) {
var lock olock
lock.Lock()
lock.UnlockObsolete()
version, restart := lock.RLock()
require.True(t, restart)
// +2 - lock , +3 - unlock obsolete
require.Equal(t, uint64(5), version)
})
t.Run("read check", func(t *testing.T) {
var lock olock
version, restart := lock.RLock()
require.False(t, restart)
require.False(t, lock.Check(version))
lock.Lock()
require.True(t, lock.Check(version))
})
t.Run("upgrade", func(t *testing.T) {
var lock olock
version, restart := lock.RLock()
require.False(t, restart)
require.False(t, lock.Upgrade(version, nil))
})
}
type integer struct {
lock olock
value int
}
func (i *integer) inc() {
for {
version, _ := i.lock.RLock()
if i.lock.Upgrade(version, nil) {
continue
}
i.value++
i.lock.Unlock()
return
}
}
func (i *integer) cas(from, to int) {
for {
version, _ := i.lock.RLock()
if i.value != from {
if i.lock.RUnlock(version, nil) {
continue
}
return
}
if i.lock.Upgrade(version, nil) {
continue
}
i.value += to
i.lock.Unlock()
return
}
}
func TestOlockIncrement(t *testing.T) {
var (
it integer
wg sync.WaitGroup
)
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
it.inc()
wg.Done()
}()
}
wg.Wait()
require.Equal(t, 100, it.value)
}
func TestOlockCas(t *testing.T) {
var (
it integer
wg sync.WaitGroup
)
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
it.cas(0, 100)
wg.Done()
}()
}
wg.Wait()
require.Equal(t, 100, it.value)
}