forked from latolukasz/beeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocker_test.go
More file actions
50 lines (40 loc) · 1.14 KB
/
locker_test.go
File metadata and controls
50 lines (40 loc) · 1.14 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
package beeorm
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestLocker(t *testing.T) {
testLocker(t, "")
}
func TestLockerNamespace(t *testing.T) {
testLocker(t, "test")
}
func testLocker(t *testing.T, namespace string) {
registry := &Registry{}
registry.RegisterRedis("localhost:6382", namespace, 15)
validatedRegistry, def, err := registry.Validate()
assert.Nil(t, err)
defer def()
engine := validatedRegistry.CreateEngine()
engine.GetRedis().FlushDB()
testLogger := &testLogHandler{}
engine.RegisterQueryLogger(testLogger, false, true, false)
l := engine.GetRedis().GetLocker()
lock, has := l.Obtain("test_key", time.Second, 0)
assert.True(t, has)
assert.NotNil(t, lock)
has = lock.Refresh(time.Second)
assert.True(t, has)
_, has = l.Obtain("test_key", time.Second, time.Millisecond)
assert.False(t, has)
left := lock.TTL()
assert.LessOrEqual(t, left.Microseconds(), time.Second.Microseconds())
lock.Release()
lock.Release()
has = lock.Refresh(time.Second)
assert.False(t, has)
assert.PanicsWithError(t, "ttl must be higher than zero", func() {
_, _ = l.Obtain("test_key", 0, time.Millisecond)
})
}