-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_test.go
195 lines (145 loc) · 2.92 KB
/
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package lockunique_test
import (
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/starboard-nz/lockunique"
)
func TestMain(m *testing.M) {
go RunDebugServer(8888)
m.Run()
}
func TestBasic(t *testing.T) {
l := lockunique.NewLockUnique[int32]()
l.Lock(int32(1))
done := make(chan struct{})
go func() {
l.Lock(int32(1))
done <- struct{}{}
}()
time.Sleep(10 * time.Millisecond)
select {
case <-done:
t.Errorf("Same ID locked twice")
default:
}
l.Lock(int32(2))
err := l.Unlock(int32(2))
assert.Nil(t, err)
select {
case <-done:
t.Errorf("Same ID locked twice")
default:
}
err = l.Unlock(int32(2))
assert.NotNil(t, err)
err = l.Unlock(int32(1))
assert.Nil(t, err)
time.Sleep(10 * time.Millisecond)
select {
case <-done:
default:
t.Errorf("Lock should have succeeded")
}
err = l.Unlock(int32(1))
assert.Nil(t, err)
err = l.Unlock(int32(1))
assert.NotNil(t, err)
}
func TestLockUnlock(t *testing.T) {
l := lockunique.NewLockUnique[int32]()
const maxID = 1000
var n [maxID]int32
wg := &sync.WaitGroup{}
for i := 0; i < 100000; i++ {
wg.Add(1)
vID := rand.Int31n(maxID-1) + 1
if i % 1000 == 0 {
// makes it keep switching back to an array as the queue clears
time.Sleep(50 * time.Millisecond)
}
go func(vID int32) {
l.Lock(vID)
n0 := atomic.AddInt32(&(n[vID-1]), 1)
if n0 != 1 {
t.Errorf("n0 = %d", n0)
}
//time.Sleep(2 * time.Millisecond)
n0 = atomic.AddInt32(&(n[vID-1]), -1)
if n0 != 0 {
t.Errorf("n0 = %d", n0)
}
l.Unlock(vID)
wg.Done()
}(vID)
}
wg.Wait()
}
func TestLockUnlock2(t *testing.T) {
l := lockunique.NewLockUnique[int32]()
const maxID = 1000
var n [maxID]int32
wg := &sync.WaitGroup{}
for i := 0; i < 100000; i++ {
wg.Add(1)
vID := rand.Int31n(maxID-1) + 1
go func(vID int32) {
l.Lock(vID)
n0 := atomic.AddInt32(&(n[vID-1]), 1)
if n0 != 1 {
t.Errorf("n0 = %d", n0)
}
time.Sleep(2 * time.Millisecond)
n0 = atomic.AddInt32(&(n[vID-1]), -1)
if n0 != 0 {
t.Errorf("n0 = %d", n0)
}
l.Unlock(vID)
wg.Done()
}(vID)
}
wg.Wait()
}
func TestLockUnlockSameID(t *testing.T) {
l := lockunique.NewLockUnique[int32]()
id := int32(123)
var (
locking, unlocking int32
)
count := 100000
wg := &sync.WaitGroup{}
queue := make(chan struct{}, 10)
i := 0
for ; i < count; i++ {
queue <-struct{}{}
wg.Add(1)
go func() {
l.Lock(id)
atomic.AddInt32(&locking, 1)
l.Unlock(id)
atomic.AddInt32(&unlocking, 1)
<-queue
wg.Done()
}()
}
done := make(chan struct{})
go func() {
wg.Wait()
done <-struct{}{}
}()
select {
case <-done:
case <-time.After(10*time.Second):
t.Logf("i = %d; locking = %d; unlocking = %d", i, locking, unlocking)
t.Errorf("Timed out.")
}
if locking != int32(count) {
t.Errorf("incorrect result")
}
if unlocking != int32(count) {
t.Errorf("incorrect result")
}
}