-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_client_test.go
More file actions
271 lines (228 loc) · 6.63 KB
/
simple_client_test.go
File metadata and controls
271 lines (228 loc) · 6.63 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package lockclient
import (
"os"
"testing"
"time"
"github.com/SystemBuilders/LocKey/internal/lockclient/cache"
"github.com/SystemBuilders/LocKey/internal/lockservice"
"github.com/SystemBuilders/LocKey/internal/lockservice/node"
"github.com/rs/zerolog"
)
func TestLockService(t *testing.T) {
zerolog.New(os.Stdout).With()
log := zerolog.New(os.Stdout).With().Logger().Level(zerolog.GlobalLevel())
scfg := lockservice.NewSimpleConfig("http://127.0.0.1", "1234")
duration := 2 * time.Second // 2 second expiry
sessionDuration := 5 * time.Second
ls := lockservice.NewSimpleLockService(log, duration)
quit := make(chan bool, 1)
go func() {
node.Start(ls, *scfg)
for {
select {
case <-quit:
return
default:
}
}
}()
// Server takes some time to start.
time.Sleep(100 * time.Millisecond)
// Flow of creating a client and acquiring a lock:
// 1. Create a cache for the client.
// 2. Create a client and plug in the created cache.
// 3. Connect to the said client and hold on to the session value.
// 4. Use the session as a key for all further transactions.
t.Run("acquire test release test", func(t *testing.T) {
size := 5
cache := cache.NewLRUCache(size)
sc := NewSimpleClient(scfg, log, cache, sessionDuration)
session := sc.Connect()
d := lockservice.NewObjectDescriptor("test")
got := sc.Acquire(d, session)
var want error
if got != want {
t.Errorf("acquire: got %q want %q", got, want)
}
d = lockservice.NewObjectDescriptor("test1")
got = sc.Acquire(d, session)
if got != want {
t.Errorf("acquire: got %q want %q", got, want)
}
d = lockservice.NewObjectDescriptor("test")
got = sc.Release(d, session)
if got != want {
t.Errorf("release: got %q want %q", got, want)
}
d = lockservice.NewObjectDescriptor("test1")
got = sc.Release(d, session)
if got != want {
t.Errorf("release: got %q want %q", got, want)
}
})
t.Run("acquire test, acquire test, release test", func(t *testing.T) {
size := 5
cache := cache.NewLRUCache(size)
sc := NewSimpleClient(scfg, log, cache, sessionDuration)
session := sc.Connect()
d := lockservice.NewObjectDescriptor("test")
got := sc.Acquire(d, session)
var want error
if got != want {
t.Errorf("acquire: got %q want %q", got, want)
}
session2 := sc.Connect()
got = sc.Acquire(d, session2)
want = lockservice.ErrFileacquired
if got.Error() != want.Error() {
t.Errorf("acquire: got %q want %q", got, want)
}
got = sc.Release(d, session)
want = nil
if got != want {
t.Errorf("release: got %q want %q", got, want)
}
})
t.Run("acquire test, trying to release test as another entity should fail", func(t *testing.T) {
size := 2
cache := cache.NewLRUCache(size)
sc := NewSimpleClient(scfg, log, cache, sessionDuration)
session := sc.Connect()
d := lockservice.NewObjectDescriptor("test")
got := sc.Acquire(d, session)
var want error
if got != want {
t.Errorf("acquire: got %q want %q", got, want)
}
session2 := sc.Connect()
got = sc.Release(d, session2)
want = lockservice.ErrUnauthorizedAccess
if got != want {
t.Errorf("acquire: got %v want %v", got, want)
}
d = lockservice.NewObjectDescriptor("test2")
got = sc.Acquire(d, session)
want = nil
if got != want {
t.Errorf("acquire: got %q want %q", got, want)
}
d = lockservice.NewObjectDescriptor("test")
got = sc.Release(d, session)
want = nil
if got != want {
t.Errorf("release: got %q want %q", got, want)
}
d = lockservice.NewObjectDescriptor("test2")
got = sc.Release(d, session)
want = nil
if got != want {
t.Errorf("release: got %q want %q", got, want)
}
})
t.Run("acquire test and release after session expiry", func(t *testing.T) {
sc := NewSimpleClient(scfg, log, nil, sessionDuration)
session := sc.Connect()
d := lockservice.NewObjectDescriptor("test3")
got := sc.Acquire(d, session)
var want error
if got != want {
t.Errorf("acquire: got %q want %q", got, want)
}
// Wait for the lock's lease to expire
time.Sleep(3 * time.Second)
got = sc.Release(d, session)
want = lockservice.ErrCantReleaseFile
if got != want {
t.Errorf("release: got %q want %q", got, want)
}
})
t.Run("try acquiring after lock expiry; should succeed", func(t *testing.T) {
sc := NewSimpleClient(scfg, log, nil, sessionDuration)
session := sc.Connect()
d := lockservice.NewObjectDescriptor("test2")
got := sc.Acquire(d, session)
var want error
if got != want {
t.Errorf("acquire: got %q want %q", got, want)
}
// Wait for the lock's lease to expire
time.Sleep(3 * time.Second)
got = sc.Acquire(d, session)
if got != want {
t.Errorf("acquire: got %q want %q", got, want)
}
})
quit <- true
return
}
// BenchmarkLocKeyWithoutCache stats: 2130 28828088 ns/op 15952 B/op 190 allocs/op
func BenchmarkLocKeyWithoutCache(b *testing.B) {
zerolog.New(os.Stdout).With()
log := zerolog.New(os.Stdout).With().Logger().Level(zerolog.GlobalLevel())
scfg := lockservice.NewSimpleConfig("http://127.0.0.1", "1234")
ls := lockservice.NewSimpleLockService(log, 5)
quit := make(chan bool, 1)
go func() {
node.Start(ls, *scfg)
for {
select {
case <-quit:
return
default:
}
}
}()
time.Sleep(100 * time.Millisecond)
sessionDuration := 5 * time.Second
sc := NewSimpleClient(scfg, log, nil, sessionDuration)
session := sc.Connect()
d := lockservice.NewObjectDescriptor("test")
for n := 0; n < b.N; n++ {
got := sc.Acquire(d, session)
var want error
if got != want {
b.Errorf("acquire: got %q want %q", got, want)
}
got = sc.Release(d, session)
if got != want {
b.Errorf("release: got %q want %q", got, want)
}
}
}
// BenchmarkLocKeyWithCache stats: 3669 28702266 ns/op 16048 B/op 194 allocs/op
func BenchmarkLocKeyWithCache(b *testing.B) {
zerolog.New(os.Stdout).With()
log := zerolog.New(os.Stdout).With().Logger().Level(zerolog.GlobalLevel())
scfg := lockservice.NewSimpleConfig("http://127.0.0.1", "1234")
duration := 2 * time.Second // 2 second expiry
ls := lockservice.NewSimpleLockService(log, duration)
quit := make(chan bool, 1)
go func() {
node.Start(ls, *scfg)
for {
select {
case <-quit:
return
default:
}
}
}()
time.Sleep(100 * time.Millisecond)
size := 5
cache := cache.NewLRUCache(size)
sessionDuration := 5 * time.Second
sc := NewSimpleClient(scfg, log, cache, sessionDuration)
session := sc.Connect()
d := lockservice.NewObjectDescriptor("test")
for n := 0; n < b.N; n++ {
got := sc.Acquire(d, session)
var want error
if got != want {
b.Errorf("acquire: got %q want %q", got, want)
}
got = sc.Release(d, session)
if got != want {
b.Errorf("release: got %q want %q", got, want)
}
}
}