-
Notifications
You must be signed in to change notification settings - Fork 2
/
locker_test.go
179 lines (146 loc) · 3.94 KB
/
locker_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
// nolint:errcheck
package geche
import (
"math/rand"
"sync"
"testing"
)
func TestLockerParallel(t *testing.T) {
// Use locker to simulate atomic balance transfer between accounts.
// Single transfer consists of getting balance of two accounts,
// then subtracting some amount from one and adding it to another.
// Operation is run concurrently on multiple goroutines,
// If tx isolation is not implemented correctly total balance can change.
locker := NewLocker[int, int](NewMapCache[int, int]())
numAccounts := 10
numTransactions := 100000
initialBalance := 1000
tx := locker.Lock()
for i := 0; i < numAccounts; i++ {
tx.Set(i, initialBalance)
}
tx.Unlock()
totalBalance := numAccounts * initialBalance
wg := &sync.WaitGroup{}
for i := 0; i < numTransactions; i++ {
wg.Add(1)
go func() {
accA := rand.Intn(numAccounts)
var accB int
for accB = rand.Intn(numAccounts); accB == accA; accB = rand.Intn(numAccounts) {
}
tx := locker.Lock()
balA, _ := tx.Get(accA)
balB, _ := tx.Get(accB)
if balA < balB {
size := rand.Intn(balB)
balA += size
balB -= size
tx.Set(accA, balA)
tx.Set(accB, balB)
} else {
size := rand.Intn(balA)
balB += size
balA -= size
tx.Set(accA, balA)
tx.Set(accB, balB)
}
tx.Unlock()
wg.Done()
}()
}
wg.Wait()
tx = locker.RLock()
sum := 0
for i := 0; i < numAccounts; i++ {
bal, _ := tx.Get(i)
sum += bal
}
tx.Unlock()
if sum != totalBalance {
t.Errorf("expected %d, got %d", totalBalance, sum)
}
}
func panics(f func()) (panicked bool) {
defer func() {
if r := recover(); r != nil {
panicked = true
}
}()
f()
return
}
func TestLockerRPanics(t *testing.T) {
locker := NewLocker[int, int](NewMapCache[int, int]())
tx := locker.RLock()
if !panics(func() { tx.Set(1, 1) }) {
t.Errorf("expected panic (Set on RLocked)")
}
if !panics(func() { tx.Del(1) }) {
t.Errorf("expected panic (Delete on RLocked)")
}
tx.Unlock()
if !panics(func() { tx.Unlock() }) {
t.Errorf("expected panic (Unlock on already unlocked)")
}
if !panics(func() { tx.Set(1, 1) }) {
t.Errorf("expected panic (Set on already unlocked)")
}
if !panics(func() { tx.Del(1) }) {
t.Errorf("expected panic (Delete on already unlocked)")
}
if !panics(func() { tx.Get(1) }) {
t.Errorf("expected panic (Get on already unlocked)")
}
if !panics(func() { tx.Len() }) {
t.Errorf("expected panic (Len on already unlocked)")
}
if !panics(func() { tx.Snapshot() }) {
t.Errorf("expected panic (Snapshot on already unlocked)")
}
}
func TestLockerRWPanics(t *testing.T) {
locker := NewLocker[int, int](NewMapCache[int, int]())
tx := locker.Lock()
if !panics(func() { _, _ = tx.ListByPrefix("test") }) {
t.Errorf("expected panic (ListByPrefix on MapCache)")
}
tx.Unlock()
if !panics(func() { tx.Unlock() }) {
t.Errorf("expected panic (Unlock on already unlocked)")
}
if !panics(func() { tx.Set(1, 1) }) {
t.Errorf("expected panic (Set on already unlocked)")
}
if !panics(func() { tx.Del(1) }) {
t.Errorf("expected panic (Delete on already unlocked)")
}
if !panics(func() { tx.Get(1) }) {
t.Errorf("expected panic (Get on already unlocked)")
}
if !panics(func() { tx.Len() }) {
t.Errorf("expected panic (Len on already unlocked)")
}
if !panics(func() { tx.Snapshot() }) {
t.Errorf("expected panic (Snapshot on already unlocked)")
}
if !panics(func() { _, _ = tx.ListByPrefix("test") }) {
t.Errorf("expected panic (ListByPrefix on already unlocked)")
}
}
func TestLockerListByPrefix(t *testing.T) {
imp := NewLocker[string, string](NewKV[string](NewMapCache[string, string]()))
tx := imp.Lock()
defer tx.Unlock()
tx.Set("test9", "test9")
tx.Set("test2", "test2")
tx.Set("test1", "test1")
tx.Set("test3", "test3")
_ = tx.Del("test2")
expected := []string{"test1", "test3", "test9"}
actual, err := tx.ListByPrefix("test")
if err != nil {
t.Errorf("unexpected error in ListByPrefix: %v", err)
}
compareSlice(t, expected, actual)
}