Files
timerx/lockx/lockx_test.go
T

54 lines
1016 B
Go
Raw Normal View History

2023-08-29 10:27:36 +08:00
package lockx_test
import (
"context"
"fmt"
"testing"
"code.yun.ink/open/timer/lockx"
"github.com/go-redis/redis/v8"
)
var Redis *redis.Client
2023-09-02 12:19:27 +08:00
// func TestMain(m *testing.M) {
// client := redis.NewClient(&redis.Options{
// Addr: "127.0.0.1" + ":" + "6379",
// Password: "", // no password set
// DB: 0, // use default DB
// })
// if client == nil {
// fmt.Println("redis init error")
// return
// }
// // fmt.Println("ffff")
// Redis = client
// }
func TestLockx(t *testing.T) {
2023-08-29 10:27:36 +08:00
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1" + ":" + "6379",
Password: "", // no password set
DB: 0, // use default DB
})
if client == nil {
fmt.Println("redis init error")
return
}
2023-09-02 12:19:27 +08:00
fmt.Println("begin")
2023-08-29 10:27:36 +08:00
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
2023-09-02 12:19:27 +08:00
lock := lockx.NewGlobalLock(ctx, client, "lockx:test")
2023-08-29 10:27:36 +08:00
if !lock.Lock() {
2023-09-02 12:19:27 +08:00
fmt.Println("lock error")
2023-08-29 10:27:36 +08:00
}
defer lock.Unlock()
lock.Refresh()
2023-09-02 12:19:27 +08:00
fmt.Println("ssss")
2023-08-29 10:27:36 +08:00
}