-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocker_test.go
49 lines (40 loc) · 1.36 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
package orm
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestLocker(t *testing.T) {
registry := NewRegistry()
registry.RegisterRedis("localhost:6385", 15, DefaultPoolCode, nil)
validatedRegistry, err := registry.Validate()
assert.Nil(t, err)
orm := validatedRegistry.NewContext(context.Background())
orm.Engine().Redis(DefaultPoolCode).FlushDB(orm)
testLogger := &MockLogHandler{}
orm.RegisterQueryLogger(testLogger, false, true, false)
l := orm.Engine().Redis(DefaultPoolCode).GetLocker()
lock, has := l.Obtain(orm, "test_key", time.Second, 0)
assert.True(t, has)
assert.NotNil(t, lock)
has = lock.Refresh(orm, time.Second)
assert.True(t, has)
_, has = l.Obtain(orm, "test_key", time.Second, time.Millisecond*100)
assert.False(t, has)
left := lock.TTL(orm)
assert.LessOrEqual(t, left.Microseconds(), time.Second.Microseconds())
lock.Release(orm) // dragonfly-db fix
_, has = l.Obtain(orm, "test_key", time.Second*10, time.Second*10)
assert.True(t, has)
lock.Release(orm)
lock.Release(orm)
has = lock.Refresh(orm, time.Second)
assert.False(t, has)
assert.PanicsWithError(t, "ttl must be higher than zero", func() {
_, _ = l.Obtain(orm, "test_key", 0, time.Millisecond)
})
assert.PanicsWithError(t, "waitTimeout can't be higher than ttl", func() {
_, _ = l.Obtain(orm, "test_key", time.Second, time.Second*2)
})
}