唯一的值改为uuid
This commit is contained in:
+18
-9
@@ -3,10 +3,11 @@ package lockx_test
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/yuninks/lockx"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/yuninks/lockx"
|
||||
)
|
||||
|
||||
var Redis *redis.Client
|
||||
@@ -28,8 +29,8 @@ var Redis *redis.Client
|
||||
func TestLockx(t *testing.T) {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: "127.0.0.1" + ":" + "6379",
|
||||
Password: "", // no password set
|
||||
DB: 0, // use default DB
|
||||
Password: "123456", // no password set
|
||||
DB: 0, // use default DB
|
||||
})
|
||||
if client == nil {
|
||||
fmt.Println("redis init error")
|
||||
@@ -40,13 +41,21 @@ func TestLockx(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
lock := lockx.NewGlobalLock(ctx, client, "lockx:test")
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
if !lock.Lock() {
|
||||
fmt.Println("lock error")
|
||||
for i := 0; i < 10000; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
lock := lockx.NewGlobalLock(ctx, client, "lockx:test")
|
||||
defer lock.Unlock()
|
||||
if !lock.Lock() {
|
||||
fmt.Println("lock error", i)
|
||||
return
|
||||
}
|
||||
fmt.Println("ssss", i)
|
||||
}(i)
|
||||
}
|
||||
defer lock.Unlock()
|
||||
|
||||
fmt.Println("ssss")
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user