-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlock_test.go
64 lines (56 loc) · 1.37 KB
/
lock_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
package eredis
import (
"context"
"strings"
"testing"
"time"
"github.com/BurntSushi/toml"
"github.com/gotomicro/ego/core/econf"
"github.com/stretchr/testify/assert"
)
func TestLock(t *testing.T) {
cmp := newCmpLock(t)
lockClient := cmp.LockClient()
ctx := context.Background()
// try to obtain my Lock
lock, err := lockClient.Obtain(ctx, "my-key", 100*time.Millisecond)
if err == ErrNotObtained {
t.Log("Could not obtain Lock!")
} else if err != nil {
t.Fatal(err)
}
defer lock.Release(ctx)
t.Log("I have a Lock!")
// Sleep and check the remaining TTL.
time.Sleep(50 * time.Millisecond)
if ttl, err := lock.TTL(ctx); err != nil {
t.Fatal("check ttl fail,", err)
} else if ttl > 0 {
t.Log("Yay, I still have my Lock!")
}
// Extend my Lock.
if err := lock.Refresh(ctx, 100*time.Millisecond); err != nil {
t.Fatal(err)
}
// Sleep a little longer, then check.
time.Sleep(100 * time.Millisecond)
if ttl, err := lock.TTL(ctx); err != nil {
t.Fatal(err)
} else if ttl == 0 {
t.Log("Now, my Lock has expired!")
}
}
func newCmpLock(t *testing.T) *Component {
conf := `
[redis]
debug=true
addr="localhost:6379"
enableAccessInterceptor = true
enableAccessInterceptorReq = true
enableAccessInterceptorRes = true
`
err := econf.LoadFromReader(strings.NewReader(conf), toml.Unmarshal)
assert.NoError(t, err)
cmp := Load("redis").Build()
return cmp
}