1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00

添加分布式锁实现-使用Redis

This commit is contained in:
2023-10-12 15:25:13 +08:00
parent 69690da6b4
commit e83db7daee
5 changed files with 307 additions and 81 deletions

View File

@ -1,19 +1,19 @@
package redis
package redis_test
import (
"context"
"fmt"
"log"
"sync"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/charlienet/go-mixed/redis"
"github.com/charlienet/go-mixed/tests"
"github.com/stretchr/testify/assert"
)
func TestGetSet(t *testing.T) {
runOnRedis(t, func(client Client) {
tests.RunOnRedis(t, func(client redis.Client) {
ctx := context.Background()
val, err := client.GetSet(ctx, "hello", "world").Result()
@ -39,7 +39,7 @@ func TestGetSet(t *testing.T) {
}
func TestRedis_SetGetDel(t *testing.T) {
runOnRedis(t, func(client Client) {
tests.RunOnRedis(t, func(client redis.Client) {
ctx := context.Background()
_, err := client.Set(ctx, "hello", "world", 0).Result()
@ -55,7 +55,7 @@ func TestRedis_SetGetDel(t *testing.T) {
}
func TestPubSub(t *testing.T) {
runOnRedis(t, func(client Client) {
tests.RunOnRedis(t, func(client redis.Client) {
ctx := context.Background()
c := "chat"
@ -132,38 +132,3 @@ func TestPubSub(t *testing.T) {
t.Logf("total received %d message", total)
})
}
func runOnRedis(t *testing.T, fn func(client Client)) {
redis, clean, err := CreateMiniRedis()
assert.Nil(t, err)
defer clean()
fn(redis)
}
func CreateMiniRedis() (r Client, clean func(), err error) {
mr, err := miniredis.Run()
if err != nil {
return nil, nil, err
}
addr := mr.Addr()
log.Println("mini redis run at:", addr)
return New(&ReidsOption{
Addrs: []string{addr},
}), func() {
ch := make(chan struct{})
go func() {
mr.Close()
close(ch)
}()
select {
case <-ch:
case <-time.After(time.Second):
}
}, nil
}