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,17 +1,14 @@
package bloom
import (
"log"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/charlienet/go-mixed/redis"
"github.com/stretchr/testify/assert"
"github.com/charlienet/go-mixed/tests"
)
func TestRedisStore(t *testing.T) {
runOnRedis(t, func(client redis.Client) {
tests.RunOnRedis(t, func(client redis.Client) {
store := newRedisStore(client, "abcdef", 10000)
err := store.Set(1, 2, 3, 9, 1223)
if err != nil {
@ -23,38 +20,3 @@ func TestRedisStore(t *testing.T) {
t.Log(store.Test(4, 5, 8))
})
}
func runOnRedis(t *testing.T, fn func(client redis.Client)) {
redis, clean, err := CreateMiniRedis()
assert.Nil(t, err)
defer clean()
fn(redis)
}
func CreateMiniRedis() (r redis.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 redis.New(&redis.ReidsOption{
Addrs: []string{addr},
}), func() {
ch := make(chan struct{})
go func() {
mr.Close()
close(ch)
}()
select {
case <-ch:
case <-time.After(time.Second):
}
}, nil
}