-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyrwmutex_test.go
162 lines (139 loc) · 3.82 KB
/
keyrwmutex_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
package keyrwmutex
import (
"testing"
"time"
)
const (
callbackTimeout = 1 * time.Second
)
func newKeyMutexes() []*KeyRWMutex {
return []*KeyRWMutex{
New(0),
New(1),
New(2),
New(4),
}
}
// Copy from https://github.com/kubernetes/utils/blob/master/keymutex/keymutex_test.go
func Test_SingleLock_NoUnlock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh := make(chan interface{})
// Act
go lockAndCallback(km, key, callbackCh)
// Assert
verifyCallbackHappens(t, callbackCh)
}
}
// Copy from https://github.com/kubernetes/utils/blob/master/keymutex/keymutex_test.go
func Test_SingleLock_SingleUnlock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh := make(chan interface{})
// Act & Assert
go lockAndCallback(km, key, callbackCh)
verifyCallbackHappens(t, callbackCh)
km.UnlockKey(key)
}
}
// Copy from https://github.com/kubernetes/utils/blob/master/keymutex/keymutex_test.go
func Test_DoubleLock_DoubleUnlock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh1stLock := make(chan interface{})
callbackCh2ndLock := make(chan interface{})
// Act & Assert
go lockAndCallback(km, key, callbackCh1stLock)
verifyCallbackHappens(t, callbackCh1stLock)
go lockAndCallback(km, key, callbackCh2ndLock)
verifyCallbackDoesntHappens(t, callbackCh2ndLock)
km.UnlockKey(key)
verifyCallbackHappens(t, callbackCh2ndLock)
km.UnlockKey(key)
}
}
func Test_SingleRLock_NoRUnlock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh := make(chan interface{})
// Act
go rLockAndCallback(km, key, callbackCh)
// Assert
verifyCallbackHappens(t, callbackCh)
}
}
func Test_DoubleRLock_DoubleRUnlock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh1stLock := make(chan interface{})
callbackCh2ndLock := make(chan interface{})
// Act & Assert
go rLockAndCallback(km, key, callbackCh1stLock)
verifyCallbackHappens(t, callbackCh1stLock)
go rLockAndCallback(km, key, callbackCh2ndLock)
verifyCallbackHappens(t, callbackCh2ndLock)
}
}
func Test_RLock_Lock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh1stLock := make(chan interface{})
callbackCh2ndLock := make(chan interface{})
// Act & Assert
go rLockAndCallback(km, key, callbackCh1stLock)
verifyCallbackHappens(t, callbackCh1stLock)
go lockAndCallback(km, key, callbackCh2ndLock)
verifyCallbackDoesntHappens(t, callbackCh2ndLock)
km.RUnlockKey(key)
verifyCallbackHappens(t, callbackCh2ndLock)
}
}
func Test_Lock_RLock(t *testing.T) {
for _, km := range newKeyMutexes() {
// Arrange
key := "fakeid"
callbackCh1stLock := make(chan interface{})
callbackCh2ndLock := make(chan interface{})
// Act & Assert
go lockAndCallback(km, key, callbackCh1stLock)
verifyCallbackHappens(t, callbackCh1stLock)
go rLockAndCallback(km, key, callbackCh2ndLock)
verifyCallbackDoesntHappens(t, callbackCh2ndLock)
km.UnlockKey(key)
verifyCallbackHappens(t, callbackCh2ndLock)
}
}
func lockAndCallback(km *KeyRWMutex, id string, callbackCh chan<- interface{}) {
km.LockKey(id)
callbackCh <- true
}
func rLockAndCallback(km *KeyRWMutex, id string, callbackCh chan<- interface{}) {
km.RLockKey(id)
callbackCh <- true
}
func verifyCallbackHappens(t *testing.T, callbackCh <-chan interface{}) bool {
t.Helper()
select {
case <-callbackCh:
return true
case <-time.After(callbackTimeout):
t.Fatalf("Timed out waiting for callback.")
return false
}
}
func verifyCallbackDoesntHappens(t *testing.T, callbackCh <-chan interface{}) bool {
t.Helper()
select {
case <-callbackCh:
t.Fatalf("Unexpected callback.")
return false
case <-time.After(callbackTimeout):
return true
}
}