-
Notifications
You must be signed in to change notification settings - Fork 664
/
lock_test.go
44 lines (42 loc) · 845 Bytes
/
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
package lock
import (
"context"
"fmt"
"github.com/go-redis/redis"
"testing"
"time"
)
func TestRedis(t *testing.T) {
GlobalClient := redis.NewClient(
&redis.Options{
Addr: "172.12.12.165:6379",
DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Password: "",
PoolSize: 10,
DB: 0,
},
)
ping, err := GlobalClient.Ping(context.Background()).Result()
if nil != err {
panic(err)
}
fmt.Println("ping", ping)
redisLock := NewRedisLock(GlobalClient, "test", "1", time.Second*3)
InitRedis(redisLock)
select {}
}
func InitRedis(lock RedisLockServer) {
go func() {
for {
time.Sleep(time.Second)
err := lock.TryLock()
if err != nil {
fmt.Println("获取锁失败")
} else {
fmt.Println("获取锁成功")
}
}
}()
}